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: 14 additions & 0 deletions 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
Library lib1 = Library.GetLibrary();
Library lib2 = Library.GetLibrary();
Assert.AreSame(lib1, lib2);
}

//test that an book was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void BookShouldRegister()
{
Library lib = Library.GetLibrary();
Book book = new Book("author", "title", 2017, 10);
Assert.AreNotEqual(lib.Register(book), -1);
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
Library lib = Library.GetLibrary();
Customer customer = new Customer("name", "addr");
Assert.AreNotEqual(lib.Register(customer), -1);
}

//test that a book can be borrowed
Expand All @@ -32,7 +41,12 @@ 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", 2017, 10);
int old = book.AvailableAmount;

BookBorrowable bookBorrowable = new BookBorrowable(null, new BorrowOne(book));

Assert.AreEqual(old - 1, book.AvailableAmount);
}
}
}
35 changes: 33 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
namespace Exercise
{
public class BookBorrowable
public abstract class ABookBorrowable
{

public virtual void BorrowOne() { }
protected BorrowOne BorrowOneProcedure;

public ABookBorrowable(BorrowOne borrowOneProcedure)
{
BorrowOneProcedure = borrowOneProcedure;
}
}

public class BookBorrowable : ABookBorrowable
{
private ABookBorrowable _bookBorrowable;

public BookBorrowable(ABookBorrowable bookBorrowable, BorrowOne borrowOne) : base(borrowOne)
{
_bookBorrowable = bookBorrowable;
}

public override void BorrowOne()
{
_bookBorrowable.BorrowOne();
}
}

public class BorrowOne
{
private LibObject item;

public BorrowOne(LibObject item)
{
item.AvailableAmount--;
}
}
}
6 changes: 5 additions & 1 deletion DesignPatternsSolution/Exercise/LibObject.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
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();
}
}

public enum ObjectType
Expand Down
12 changes: 9 additions & 3 deletions DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
public class Library
{
//implement Singleton to make sure only one library will exist

private static Library instance = new Library();

private Library() { }

public static Library GetLibrary()
{
return instance;
}

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

//dummy, just to compile
return 0;
return RegistrationRepository.Register(item);
}
}
}