diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..1aef735 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -12,18 +12,29 @@ 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.Instance; + Library lib2 = Library.Instance; + Assert.ReferenceEquals(lib1, lib2); } - //test that an book was registered successfully by checking the returned Id value is not -1 + //test that a book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + Library lib1 = Library.Instance; + Book book1 = new Book("I am Great", "Donald Trump", 2017, 19); + int result = lib1.Register(book1); + Assert.AreNotEqual(result, -1); } - //test that an customer was registered successfully by checking the returned Id value is not -1 + //test that a customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + Library lib1 = Library.Instance; + Customer cust1 = new Customer("Daffy Duck", "123 Dippy Lane, Disneyland, CA"); + int result = lib1.Register(cust1); + Assert.AreNotEqual(result, -1); } //test that a book can be borrowed @@ -32,7 +43,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. - - } + Library lib1 = Library.Instance; + Book book1 = new Book("I am Great", "Donald Trump", 2017, 19); + int result = lib1.Register(book1); + + Assert.AreNotEqual(result, -1); + + } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..b8c7e07 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -2,6 +2,6 @@ { public class BookBorrowable { - + // find book in Registration Repository as class RegisteredObject and return Available Amount? } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..452022b 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -1,6 +1,6 @@ namespace Exercise { - public abstract class Item : LibObject + public abstract class Item : LibObject, IRegistarable { public Item(int amount, int year) { @@ -8,15 +8,34 @@ public Item(int amount, int year) ObjType = ObjectType.Item; YearCreated = year; } + + //public int Register() + //{ + // return 0; + //} + + public abstract RegisteredObject GetRegistrationInfo(); + } public class Book : Item { - public Book(string author, string title, int year, int amount) : base(amount, year) + public Book(string author, string title, int year, int amount) + : base(amount, year) { NameOrTitle = title; Author = author; } public string Author { get; set; } + + //my code + public override RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject + { + Info = "Book Name: " + NameOrTitle + " , Author: " + Author + " Year: " + YearCreated, + AvailableAmount = AvailableAmount + }; + } } } diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..5b3e7be 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -1,18 +1,32 @@ namespace Exercise { - public class Library + public sealed class Library { //implement Singleton to make sure only one library will exist + private static Library instance; + private Library() { } - + public static Library Instance + { + get + { + if (instance == null) + { + instance = new Library(); + } + return instance; + } + } + + //public static Library getInstance() () //Implement Register method by utilizing RegistrationRepository (complete missing parts) - public int Register() + public int Register(IRegistarable newRegObject) { - // return RegistrationRepository.Register(); + return RegistrationRepository.Register(newRegObject); //dummy, just to compile - return 0; + //return 0; } } } diff --git a/DesignPatternsSolution/Exercise/Person.cs b/DesignPatternsSolution/Exercise/Person.cs index 2c2ed21..fed86e7 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; } @@ -19,5 +19,14 @@ public void SetId(int id) { ObjectId = id; } + + public RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject + { + Info = "Customer Name: " + NameOrTitle + " , Address: " + Address + " Date Registered: " + RegisteredAt, + AvailableAmount = AvailableAmount + }; + } } }