From 9f4b6418cff700be0d8292e5d4c93106c12eff92 Mon Sep 17 00:00:00 2001 From: Sasa Ivetic Date: Fri, 27 Jan 2017 10:05:51 -0600 Subject: [PATCH] Training exercise --- .../Execise.Tests/Execise.Tests.csproj | 2 +- .../Execise.Tests/UnitTests.cs | 15 ++++++-- .../Exercise/BookBorrowable.cs | 38 ++++++++++++++++++- .../Exercise/Exercise.csproj | 3 +- DesignPatternsSolution/Exercise/Item.cs | 9 ++++- DesignPatternsSolution/Exercise/LibObject.cs | 14 ++++++- DesignPatternsSolution/Exercise/Library.cs | 17 ++++++--- DesignPatternsSolution/Exercise/Person.cs | 3 +- .../Exercise/RegistrationApi.cs | 30 +++++++++++++++ 9 files changed, 114 insertions(+), 17 deletions(-) create mode 100644 DesignPatternsSolution/Exercise/RegistrationApi.cs diff --git a/DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj b/DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj index 71e68ac..252ccb3 100644 --- a/DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj +++ b/DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj @@ -8,7 +8,7 @@ Properties Execise.Tests Execise.Tests - v4.5.2 + v4.5 512 {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 10.0 diff --git a/DesignPatternsSolution/Execise.Tests/UnitTests.cs b/DesignPatternsSolution/Execise.Tests/UnitTests.cs index 0a3b80e..1570aec 100644 --- a/DesignPatternsSolution/Execise.Tests/UnitTests.cs +++ b/DesignPatternsSolution/Execise.Tests/UnitTests.cs @@ -10,20 +10,25 @@ 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 lib1 = Library.GetInstance(); + Library lib2 = Library.GetInstance(); + Assert.ReferenceEquals(lib1, lib2); } //test that an book was registered successfully by checking the returned Id value is not -1 [TestMethod] public void BookShouldRegister() { + Book book = new Book("John Smith", "Adventures in Coding", 2017, 1, new Exercise.RegistrationApi()); + Assert.IsTrue(RegistrationRepository.Register(book) > -1); } //test that an customer was registered successfully by checking the returned Id value is not -1 [TestMethod] public void CustomerShouldRegister() { + Customer customer = new Customer("Jane Doe", "386 Broadway", new Exercise.RegistrationApi()); + Assert.IsTrue(RegistrationRepository.Register(customer) > -1); } //test that a book can be borrowed @@ -32,7 +37,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. - + int totalAmount = 10; + Book book = new Book("John Smith", "Adventures in Coding", 2017, totalAmount, new Exercise.RegistrationApi()); + BookBorrowable borrowable = new BookBorrowable(null, book); + borrowable.BorrowOne(); + Assert.IsTrue(book.AvailableAmount == (totalAmount - 1)); } } } diff --git a/DesignPatternsSolution/Exercise/BookBorrowable.cs b/DesignPatternsSolution/Exercise/BookBorrowable.cs index 21bd9cf..d84f060 100644 --- a/DesignPatternsSolution/Exercise/BookBorrowable.cs +++ b/DesignPatternsSolution/Exercise/BookBorrowable.cs @@ -1,7 +1,41 @@ namespace Exercise { - public class BookBorrowable + public abstract class Borrowable { - + public virtual void BorrowOne() {} + + protected LibObject LibObject; + public Borrowable(LibObject libObject) + { + LibObject = libObject; + } + } + + public class BookBorrowable : Borrowable + { + private Borrowable borrowable; + + public BookBorrowable(Borrowable borrowable, Book book) + : base(book) + { + this.borrowable = borrowable; + } + + public override void BorrowOne() + { + if (borrowable != null) + { + borrowable.BorrowOne(); + } + + if (LibObject.AvailableAmount > 0) + { + LibObject.AvailableAmount--; + } + else + { + throw new System.Exception("No books left to borrow."); + } + } } } diff --git a/DesignPatternsSolution/Exercise/Exercise.csproj b/DesignPatternsSolution/Exercise/Exercise.csproj index d2cc582..f3315b4 100644 --- a/DesignPatternsSolution/Exercise/Exercise.csproj +++ b/DesignPatternsSolution/Exercise/Exercise.csproj @@ -9,7 +9,7 @@ Properties Exercise Exercise - v4.5.2 + v4.5 512 true @@ -53,6 +53,7 @@ +