From e30b7e2e40782eeeaac490d8da6ffd8672d3a7b1 Mon Sep 17 00:00:00 2001 From: Andrew Wold Date: Thu, 26 Jan 2017 11:39:28 -0600 Subject: [PATCH] first pass at exercise --- .../Execise.Tests/UnitTests.cs | 15 +++++++++++ .../Exercise/BookBorrowable.cs | 25 +++++++++++++++++-- DesignPatternsSolution/Exercise/Item.cs | 10 +++++++- DesignPatternsSolution/Exercise/Library.cs | 13 ++++++---- DesignPatternsSolution/Exercise/Person.cs | 11 +++++++- 5 files changed, 65 insertions(+), 9 deletions(-) diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..9dc6bee 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -12,18 +12,28 @@ 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 lib1 = Library.getInstance(); + Library lib2 = Library.getInstance(); + + Assert.AreSame(lib1, lib2); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + int retVal = RegistrationRepository.Register(new Book("Author", "title", 2012, 42)); + + Assert.AreNotEqual(-1, retVal); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + int retVal = RegistrationRepository.Register(new Customer("Andrew", "123 fake st")); + Assert.AreNotEqual(-1, retVal); } //test that a book can be borrowed @@ -32,6 +42,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 book = new Book("Author", "title", 2012, 42); + BookBorrowable borrowableBOok = new BookBorrowable(book, new BookDecorator()); + + borrowableBOok.BorrowOne(); + Assert.AreEqual(41, book.AvailableAmount); } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..3bd5b6d 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,28 @@ namespace Exercise { - public class BookBorrowable + public class BookBorrowable: Book { - + private Book item; + private BookDecorator decorator; + + public BookBorrowable(Book item, BookDecorator decorator) : + base(item.Author, item.NameOrTitle, item.YearCreated, item.AvailableAmount) + { + this.item = item; + this.decorator = decorator; + } + + public void BorrowOne() + { + decorator.BorrowOne(item); + } + } + + public class BookDecorator + { + public void BorrowOne(Item item) + { + item.AvailableAmount--; + } } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..d4d353f 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -8,15 +8,23 @@ 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) { NameOrTitle = title; Author = author; } + public string Author { get; set; } + + public RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject() { AvailableAmount = AvailableAmount, Info = NameOrTitle }; + } } } diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..b7ea4e7 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 libraryInstance = new Library(); - + private Library() { } + + public static Library getInstance() + { + return libraryInstance; + } //Implement Register method by utilizing RegistrationRepository (complete missing parts) public int Register() { - // return RegistrationRepository.Register(); - - //dummy, just to compile - return 0; + return RegistrationRepository.Register(new Book("andrew", "title",2017, 4)); } } } diff --git a/DesignPatternsSolution/Exercise/Person.cs b/DesignPatternsSolution/Exercise/Person.cs index 2c2ed21..e1cc4cb 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() + { + return new RegisteredObject() + { + AvailableAmount = 1, + Info = NameOrTitle + }; + } } }