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: 14 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 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("J.D. Salinger", "The Catcher in the Rye", 1951, 13);
var bookId = RegistrationRepository.Register(book);
Assert.IsFalse(bookId == -1);
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
var customer = new Customer("Jason Wood", "386 Broadway");
var customerId = RegistrationRepository.Register(customer);
Assert.IsFalse(customerId == -1);
}

//test that a book can be borrowed
Expand All @@ -32,7 +41,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.

const int bookAmount = 13;
var book = new Book("J.D. Salinger", "The Catcher in the Rye", 1951, bookAmount);
var bookBorrowable = new BookBorrowable(book, book.Author, book.NameOrTitle, book.YearCreated, book.AvailableAmount);
bookBorrowable.BorrowOne();
Assert.IsTrue(bookBorrowable._book.AvailableAmount == bookAmount - 1);
}
}
}
21 changes: 19 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
namespace Exercise
{
public class BookBorrowable
public class BookBorrowable : Book
{

public Book _book { get; private set; }

public BookBorrowable(Book book, string author, string title, int year, int amount) : base(author, title, year, amount)
{
_book = book;
}

public void BorrowOne()
{
if (_book.AvailableAmount > 0)
{
_book.AvailableAmount--;
}
else
{
_book.AvailableAmount = -1;
}
}
}
}
12 changes: 11 additions & 1 deletion DesignPatternsSolution/Exercise/LibObject.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
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 RegisteredObject GetRegistrationInfo()
{
return new RegisteredObject()
{
AvailableAmount = AvailableAmount,
Id = ObjectId,
Info = NameOrTitle
};
}
}

public enum ObjectType
Expand Down
8 changes: 7 additions & 1 deletion DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
public class Library
{
//implement Singleton to make sure only one library will exist
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()
Expand Down