From a0c30f20a31729fc8ef7a98cdd8d8a6e4009d8ff Mon Sep 17 00:00:00 2001 From: VarianChrisMacKinnon Date: Fri, 27 Jan 2017 15:11:26 -0600 Subject: [PATCH] Training Completed --- .../Execise.Tests/UnitTests.cs | 27 +++++++++++--- .../Exercise/BookBorrowable.cs | 19 ++++++++-- DesignPatternsSolution/Exercise/Item.cs | 36 ++++++++++++++++++- DesignPatternsSolution/Exercise/LibObject.cs | 4 ++- DesignPatternsSolution/Exercise/Library.cs | 16 +++++---- DesignPatternsSolution/Exercise/Person.cs | 10 ++++++ 6 files changed, 96 insertions(+), 16 deletions(-) diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..11adee8 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -10,29 +10,46 @@ 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 instance1 = Library.getInstance(); + Library instance2 = Library.getInstance(); + + Assert.AreSame(instance1, instance2); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + Library instance = Library.getInstance(); + Book myBook = new Book("Orwell, George", "Nineteen Eighty-Four", 1949, 5); + + int idValue = instance.Register(myBook); + + Assert.IsTrue(idValue != -1); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + Library instance = Library.getInstance(); + Customer customer = new Customer("Christopher MacKinnon", "386 Broadway"); + + int idValue = instance.Register(customer); + + Assert.IsTrue( idValue != -1); } //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. - + BookBorrowable borrowableBook = new BookBorrowable(new Book("Orwell, George", "Nineteen Eighty-Four", 1949, 6)); + int originalAmount = borrowableBook.AmountToBorrow(); + + borrowableBook.BorrowOne(); + + Assert.IsTrue(borrowableBook.AmountToBorrow() < originalAmount); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..0713c94 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,22 @@ namespace Exercise { - public class BookBorrowable + public class BookBorrowable { - + private Book book; + + public BookBorrowable(Book book) + { + this.book = book; + } + + public void BorrowOne() + { + book.AvailableAmount--; + } + + public int AmountToBorrow() + { + return book.AvailableAmount; + } } } diff --git a/DesignPatternsSolution/Exercise/Item.cs b/DesignPatternsSolution/Exercise/Item.cs index 92c21e1..235687b 100644 --- a/DesignPatternsSolution/Exercise/Item.cs +++ b/DesignPatternsSolution/Exercise/Item.cs @@ -1,4 +1,6 @@ -namespace Exercise +using System.Net; + +namespace Exercise { public abstract class Item : LibObject { @@ -8,6 +10,8 @@ public Item(int amount, int year) ObjType = ObjectType.Item; YearCreated = year; } + + } public class Book : Item @@ -18,5 +22,35 @@ public Book(string author, string title, int year, int amount) : base(amount, ye Author = author; } public string Author { get; set; } + + public override RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject() + { + AvailableAmount = AvailableAmount, + Id = -1, + Info = NameOrTitle + }; + } + } + + public class Video : Item + { + public Video(string director, string title, int year, int amount) : base(amount, year) + { + NameOrTitle = title; + Director = director; + } + public string Director { get; set; } + + public override RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject() + { + AvailableAmount = AvailableAmount, + Id = -1, + Info = NameOrTitle + }; + } } } diff --git a/DesignPatternsSolution/Exercise/LibObject.cs b/DesignPatternsSolution/Exercise/LibObject.cs index cf1e2bb..8b170f3 100644 --- a/DesignPatternsSolution/Exercise/LibObject.cs +++ b/DesignPatternsSolution/Exercise/LibObject.cs @@ -1,12 +1,14 @@ 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 abstract RegisteredObject GetRegistrationInfo(); } public enum ObjectType diff --git a/DesignPatternsSolution/Exercise/Library.cs b/DesignPatternsSolution/Exercise/Library.cs index 1d9dde4..bd4bd95 100644 --- a/DesignPatternsSolution/Exercise/Library.cs +++ b/DesignPatternsSolution/Exercise/Library.cs @@ -1,18 +1,20 @@ -namespace Exercise +using System.Security.Policy; + +namespace Exercise { 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() + public int Register(IRegistarable registerableObject) { - // return RegistrationRepository.Register(); - - //dummy, just to compile - return 0; + return RegistrationRepository.Register(registerableObject); } } } diff --git a/DesignPatternsSolution/Exercise/Person.cs b/DesignPatternsSolution/Exercise/Person.cs index 2c2ed21..7264ee8 100644 --- a/DesignPatternsSolution/Exercise/Person.cs +++ b/DesignPatternsSolution/Exercise/Person.cs @@ -19,5 +19,15 @@ public void SetId(int id) { ObjectId = id; } + + public override RegisteredObject GetRegistrationInfo() + { + return new RegisteredObject() + { + AvailableAmount = 1, + Id = -1, + Info = NameOrTitle + }; + } } }