diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..2ca60bc 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -12,18 +12,27 @@ 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.GetLibrary(); + Library lib2 = Library.GetLibrary(); + Assert.AreSame(lib1, lib2); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + Library lib = Library.GetLibrary(); + Book book = new Book("author", "title", 2017, 10); + Assert.AreNotEqual(lib.Register(book), -1); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + Library lib = Library.GetLibrary(); + Customer customer = new Customer("name", "addr"); + Assert.AreNotEqual(lib.Register(customer), -1); } //test that a book can be borrowed @@ -32,7 +41,12 @@ 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. + Book book = new Book("author", "title", 2017, 10); + int old = book.AvailableAmount; + + BookBorrowable bookBorrowable = new BookBorrowable(null, new BorrowOne(book)); + Assert.AreEqual(old - 1, book.AvailableAmount); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..ef6f92c 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,38 @@ namespace Exercise { - public class BookBorrowable + public abstract class ABookBorrowable { - + public virtual void BorrowOne() { } + protected BorrowOne BorrowOneProcedure; + + public ABookBorrowable(BorrowOne borrowOneProcedure) + { + BorrowOneProcedure = borrowOneProcedure; + } + } + + public class BookBorrowable : ABookBorrowable + { + private ABookBorrowable _bookBorrowable; + + public BookBorrowable(ABookBorrowable bookBorrowable, BorrowOne borrowOne) : base(borrowOne) + { + _bookBorrowable = bookBorrowable; + } + + public override void BorrowOne() + { + _bookBorrowable.BorrowOne(); + } + } + + public class BorrowOne + { + private LibObject item; + + public BorrowOne(LibObject item) + { + item.AvailableAmount--; + } } } diff --git a/DesignPatternsSolution/Exercise/LibObject.cs b/DesignPatternsSolution/Exercise/LibObject.cs index cf1e2bb..8fc5465 100644 --- a/DesignPatternsSolution/Exercise/LibObject.cs +++ b/DesignPatternsSolution/Exercise/LibObject.cs @@ -1,12 +1,16 @@ 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; } + public RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject(); + } } public enum ObjectType diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..d9b00fa 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -3,16 +3,22 @@ public class Library { //implement Singleton to make sure only one library will exist - + private static Library instance = new Library(); + private Library() { } + + public static Library GetLibrary() + { + return instance; + } //Implement Register method by utilizing RegistrationRepository (complete missing parts) - public int Register() + public int Register(IRegistarable item) { // return RegistrationRepository.Register(); //dummy, just to compile - return 0; + return RegistrationRepository.Register(item); } } }