From 89bd2e5cf32b57700a8efc56d32085b668757f31 Mon Sep 17 00:00:00 2001 From: Brandon Meilleur Date: Thu, 26 Jan 2017 11:50:29 -0600 Subject: [PATCH] Changeset for Brandon Meilleur --- .../Execise.Tests/UnitTests.cs | 15 +++++++++--- .../Exercise/BookBorrowable.cs | 23 +++++++++++++++++-- DesignPatternsSolution/Exercise/Item.cs | 10 ++++++++ DesignPatternsSolution/Exercise/LibObject.cs | 4 +++- DesignPatternsSolution/Exercise/Library.cs | 17 ++++++++++---- DesignPatternsSolution/Exercise/Person.cs | 8 +++++++ 6 files changed, 66 insertions(+), 11 deletions(-) diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..c29f918 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -10,20 +10,28 @@ public class UnitTests [TestMethod] public void LibraryShouldBeSingleton() { - //check that when you create a Library instance second time, - //you get exactly the same instance as for a very first time + Library lib1 = Library.getInstance(); + Library lib2 = Library.getInstance(); + + Assert.AreSame(lib1, lib2, "Objects are not the same instance!"); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + Library library = Library.getInstance(); + LibObject book = new Book("Brandon Meilleur", "Best Code Practices", 1994, 20); + Assert.AreNotEqual(-1, library.Register(book)); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + Library library = Library.getInstance(); + LibObject customer = new Customer("Brandon Meilleur", "123 Fake St"); + Assert.AreNotEqual(-1, library.Register(customer)); } //test that a book can be borrowed @@ -32,7 +40,8 @@ public void CanBorrowBook() { //create a borrowable book with available amount more than one. //Run BorrowOne method of the BookBorrowable instance. Check that total amount was reduced by one. - + BookBorrowable borrowable = new BookBorrowable("Brandon Meilleur", "Cool Things and Stuff", 2000, 15); + Assert.AreEqual(14, borrowable.borrowOne()); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..17caea5 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,26 @@ namespace Exercise { - public class BookBorrowable + public class BookBorrowable : Book { - + + public BookBorrowable(string author, string title, int year, int amount) + : base(author, title, year, amount) + { + + } + + public int borrowOne() + { + if (this.AvailableAmount > 0) + { + this.AvailableAmount -= 1; + return this.AvailableAmount; + } + else + { + return -1; + } + + } } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..3146b54 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -8,6 +8,16 @@ public Item(int amount, int year) ObjType = ObjectType.Item; YearCreated = year; } + + override public RegisteredObject GetRegistrationInfo() + { + RegisteredObject registerable = new RegisteredObject(); + registerable.Info = ObjType.ToString(); + registerable.AvailableAmount = AvailableAmount; + + return registerable; + } + } public class Book : Item diff --git a/DesignPatternsSolution/Exercise/LibObject.cs b/DesignPatternsSolution/Exercise/LibObject.cs index cf1e2bb..c74ef04 100644 --- a/DesignPatternsSolution/Exercise/LibObject.cs +++ b/DesignPatternsSolution/Exercise/LibObject.cs @@ -1,12 +1,14 @@ namespace Exercise { - public abstract class LibObject + public abstract class LibObject : IRegistarable { public int ObjectId { get; set; } public int AvailableAmount { get; set; } public string NameOrTitle { get; set; } public ObjectType ObjType { get; set; } public int YearCreated { get; set; } + + abstract public RegisteredObject GetRegistrationInfo(); } public enum ObjectType diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..ca49ac2 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -3,16 +3,23 @@ public class Library { //implement Singleton to make sure only one library will exist + static Library instance = new Library(); + private Library() + { + //nothing needed here + } + + public static Library getInstance() + { + return instance; + } //Implement Register method by utilizing RegistrationRepository (complete missing parts) - public int Register() + public int Register(LibObject libObject) { - // return RegistrationRepository.Register(); - - //dummy, just to compile - return 0; + return RegistrationRepository.Register(libObject); } } } diff --git a/DesignPatternsSolution/Exercise/Person.cs b/DesignPatternsSolution/Exercise/Person.cs index 2c2ed21..87fbb74 100644 --- a/DesignPatternsSolution/Exercise/Person.cs +++ b/DesignPatternsSolution/Exercise/Person.cs @@ -19,5 +19,13 @@ public void SetId(int id) { ObjectId = id; } + + override public RegisteredObject GetRegistrationInfo() + { + RegisteredObject registerable = new RegisteredObject(); + registerable.Info = ObjType.ToString(); + + return registerable; + } } }