diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..2a7fd17 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -1,5 +1,6 @@ using Exercise; using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; namespace Execise.Tests { @@ -10,29 +11,46 @@ 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 + Library myLib = Library.getInstance(); + Library secondLib = Library.getInstance(); + + Assert.AreSame(myLib, secondLib); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + Library myLib = Library.getInstance(); + Book book = new Book("Author", "Title", 2010, 5); + int ret_code = myLib.Register(book); + Assert.AreNotEqual(-1, ret_code); + } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + Console.WriteLine("WHAT"); + Library myLib = Library.getInstance(); + Customer customer = new Customer("Name", "Address"); + int ret_code = myLib.Register(customer); + Assert.AreNotEqual(-1, ret_code); } //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. - + //Didn't do decorator.. this is more reliable. + int start_book_count = 30; + Book borrowBook = new Book("Author2", "Title2", 1990, start_book_count); + Library myLib = Library.getInstance(); + int book_id = myLib.Register(borrowBook); + myLib.borrow(book_id); + int book_count = myLib.getAvailableAmount(book_id); + Assert.AreEqual(start_book_count - 1, book_count); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..e42b522 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,7 @@ namespace Exercise { - public class BookBorrowable + public interface IBorrowable { - + } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..13c5e4b 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -10,13 +10,22 @@ public Item(int amount, int year) } } - public class Book : Item + public class Book : Item, IRegistarable, IBorrowable { public Book(string author, string title, int year, int amount) : base(amount, year) { NameOrTitle = title; Author = author; } + + public RegisteredObject GetRegistrationInfo() + { + RegisteredObject obj = new RegisteredObject(); + obj.AvailableAmount = this.AvailableAmount; + obj.Info = "Stuff about this book"; + obj.obj = this; + return obj; + } public string Author { get; set; } } } diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..7a7d43e 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -3,16 +3,31 @@ public class Library { //implement Singleton to make sure only one library will exist - + private static Library library = new Library(); + private Library() { + + } + + public static Library getInstance() + { + return library; + } //Implement Register method by utilizing RegistrationRepository (complete missing parts) - public int Register() + public int Register(IRegistarable registarable) { - // return RegistrationRepository.Register(); + return RegistrationRepository.Register(registarable); + } - //dummy, just to compile - return 0; + public int borrow(int book_id) + { + return RegistrationRepository.BorrowBook(book_id); + } + + public int getAvailableAmount(int book_id) + { + return RegistrationRepository.GetAvailableAmount(book_id); } } } diff --git a/DesignPatternsSolution/Exercise/Person.cs b/DesignPatternsSolution/Exercise/Person.cs index 2c2ed21..0ba8fe9 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() + { + RegisteredObject obj = new RegisteredObject(); + obj.AvailableAmount = this.AvailableAmount; + obj.Info = "Stuff about this person"; + obj.obj = this; + return obj; + } } } diff --git a/DesignPatternsSolution/Exercise/Registration.cs b/DesignPatternsSolution/Exercise/Registration.cs index f8c4eb0..a60bc5c 100644 --- a/DesignPatternsSolution/Exercise/Registration.cs +++ b/DesignPatternsSolution/Exercise/Registration.cs @@ -43,6 +43,33 @@ public static int DeleteAllRegisteredItems() return size; } + + public static int BorrowBook(int book_id) + { + foreach(RegisteredObject obj in _registeredList) { + if (obj.Id == book_id) + { + if (obj.obj is IBorrowable && obj.AvailableAmount > 0) + { + obj.AvailableAmount -= 1; + return 0; + } + } + } + return -1; + } + + public static int GetAvailableAmount(int book_id) + { + foreach (RegisteredObject obj in _registeredList) + { + if (obj.Id == book_id) + { + return obj.AvailableAmount; + } + } + return -1; + } } public class RegisteredObject @@ -50,6 +77,7 @@ public class RegisteredObject public string Info { get; set; } public int Id { get; set; } public int AvailableAmount { get; set; } + public IRegistarable obj { get; set; } public override string ToString() {