diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..fd6b341 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -10,29 +10,39 @@ 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 l1 = Library.GetInstance(); + var l2 = Library.GetInstance(); + + Assert.AreSame(l1, l2); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + int value = RegistrationRepository.Register(new Book("Author", "Title", 1997, 5)); + + Assert.AreNotEqual(-1, value); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + int value = RegistrationRepository.Register(new Customer("Customer Name", "Address")); + + Assert.AreNotEqual(-1, value); } //test that a book can be borrowed [TestMethod] 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. - + var book = new Book("Author", "Title", 1997, 5); + var borrowable = new BookBorrowable(book); + + borrowable.BorrowOne(); + Assert.AreEqual(4, book.AvailableAmount); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..8c70b74 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,24 @@ namespace Exercise { - public class BookBorrowable + public class BookBorrowable : Book { - + private Book Book { get; set; } + + public BookBorrowable(Book bookToBorrow) : base(bookToBorrow.Author, bookToBorrow.NameOrTitle, bookToBorrow.YearCreated, bookToBorrow.YearCreated) + { + this.Book = bookToBorrow; + } + + public bool BorrowOne() + { + if (this.Book.AvailableAmount > 0) + { + this.Book.AvailableAmount--; + return true; + } + else + return false; + + } } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..475a885 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -1,4 +1,6 @@ -namespace Exercise +using System; + +namespace Exercise { public abstract class Item : LibObject { diff --git a/DesignPatternsSolution/Exercise/LibObject.cs b/DesignPatternsSolution/Exercise/LibObject.cs index cf1e2bb..756d181 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 = this.AvailableAmount, + Id = this.ObjectId, + Info = this.NameOrTitle + }; + } } public enum ObjectType diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..c86c759 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -2,9 +2,10 @@ { 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 Library.Instance; } //Implement Register method by utilizing RegistrationRepository (complete missing parts) public int Register()