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
14 changes: 13 additions & 1 deletion DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,27 @@ 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 first = Library.getLibrary();
var second = Library.getLibrary();

Assert.AreEqual(first, second);
}

//test that an book was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void BookShouldRegister()
{
int x = RegistrationRepository.Register(new Book("Bakul", "No title", 2015, 19));

Assert.AreNotEqual(-1, x);
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
int x = RegistrationRepository.Register(new Customer("Bakul", "320 street not available"));
Assert.AreNotEqual(-1, x);
}

//test that a book can be borrowed
Expand All @@ -32,7 +41,10 @@ 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 b = new Book("Bakul", "No title", 2015, 200);
BookBorrowable bb = new BookBorrowable(b, new Decorator());
bb.Borrow();
Assert.AreNotEqual(200, b.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
public class BookBorrowable : Book
{

private Book thing;
private Decorator decorator;

public BookBorrowable(Book book, Decorator decorator) :
base(book.Author, book.NameOrTitle, book.YearCreated, book.AvailableAmount)
{
thing = book;
this.decorator = decorator;
}

public void Borrow()
{
decorator.Borrow(thing);
}
}

public class Decorator
{
public void Borrow(Book x)
{
x.AvailableAmount--;
}
}
}
6 changes: 5 additions & 1 deletion DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ public Item(int amount, int 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 };
}
}
}
6 changes: 5 additions & 1 deletion DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ public class Library
{
//implement Singleton to make sure only one library will exist


static Library library = new Library();

private Library() { }

public static Library getLibrary() { return library; }

//Implement Register method by utilizing RegistrationRepository (complete missing parts)
public int Register()
Expand Down
10 changes: 8 additions & 2 deletions 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 , IRegistarable
{
public string Address { get; set; }
public DateTime RegisteredAt { get; set; }
Expand All @@ -14,7 +14,13 @@ public Customer(string name, string addr)
RegisteredAt = DateTime.Now;
ObjType = ObjectType.Person;
}

public RegisteredObject GetRegistrationInfo()
{
return new RegisteredObject()
{
Info = NameOrTitle
};
}
public void SetId(int id)
{
ObjectId = id;
Expand Down