diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..ff4252b 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 library1 = Library.getInstance(); + var library2 = Library.getInstance(); + Assert.AreEqual(library1, library2); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + var book = new Book("J.D. Salinger", "The Catcher in the Rye", 1951, 13); + var bookId = RegistrationRepository.Register(book); + Assert.IsFalse(bookId == -1); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + var customer = new Customer("Jason Wood", "386 Broadway"); + var customerId = RegistrationRepository.Register(customer); + Assert.IsFalse(customerId == -1); } //test that a book can be borrowed @@ -32,7 +41,11 @@ 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. - + const int bookAmount = 13; + var book = new Book("J.D. Salinger", "The Catcher in the Rye", 1951, bookAmount); + var bookBorrowable = new BookBorrowable(book, book.Author, book.NameOrTitle, book.YearCreated, book.AvailableAmount); + bookBorrowable.BorrowOne(); + Assert.IsTrue(bookBorrowable._book.AvailableAmount == bookAmount - 1); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..01ff016 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,24 @@ namespace Exercise { - public class BookBorrowable + public class BookBorrowable : Book { - + public Book _book { get; private set; } + + public BookBorrowable(Book book, string author, string title, int year, int amount) : base(author, title, year, amount) + { + _book = book; + } + + public void BorrowOne() + { + if (_book.AvailableAmount > 0) + { + _book.AvailableAmount--; + } + else + { + _book.AvailableAmount = -1; + } + } } } diff --git a/DesignPatternsSolution/Exercise/LibObject.cs b/DesignPatternsSolution/Exercise/LibObject.cs index cf1e2bb..3c13600 100644 --- a/DesignPatternsSolution/Exercise/LibObject.cs +++ b/DesignPatternsSolution/Exercise/LibObject.cs @@ -1,12 +1,22 @@ 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() + { + AvailableAmount = AvailableAmount, + Id = ObjectId, + Info = NameOrTitle + }; + } } public enum ObjectType diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..67ee0b3 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -3,8 +3,14 @@ public class Library { //implement Singleton to make sure only one library will exist + static Library libraryInstance = new Library(); - + private Library() { } + + public static Library getInstance() + { + return libraryInstance; + } //Implement Register method by utilizing RegistrationRepository (complete missing parts) public int Register()