From dacdbf06a9b139fc1ad73364ef8c45b1c7b54c20 Mon Sep 17 00:00:00 2001 From: TaylorVarian Date: Fri, 27 Jan 2017 16:24:34 -0600 Subject: [PATCH] Done --- .../Execise.Tests/UnitTests.cs | 21 +++++++++++++++---- .../Exercise/BookBorrowable.cs | 21 +++++++++++++++++-- DesignPatternsSolution/Exercise/Item.cs | 14 +++++++++++-- DesignPatternsSolution/Exercise/Library.cs | 21 +++++++++++++++---- DesignPatternsSolution/Exercise/Person.cs | 9 +++++++- .../Exercise/Registration.cs | 1 + 6 files changed, 74 insertions(+), 13 deletions(-) diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..f14aa26 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -10,20 +10,29 @@ 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 + var lib = Library.getInstance(); + var rib = Library.getInstance(); + + Assert.AreEqual(lib, rib); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + Book b = new Book("Guy", "title", 1990, 100); + var lib = Library.getInstance(); + + Assert.AreNotEqual(lib.Register(b), -1); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + Customer c = new Customer("Guy", "777 Broadway Ave"); + var lib = Library.getInstance(); + Assert.AreNotEqual(lib.Register(c), -1); } //test that a book can be borrowed @@ -32,7 +41,11 @@ 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 b = new Book("Guy", "title", 1990, 100); + BookBorrowable bb = new BookBorrowable(b, 10); + var x = bb.borrowOne(); + Assert.AreEqual(bb.numberOfBooksLeft(), 9); + + } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..b83d377 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,24 @@ namespace Exercise { - public class BookBorrowable + public class BookBorrowable { - + private Book book; + private int numberOfBooks = 0; + public BookBorrowable(Book b, int amount) + { + book = b; + numberOfBooks = amount; + } + + public Book borrowOne() + { + + numberOfBooks--; + return book; + } + public int numberOfBooksLeft() + { + return numberOfBooks; + } } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..40fbb60 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -8,15 +8,25 @@ public Item(int amount, int year) ObjType = ObjectType.Item; YearCreated = year; } + } - public class Book : Item + public class Book : Item, IRegistarable { - 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; } + + public RegisteredObject GetRegistrationInfo() + { + RegisteredObject x = new RegisteredObject(); + x.obj = this; + return x; + } + } } diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..39dc64d 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -2,17 +2,30 @@ { public class Library { + private BookBorrowable bbook; + private int numberOfBooks = 0; + + static Library instance = new Library(); //implement Singleton to make sure only one library will exist + private Library() { } + + + public static Library getInstance() + { + return instance; + } - //Implement Register method by utilizing RegistrationRepository (complete missing parts) - public int Register() + public int Register(IRegistarable obj) { - // return RegistrationRepository.Register(); + + + return RegistrationRepository.Register(obj); //dummy, just to compile - return 0; + //return 0; } + } } diff --git a/DesignPatternsSolution/Exercise/Person.cs b/DesignPatternsSolution/Exercise/Person.cs index 2c2ed21..34df256 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,12 @@ public void SetId(int id) { ObjectId = id; } + + public RegisteredObject GetRegistrationInfo() + { + RegisteredObject x = new RegisteredObject(); + x.obj = this; + return x; + } } } diff --git a/DesignPatternsSolution/Exercise/Registration.cs b/DesignPatternsSolution/Exercise/Registration.cs index f8c4eb0..84737d7 100644 --- a/DesignPatternsSolution/Exercise/Registration.cs +++ b/DesignPatternsSolution/Exercise/Registration.cs @@ -50,6 +50,7 @@ public class RegisteredObject public string Info { get; set; } public int Id { get; set; } public int AvailableAmount { get; set; } + public LibObject obj { get; set; } public override string ToString() {