From f436f87afdef61e4f008fb4a85b754e2143a4a59 Mon Sep 17 00:00:00 2001 From: VarianLuke Date: Fri, 27 Jan 2017 17:40:23 -0600 Subject: [PATCH] Luke changes. Reason for usage of Decorator pattern in Part 3 was unclear from the instructions. --- .../Execise.Tests/UnitTests.cs | 28 +++++++++++++++++-- .../Exercise/BookBorrowable.cs | 25 +++++++++++++++-- DesignPatternsSolution/Exercise/Item.cs | 19 +++++++++++-- DesignPatternsSolution/Exercise/Library.cs | 13 +++++---- DesignPatternsSolution/Exercise/Person.cs | 12 +++++++- .../Exercise/Registration.cs | 8 +++--- 6 files changed, 87 insertions(+), 18 deletions(-) diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..5ae44da 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -6,24 +6,39 @@ namespace Execise.Tests [TestClass] public class UnitTests { - //test the two calls to library instance return same instance [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 library1 = Library.GetInstance(); + var library2 = Library.GetInstance(); + + Assert.AreEqual(library1, library2); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + var book = new Book("The Author", "The Book", 2001, 25); + + var library = Library.GetInstance(); + + var registrationResult = library.Register(book); + + Assert.AreNotEqual(-1, registrationResult); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + var customer = new Customer("Fred", "123 Place St."); + + var library = Library.GetInstance(); + + var registrationResult = library.Register(customer); + + Assert.AreNotEqual(-1, registrationResult); } //test that a book can be borrowed @@ -33,6 +48,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. + var book = new Book("Mr. Cat", "Supertown", 2009, 7); + + var borrowableBook = new BorrowableBook(book); + + borrowableBook.BorrowOne(); + + Assert.AreEqual(6, borrowableBook.AvailableAmount); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..2a5be27 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,28 @@ namespace Exercise { - public class BookBorrowable + /* + * Instructions unclear. Did this. + */ + public abstract class BookDecorator : Book { - + private Book _book; + + protected BookDecorator(Book book) + : base(book.Author, book.NameOrTitle, book.YearCreated, book.AvailableAmount) + { + _book = book; + } + } + + public class BorrowableBook : BookDecorator + { + public BorrowableBook(Book book) + : base(book) + { } + + public void BorrowOne() + { + AvailableAmount--; + } } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..e735640 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -1,6 +1,8 @@ -namespace Exercise +using System; + +namespace Exercise { - public abstract class Item : LibObject + public abstract class Item : LibObject, IRegistrable { public Item(int amount, int year) { @@ -8,11 +10,22 @@ public Item(int amount, int year) ObjType = ObjectType.Item; YearCreated = year; } + + public RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject + { + Id = ObjectId, + AvailableAmount = AvailableAmount, + Info = String.Format("{0} ({1})", NameOrTitle ?? ObjType.ToString(), YearCreated) + }; + } } 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; diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..369403d 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -3,16 +3,19 @@ public class Library { //implement Singleton to make sure only one library will exist + private static Library _library; - + private Library() {} //Implement Register method by utilizing RegistrationRepository (complete missing parts) - public int Register() + public int Register(IRegistrable registrableItem) { - // return RegistrationRepository.Register(); + return RegistrationRepository.Register(registrableItem); + } - //dummy, just to compile - return 0; + public static Library GetInstance() + { + return _library ?? (_library = new Library()); } } } diff --git a/DesignPatternsSolution/Exercise/Person.cs b/DesignPatternsSolution/Exercise/Person.cs index 2c2ed21..b49ab84 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, IRegistrable { public string Address { get; set; } public DateTime RegisteredAt { get; set; } @@ -19,5 +19,15 @@ public void SetId(int id) { ObjectId = id; } + + public RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject + { + Id = ObjectId, + AvailableAmount = AvailableAmount, + Info = String.Format("{0} of {1} ({2})", NameOrTitle, Address, RegisteredAt) + }; + } } } diff --git a/DesignPatternsSolution/Exercise/Registration.cs b/DesignPatternsSolution/Exercise/Registration.cs index f8c4eb0..ce56d7d 100644 --- a/DesignPatternsSolution/Exercise/Registration.cs +++ b/DesignPatternsSolution/Exercise/Registration.cs @@ -3,7 +3,7 @@ using System.Linq; namespace Exercise { - public interface IRegistarable + public interface IRegistrable { RegisteredObject GetRegistrationInfo(); } @@ -16,7 +16,7 @@ public static class RegistrationRepository private static int _nextId = 1; //With BRIDGE pattern, implement Register method so it will accept both a Person and an Item - public static int Register(IRegistarable registarable) + public static int Register(IRegistrable registarable) { //get info from an lib object var info = registarable.GetRegistrationInfo(); @@ -37,7 +37,7 @@ public static int Register(IRegistarable registarable) public static int DeleteAllRegisteredItems() { - var size = _registeredList.Count(); + var size = _registeredList.Count; _registeredList.RemoveRange(0, size); _nextId = 1; @@ -53,7 +53,7 @@ public class RegisteredObject public override string ToString() { - return Info + " " + "Available: " + AvailableAmount; + return string.Format("{0} Available: {1}", Info, AvailableAmount); } } }