From fa622d4583832e2e1a19fcaf30a5f2a9cf10d29d Mon Sep 17 00:00:00 2001 From: SajidYarKhan Date: Mon, 30 Jan 2017 11:21:36 -0600 Subject: [PATCH] DesignPattern-Sajid --- .../Execise.Tests/UnitTests.cs | 22 ++++++++++++++++--- .../Exercise/BookBorrowable.cs | 20 +++++++++++++++-- DesignPatternsSolution/Exercise/LibObject.cs | 12 +++++++++- DesignPatternsSolution/Exercise/Library.cs | 9 +++++++- 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..1d66dd5 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -10,20 +10,30 @@ 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 + var libraryInstance1 = Library.GetInstance(); + var libraryInstance2 = Library.GetInstance(); + + Assert.AreSame(libraryInstance1, libraryInstance2); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + var book = new Book("Jason Mark","Clean Code",1997,25); + var response = RegistrationRepository.Register(book); + + Assert.IsTrue(response > -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 Mark","386 Broadway"); + var response = RegistrationRepository.Register(customer); + + Assert.IsTrue(response > -1); } //test that a book can be borrowed @@ -32,7 +42,13 @@ 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. - + int bookInInventory = 25; + var book = new Book("Jason Mark", "Clean Code", 1997, bookInInventory); + var borrowable = new BookBorrowable(book); + + borrowable.BorrowBook(); + Assert.IsTrue(book.AvailableAmount == bookInInventory - 1); + } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..5f2789e 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,23 @@ namespace Exercise { - public class BookBorrowable + public class BookBorrowable : Book { - + private Book Book { get; set; } + + public BookBorrowable(Book canBorrow) : base(canBorrow.Author, canBorrow.NameOrTitle, canBorrow.YearCreated, canBorrow.AvailableAmount) + { + Book = canBorrow; + } + + public bool BorrowBook() + { + if (Book.AvailableAmount > 0) + { + Book.AvailableAmount--; + return true; + } + + return false; + } } } diff --git a/DesignPatternsSolution/Exercise/LibObject.cs b/DesignPatternsSolution/Exercise/LibObject.cs index cf1e2bb..99a0913 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..eb2f2e2 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -3,8 +3,15 @@ public class Library { //implement Singleton to make sure only one library will exist + private static Library Instance { get; set; } - + + private Library() { Instance = new Library(); } + + public static Library GetInstance() + { + return Instance; + } //Implement Register method by utilizing RegistrationRepository (complete missing parts) public int Register()