diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..ce6556f 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 + var first = Library.getLibrary(); + var second = Library.getLibrary(); + + Assert.AreEqual(first, second); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + int x = RegistrationRepository.Register(new Book("Bakul", "No title", 2015, 19)); + + Assert.AreNotEqual(-1, x); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + int x = RegistrationRepository.Register(new Customer("Bakul", "320 street not available")); + Assert.AreNotEqual(-1, x); } //test that a book can be borrowed @@ -32,7 +41,10 @@ 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 b = new Book("Bakul", "No title", 2015, 200); + BookBorrowable bb = new BookBorrowable(b, new Decorator()); + bb.Borrow(); + Assert.AreNotEqual(200, b.AvailableAmount); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..3b04535 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,28 @@ namespace Exercise { - public class BookBorrowable + public class BookBorrowable : Book { - + private Book thing; + private Decorator decorator; + + public BookBorrowable(Book book, Decorator decorator) : + base(book.Author, book.NameOrTitle, book.YearCreated, book.AvailableAmount) + { + thing = book; + this.decorator = decorator; + } + + public void Borrow() + { + decorator.Borrow(thing); + } + } + + public class Decorator + { + public void Borrow(Book x) + { + x.AvailableAmount--; + } } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..972eab0 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -10,7 +10,7 @@ public Item(int amount, int year) } } - public class Book : Item + public class Book : Item, IRegistarable { public Book(string author, string title, int year, int amount) : base(amount, year) { @@ -18,5 +18,9 @@ public Book(string author, string title, int year, int amount) : base(amount, ye Author = author; } public string Author { get; set; } + public RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject() { AvailableAmount = AvailableAmount, Info = NameOrTitle }; + } } } diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..af0c347 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -4,7 +4,11 @@ public class Library { //implement Singleton to make sure only one library will exist - + static Library library = new Library(); + + private Library() { } + + public static Library getLibrary() { return library; } //Implement Register method by utilizing RegistrationRepository (complete missing parts) public int Register() diff --git a/DesignPatternsSolution/Exercise/Person.cs b/DesignPatternsSolution/Exercise/Person.cs index 2c2ed21..977cd1d 100644 --- a/DesignPatternsSolution/Exercise/Person.cs +++ b/DesignPatternsSolution/Exercise/Person.cs @@ -2,7 +2,7 @@ namespace Exercise { - public class Customer : LibObject + public class Customer : LibObject , IRegistarable { public string Address { get; set; } public DateTime RegisteredAt { get; set; } @@ -14,7 +14,13 @@ public Customer(string name, string addr) RegisteredAt = DateTime.Now; ObjType = ObjectType.Person; } - + public RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject() + { + Info = NameOrTitle + }; + } public void SetId(int id) { ObjectId = id;