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
15 changes: 15 additions & 0 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ 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.AreSame(lib1, lib2);
}

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

Assert.AreNotEqual(-1, retVal);
}

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

//test that a book can be borrowed
Expand All @@ -32,6 +42,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.
Book book = new Book("Author", "title", 2012, 42);
BookBorrowable borrowableBOok = new BookBorrowable(book, new BookDecorator());

borrowableBOok.BorrowOne();
Assert.AreEqual(41, book.AvailableAmount);

}
}
Expand Down
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 item;
private BookDecorator decorator;

public BookBorrowable(Book item, BookDecorator decorator) :
base(item.Author, item.NameOrTitle, item.YearCreated, item.AvailableAmount)
{
this.item = item;
this.decorator = decorator;
}

public void BorrowOne()
{
decorator.BorrowOne(item);
}
}

public class BookDecorator
{
public void BorrowOne(Item item)
{
item.AvailableAmount--;
}
}
}
10 changes: 9 additions & 1 deletion DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ public Item(int amount, int year)
ObjType = ObjectType.Item;
YearCreated = 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 };
}
}
}
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 libraryInstance = new Library();


private Library() { }

public static Library getInstance()
{
return libraryInstance;
}

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

//dummy, just to compile
return 0;
return RegistrationRepository.Register(new Book("andrew", "title",2017, 4));
}
}
}
11 changes: 10 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, IRegistarable
{
public string Address { get; set; }
public DateTime RegisteredAt { get; set; }
Expand All @@ -19,5 +19,14 @@ public void SetId(int id)
{
ObjectId = id;
}

public RegisteredObject GetRegistrationInfo()
{
return new RegisteredObject()
{
AvailableAmount = 1,
Info = NameOrTitle
};
}
}
}