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
20 changes: 15 additions & 5 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,39 @@ public class UnitTests
[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 l1 = Library.GetInstance();
var l2 = Library.GetInstance();

Assert.AreSame(l1, l2);
}

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

Assert.AreNotEqual<int>(-1, value);
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
int value = RegistrationRepository.Register(new Customer("Customer Name", "Address"));

Assert.AreNotEqual<int>(-1, value);
}

//test that a book can be borrowed
[TestMethod]
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("Author", "Title", 1997, 5);
var borrowable = new BookBorrowable(book);

borrowable.BorrowOne();
Assert.AreEqual<int>(4, book.AvailableAmount);
}
}
}
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
{

private Book Book { get; set; }

public BookBorrowable(Book bookToBorrow) : base(bookToBorrow.Author, bookToBorrow.NameOrTitle, bookToBorrow.YearCreated, bookToBorrow.YearCreated)
{
this.Book = bookToBorrow;
}

public bool BorrowOne()
{
if (this.Book.AvailableAmount > 0)
{
this.Book.AvailableAmount--;
return true;
}
else
return false;

}
}
}
4 changes: 3 additions & 1 deletion DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Exercise
using System;

namespace Exercise
{
public abstract class Item : LibObject
{
Expand Down
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 = this.AvailableAmount,
Id = this.ObjectId,
Info = this.NameOrTitle
};
}
}

public enum ObjectType
Expand Down
5 changes: 3 additions & 2 deletions DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
{
public class Library
{
//implement Singleton to make sure only one library will exist
private static Library Instance { get; set; }


private Library() { Instance = new Library(); }
public static Library GetInstance() { return Library.Instance; }

//Implement Register method by utilizing RegistrationRepository (complete missing parts)
public int Register()
Expand Down