diff --git a/DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj b/DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj index 71e68ac..4f78f0d 100644 --- a/DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj +++ b/DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj @@ -8,7 +8,7 @@ Properties Execise.Tests Execise.Tests - v4.5.2 + v4.5 512 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 10.0 @@ -16,6 +16,7 @@ $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages False UnitTest + true diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..0b01aa3 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -10,29 +10,43 @@ 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 + Assert.AreEqual(Library.getInstance(), Library.getInstance()); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + Library library = Library.getInstance(); + + Book book = new Book("author", "title", 1950, 2, new LibObjectRegistrationInfoAPI()); + + Assert.AreNotEqual(-1, library.Register(book)); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + Library library = Library.getInstance(); + + Customer customer = new Customer("name", "address", new LibObjectRegistrationInfoAPI()); + + Assert.AreNotEqual(-1, library.Register(customer)); } //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. - + Library library = Library.getInstance(); + + Book book = new Book("author", "title", 1950, 2, new LibObjectRegistrationInfoAPI()); + BookBorrowable borrowable = new BookBorrowable(null, book); + + borrowable.BorrowOne(); + + Assert.AreEqual(1, book.AvailableAmount); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..f06f4d5 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,33 @@ namespace Exercise { - public class BookBorrowable + public abstract class Borrowable { - + abstract public void BorrowOne(); + } + + public class BookBorrowable : Borrowable + { + private Book _book; + private Borrowable _borrowable; + + public BookBorrowable(Borrowable borrowable, Book book) + { + this._borrowable = borrowable; + this._book = book; + } + + override public void BorrowOne() + { + // note that this is not decorating a "book", but a "Borrowable" + if(_borrowable != null) + { + _borrowable.BorrowOne(); + } + + if (this._book.AvailableAmount > 0) + { + this._book.AvailableAmount--; + } + } } } diff --git a/DesignPatternsSolution/Exercise/Person.cs b/DesignPatternsSolution/Exercise/Customer.cs similarity index 75% rename from DesignPatternsSolution/Exercise/Person.cs rename to DesignPatternsSolution/Exercise/Customer.cs index 2c2ed21..e530af0 100644 --- a/DesignPatternsSolution/Exercise/Person.cs +++ b/DesignPatternsSolution/Exercise/Customer.cs @@ -7,7 +7,8 @@ public class Customer : LibObject public string Address { get; set; } public DateTime RegisteredAt { get; set; } - public Customer(string name, string addr) + public Customer(string name, string addr, IRegistrationInfoAPI registerableInfoBridge) + : base(registerableInfoBridge) { NameOrTitle = name; Address = addr; diff --git a/DesignPatternsSolution/Exercise/Exercise.csproj b/DesignPatternsSolution/Exercise/Exercise.csproj index d2cc582..2fd3252 100644 --- a/DesignPatternsSolution/Exercise/Exercise.csproj +++ b/DesignPatternsSolution/Exercise/Exercise.csproj @@ -9,9 +9,10 @@ Properties Exercise Exercise - v4.5.2 + v4.5 512 true + AnyCPU @@ -47,11 +48,13 @@ + - + + diff --git a/DesignPatternsSolution/Exercise/IRegistrationInfoAPI.cs b/DesignPatternsSolution/Exercise/IRegistrationInfoAPI.cs new file mode 100644 index 0000000..f07e099 --- /dev/null +++ b/DesignPatternsSolution/Exercise/IRegistrationInfoAPI.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Exercise +{ + public interface IRegistrationInfoAPI + { + RegisteredObject GetRegistrationInfo(LibObject objectIn); + } +} diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..f67d6fe 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -2,7 +2,7 @@ { public abstract class Item : LibObject { - public Item(int amount, int year) + public Item(int amount, int year, IRegistrationInfoAPI registerableInfoBridge) : base(registerableInfoBridge) { AvailableAmount = amount; ObjType = ObjectType.Item; @@ -12,7 +12,7 @@ public Item(int amount, int year) 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, IRegistrationInfoAPI registerableInfoBridge) : base(amount, year, registerableInfoBridge) { NameOrTitle = title; Author = author; diff --git a/DesignPatternsSolution/Exercise/LibObject.cs b/DesignPatternsSolution/Exercise/LibObject.cs index cf1e2bb..1a4b4d2 100644 --- a/DesignPatternsSolution/Exercise/LibObject.cs +++ b/DesignPatternsSolution/Exercise/LibObject.cs @@ -1,12 +1,24 @@ namespace Exercise { - public abstract class LibObject + public abstract class LibObject : IRegistarable { + private IRegistrationInfoAPI _registerableInfoBridge; + 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 _registerableInfoBridge.GetRegistrationInfo(this); + } + + public LibObject(IRegistrationInfoAPI registerableInfoBridge) + { + this._registerableInfoBridge = registerableInfoBridge; + } } public enum ObjectType diff --git a/DesignPatternsSolution/Exercise/LibObjectRegistrationInfoAPI.cs b/DesignPatternsSolution/Exercise/LibObjectRegistrationInfoAPI.cs new file mode 100644 index 0000000..0d9a1a2 --- /dev/null +++ b/DesignPatternsSolution/Exercise/LibObjectRegistrationInfoAPI.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Exercise +{ + public class LibObjectRegistrationInfoAPI : IRegistrationInfoAPI + { + public LibObjectRegistrationInfoAPI() { } + + public RegisteredObject GetRegistrationInfo(LibObject objectIn) + { + RegisteredObject regObject = new RegisteredObject(); + regObject.Info = objectIn.NameOrTitle; + regObject.AvailableAmount = objectIn.AvailableAmount; + regObject.Id = objectIn.ObjectId; + return regObject; + } + } +} diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..ae560ca 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -2,17 +2,25 @@ { public class Library { + private static Library instance; + //implement Singleton to make sure only one library will exist + public static Library getInstance() + { + if (instance == null) + { + instance = new Library(); + } + return instance; + } + private Library() {} //Implement Register method by utilizing RegistrationRepository (complete missing parts) - public int Register() + public int Register(IRegistarable registerable) { - // return RegistrationRepository.Register(); - - //dummy, just to compile - return 0; + return RegistrationRepository.Register(registerable); } } }