From b1631e9a1b8f5e8bbf8350524aaee5bf59a50bb3 Mon Sep 17 00:00:00 2001 From: CashCraigVarian Date: Fri, 27 Jan 2017 16:56:47 -0600 Subject: [PATCH] commit all changes --- .../DesignPatternsSolution.sln | 6 +-- ...ise.Tests.csproj => Exercise.Tests.csproj} | 2 +- .../Execise.Tests/Properties/AssemblyInfo.cs | 4 +- .../Execise.Tests/UnitTests.cs | 27 ++++++++--- .../Exercise/BookBorrowable.cs | 45 ++++++++++++++++++- DesignPatternsSolution/Exercise/Item.cs | 13 +++++- DesignPatternsSolution/Exercise/Library.cs | 15 ++++--- DesignPatternsSolution/Exercise/Person.cs | 11 ++++- .../Exercise/Registration.cs | 23 ++++------ 9 files changed, 108 insertions(+), 38 deletions(-) rename DesignPatternsSolution/Execise.Tests/{Execise.Tests.csproj => Exercise.Tests.csproj} (98%) diff --git a/DesignPatternsSolution/DesignPatternsSolution.sln b/DesignPatternsSolution/DesignPatternsSolution.sln index e5b257d..83033f8 100644 --- a/DesignPatternsSolution/DesignPatternsSolution.sln +++ b/DesignPatternsSolution/DesignPatternsSolution.sln @@ -1,11 +1,11 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.24720.0 +# Visual Studio 2013 +VisualStudioVersion = 12.0.31101.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exercise", "Exercise\Exercise.csproj", "{EA98A53A-F8CD-4539-9E57-1FEF2D64F13A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Execise.Tests", "Execise.Tests\Execise.Tests.csproj", "{464FBD03-CA8B-4926-8F69-66726819DA02}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exercise.Tests", "Execise.Tests\Exercise.Tests.csproj", "{464FBD03-CA8B-4926-8F69-66726819DA02}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documents", "Documents", "{8C88EFFA-EC68-42ED-A482-A7B8964538E8}" ProjectSection(SolutionItems) = preProject diff --git a/DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj b/DesignPatternsSolution/Execise.Tests/Exercise.Tests.csproj similarity index 98% rename from DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj rename to DesignPatternsSolution/Execise.Tests/Exercise.Tests.csproj index 317ab1f..ea9919a 100644 --- a/DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj +++ b/DesignPatternsSolution/Execise.Tests/Exercise.Tests.csproj @@ -6,7 +6,7 @@ {464FBD03-CA8B-4926-8F69-66726819DA02} Library Properties - Execise.Tests + Exercise.Tests Execise.Tests v3.5 512 diff --git a/DesignPatternsSolution/Execise.Tests/Properties/AssemblyInfo.cs b/DesignPatternsSolution/Execise.Tests/Properties/AssemblyInfo.cs index b8d1030..cc8baf1 100644 --- a/DesignPatternsSolution/Execise.Tests/Properties/AssemblyInfo.cs +++ b/DesignPatternsSolution/Execise.Tests/Properties/AssemblyInfo.cs @@ -5,11 +5,11 @@ // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. -[assembly: AssemblyTitle("Execise.Tests")] +[assembly: AssemblyTitle("Exercise.Tests")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Execise.Tests")] +[assembly: AssemblyProduct("Exercise.Tests")] [assembly: AssemblyCopyright("Copyright © 2016")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..1bab1db 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -1,7 +1,7 @@ using Exercise; using Microsoft.VisualStudio.TestTools.UnitTesting; -namespace Execise.Tests +namespace Exercise.Tests { [TestClass] public class UnitTests @@ -10,29 +10,44 @@ 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 libraryInstance1 = Library.GetInstance(); + Library libraryInstance2 = Library.GetInstance(); + + Assert.AreEqual(libraryInstance1, libraryInstance2); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + Book book = new Book("Cash Craig", "Fake Book", 1991, 7); + int result = RegistrationRepository.Register(book); + Assert.AreNotEqual(-1, result); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + Customer customer = new Customer("Cash Craig", "123 Fake Street"); + int result = RegistrationRepository.Register(customer); + Assert.AreNotEqual(-1, result); } //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. - + Book myBook = new Book("Cash Craig", "Fake Book", 1991, 7); + + int oldCount = myBook.AvailableAmount; + + Borrowable bookBorrowable = new Borrowable(null, new Borrow(myBook)); + bookBorrowable.BorrowOne(); + + int newCount = myBook.AvailableAmount; + + Assert.AreEqual(oldCount - 1, newCount); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..412c90f 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,48 @@ namespace Exercise { - public class BookBorrowable + public abstract class ABorrowable { - + public virtual void BorrowOne() { } + protected Borrow Borrow; + + protected ABorrowable(Borrow borrow) + { + this.Borrow = borrow; + } + } + + public class Borrowable : ABorrowable + { + private readonly ABorrowable _borrowable; + + public Borrowable(ABorrowable borrowable, Borrow borrow) : base(borrow) + { + _borrowable = borrowable; + } + + public override void BorrowOne() + { + if (_borrowable != null) + { + _borrowable.BorrowOne(); + } + + Borrow.BorrowOne(); + } + } + + public class Borrow + { + private readonly LibObject _libObject; + + public Borrow(LibObject libObject) + { + this._libObject = libObject; + } + + public void BorrowOne() + { + this._libObject.AvailableAmount--; + } } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..2184cde 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) + protected Item(int amount, int year) { AvailableAmount = amount; ObjType = ObjectType.Item; @@ -10,7 +10,7 @@ public Item(int amount, int year) } } - public class Book : Item + public class Book : Item, IRegistrable { public Book(string author, string title, int year, int amount) : base(amount, year) { @@ -18,5 +18,14 @@ public Book(string author, string title, int year, int amount) : base(amount, ye Author = author; } public string Author { get; set; } + + public RegisteredObject GetRegistrationInfo() + { + RegisteredObject resgisteredObject = new RegisteredObject(); + resgisteredObject.Info = this.NameOrTitle; + resgisteredObject.AvailableAmount = this.AvailableAmount; + + return resgisteredObject; + } } } diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..dc84b01 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -2,16 +2,19 @@ { public class Library { - //implement Singleton to make sure only one library will exist + private static readonly Library Instance = new Library(); - + private Library() + { + } - //Implement Register method by utilizing RegistrationRepository (complete missing parts) - public int Register() + public static Library GetInstance() { - // return RegistrationRepository.Register(); + return Instance; + } - //dummy, just to compile + public int Register() + { return 0; } } diff --git a/DesignPatternsSolution/Exercise/Person.cs b/DesignPatternsSolution/Exercise/Person.cs index 2c2ed21..06e2cad 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,14 @@ public void SetId(int id) { ObjectId = id; } + + public RegisteredObject GetRegistrationInfo() + { + RegisteredObject registeredObject = new RegisteredObject(); + registeredObject.Info = this.NameOrTitle; + registeredObject.AvailableAmount = this.AvailableAmount; + + return registeredObject; + } } } diff --git a/DesignPatternsSolution/Exercise/Registration.cs b/DesignPatternsSolution/Exercise/Registration.cs index f8c4eb0..d21240e 100644 --- a/DesignPatternsSolution/Exercise/Registration.cs +++ b/DesignPatternsSolution/Exercise/Registration.cs @@ -3,42 +3,35 @@ using System.Linq; namespace Exercise { - public interface IRegistarable + public interface IRegistrable { RegisteredObject GetRegistrationInfo(); } public static class RegistrationRepository { - //registered objects list - private static List _registeredList = new List(); + private static readonly List RegisteredList = new List(); 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 registrable) { - //get info from an lib object - var info = registarable.GetRegistrationInfo(); + var info = registrable.GetRegistrationInfo(); if (info == null) return -1; - //get new id for for the registered object info.Id = _nextId; - //add to registration repository - _registeredList.Add(info); + RegisteredList.Add(info); - //store next available id - _nextId = _registeredList.Count + 1; + _nextId = RegisteredList.Count + 1; - //return success return info.Id; } public static int DeleteAllRegisteredItems() { - var size = _registeredList.Count(); - _registeredList.RemoveRange(0, size); + var size = RegisteredList.Count(); + RegisteredList.RemoveRange(0, size); _nextId = 1; return size;