Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,39 @@ namespace Execise.Tests
[TestClass]
public class UnitTests
{
//test the two calls to library instance return same instance
[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 library1 = Library.GetInstance();
var library2 = Library.GetInstance();

Assert.AreEqual(library1, library2);
}

//test that an book was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void BookShouldRegister()
{
var book = new Book("The Author", "The Book", 2001, 25);

var library = Library.GetInstance();

var registrationResult = library.Register(book);

Assert.AreNotEqual(-1, registrationResult);
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
var customer = new Customer("Fred", "123 Place St.");

var library = Library.GetInstance();

var registrationResult = library.Register(customer);

Assert.AreNotEqual(-1, registrationResult);
}

//test that a book can be borrowed
Expand All @@ -33,6 +48,13 @@ 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.

var book = new Book("Mr. Cat", "Supertown", 2009, 7);

var borrowableBook = new BorrowableBook(book);

borrowableBook.BorrowOne();

Assert.AreEqual(6, borrowableBook.AvailableAmount);
}
}
}
25 changes: 23 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
namespace Exercise
{
public class BookBorrowable
/*
* Instructions unclear. Did this.
*/
public abstract class BookDecorator : Book
{

private Book _book;

protected BookDecorator(Book book)
: base(book.Author, book.NameOrTitle, book.YearCreated, book.AvailableAmount)
{
_book = book;
}
}

public class BorrowableBook : BookDecorator
{
public BorrowableBook(Book book)
: base(book)
{ }

public void BorrowOne()
{
AvailableAmount--;
}
}
}
19 changes: 16 additions & 3 deletions DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
namespace Exercise
using System;

namespace Exercise
{
public abstract class Item : LibObject
public abstract class Item : LibObject, IRegistrable
{
public Item(int amount, int year)
{
AvailableAmount = amount;
ObjType = ObjectType.Item;
YearCreated = year;
}

public RegisteredObject GetRegistrationInfo()
{
return new RegisteredObject
{
Id = ObjectId,
AvailableAmount = AvailableAmount,
Info = String.Format("{0} ({1})", NameOrTitle ?? ObjType.ToString(), YearCreated)
};
}
}

public class Book : Item
{
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;
Expand Down
13 changes: 8 additions & 5 deletions DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
public class Library
{
//implement Singleton to make sure only one library will exist
private static Library _library;


private Library() {}

//Implement Register method by utilizing RegistrationRepository (complete missing parts)
public int Register()
public int Register(IRegistrable registrableItem)
{
// return RegistrationRepository.Register();
return RegistrationRepository.Register(registrableItem);
}

//dummy, just to compile
return 0;
public static Library GetInstance()
{
return _library ?? (_library = new Library());
}
}
}
12 changes: 11 additions & 1 deletion DesignPatternsSolution/Exercise/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Exercise
{
public class Customer : LibObject
public class Customer : LibObject, IRegistrable
{
public string Address { get; set; }
public DateTime RegisteredAt { get; set; }
Expand All @@ -19,5 +19,15 @@ public void SetId(int id)
{
ObjectId = id;
}

public RegisteredObject GetRegistrationInfo()
{
return new RegisteredObject
{
Id = ObjectId,
AvailableAmount = AvailableAmount,
Info = String.Format("{0} of {1} ({2})", NameOrTitle, Address, RegisteredAt)
};
}
}
}
8 changes: 4 additions & 4 deletions DesignPatternsSolution/Exercise/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
namespace Exercise
{
public interface IRegistarable
public interface IRegistrable
{
RegisteredObject GetRegistrationInfo();
}
Expand All @@ -16,7 +16,7 @@ public static class RegistrationRepository
private static int _nextId = 1;

//With BRIDGE pattern, implement Register method so it will accept both a Person and an Item
public static int Register(IRegistarable registarable)
public static int Register(IRegistrable registarable)
{
//get info from an lib object
var info = registarable.GetRegistrationInfo();
Expand All @@ -37,7 +37,7 @@ public static int Register(IRegistarable registarable)

public static int DeleteAllRegisteredItems()
{
var size = _registeredList.Count();
var size = _registeredList.Count;
_registeredList.RemoveRange(0, size);
_nextId = 1;

Expand All @@ -53,7 +53,7 @@ public class RegisteredObject

public override string ToString()
{
return Info + " " + "Available: " + AvailableAmount;
return string.Format("{0} Available: {1}", Info, AvailableAmount);
}
}
}