From bbba48b9bd5e2fcc6ba181cb7c38b604b9638598 Mon Sep 17 00:00:00 2001 From: ecassell Date: Mon, 30 Jan 2017 08:50:49 -0600 Subject: [PATCH] ecassell DesignPatterns Training --- .../Execise.Tests/UnitTests.cs | 15 ++++++++++-- .../Exercise/BookBorrowable.cs | 24 +++++++++++++++++-- DesignPatternsSolution/Exercise/Item.cs | 9 +++++++ DesignPatternsSolution/Exercise/LibObject.cs | 11 +++++++-- DesignPatternsSolution/Exercise/Library.cs | 11 ++++++++- .../Exercise/Registration.cs | 7 ++++++ 6 files changed, 70 insertions(+), 7 deletions(-) diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..95a2dce 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -12,27 +12,38 @@ 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.IsTrue(Library.getInstance() == Library.getInstance()); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + var book = new Book("George Orwell", "1984", 1934, 5); + RegistrationRepository.Register(book); + Assert.IsFalse(book.ObjectId == -1); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + var customer = new Customer("George Orwell", "19 Eightyfour Lane"); + RegistrationRepository.Register(customer); + Assert.IsFalse(customer.ObjectId == -1); } //test that a book can be borrowed [TestMethod] public void CanBorrowBook() { - //create a borrowable book with available amount more than one. + //create a borrowable book with available amount more than one. + var borrowableBook = new BookBorrowable(new Book("Isaac Asimov", "Foundation", 1974, 4)); + int initialAmount = borrowableBook.BookToBeBorrowed.AvailableAmount; + borrowableBook.BorrowOne(); + int afterBorrowedAmount = borrowableBook.BookToBeBorrowed.AvailableAmount; //Run BorrowOne method of the BookBorrowable instance. Check that total amount was reduced by one. - + Assert.IsTrue(afterBorrowedAmount == (initialAmount - 1)); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..1ee0d76 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,27 @@ -namespace Exercise +using System; + +namespace Exercise { - public class BookBorrowable + public class BookBorrowable { + public Book BookToBeBorrowed { get; set; } + + public BookBorrowable(Book book) + { + BookToBeBorrowed = book; + } + + public void BorrowOne() + { + if (BookToBeBorrowed.AvailableAmount > 0) + { + BookToBeBorrowed.AvailableAmount--; + } + else + { + Console.WriteLine("No amount left to borrow for this book: " + BookToBeBorrowed.NameOrTitle); + } + } } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..dfb9f41 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -19,4 +19,13 @@ public Book(string author, string title, int year, int amount) : base(amount, ye } public string Author { get; set; } } + + public class Video : Item + { + public Video(string title, int year, int amount) + : base(amount, year) + { + NameOrTitle = title; + } + } } diff --git a/DesignPatternsSolution/Exercise/LibObject.cs b/DesignPatternsSolution/Exercise/LibObject.cs index cf1e2bb..1189eec 100644 --- a/DesignPatternsSolution/Exercise/LibObject.cs +++ b/DesignPatternsSolution/Exercise/LibObject.cs @@ -1,12 +1,19 @@ -namespace Exercise +using System.Runtime.Remoting.Messaging; + +namespace Exercise { - public abstract class LibObject + public abstract class LibObject : IRegistarable { 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 new RegisteredObject(this); + } } public enum ObjectType diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..f74c728 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -4,7 +4,16 @@ public class Library { //implement Singleton to make sure only one library will exist - + static Library instance = new Library(); + + private Library() + { + } + + public static Library getInstance() + { + return instance; + } //Implement Register method by utilizing RegistrationRepository (complete missing parts) public int Register() diff --git a/DesignPatternsSolution/Exercise/Registration.cs b/DesignPatternsSolution/Exercise/Registration.cs index f8c4eb0..bbb5358 100644 --- a/DesignPatternsSolution/Exercise/Registration.cs +++ b/DesignPatternsSolution/Exercise/Registration.cs @@ -51,6 +51,13 @@ public class RegisteredObject public int Id { get; set; } public int AvailableAmount { get; set; } + public RegisteredObject(LibObject libObj) + { + Id = libObj.ObjectId; + Info = libObj.NameOrTitle; + AvailableAmount = libObj.AvailableAmount; + } + public override string ToString() { return Info + " " + "Available: " + AvailableAmount;