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: 13 additions & 2 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,38 @@ 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
Assert.IsTrue(Library.getInstance() == Library.getInstance());
}

//test that an book was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void BookShouldRegister()
{
var book = new Book("George Orwell", "1984", 1934, 5);
RegistrationRepository.Register(book);
Assert.IsFalse(book.ObjectId == -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("George Orwell", "19 Eightyfour Lane");
RegistrationRepository.Register(customer);
Assert.IsFalse(customer.ObjectId == -1);
}

//test that a book can be borrowed
[TestMethod]
public void CanBorrowBook()
{
//create a borrowable book with available amount more than one.
//create a borrowable book with available amount more than one.
var borrowableBook = new BookBorrowable(new Book("Isaac Asimov", "Foundation", 1974, 4));
int initialAmount = borrowableBook.BookToBeBorrowed.AvailableAmount;
borrowableBook.BorrowOne();
int afterBorrowedAmount = borrowableBook.BookToBeBorrowed.AvailableAmount;
//Run BorrowOne method of the BookBorrowable instance. Check that total amount was reduced by one.

Assert.IsTrue(afterBorrowedAmount == (initialAmount - 1));
}
}
}
24 changes: 22 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
namespace Exercise
using System;

namespace Exercise
{
public class BookBorrowable
public class BookBorrowable
{
public Book BookToBeBorrowed { get; set; }

public BookBorrowable(Book book)
{
BookToBeBorrowed = book;
}

public void BorrowOne()
{
if (BookToBeBorrowed.AvailableAmount > 0)
{
BookToBeBorrowed.AvailableAmount--;
}
else
{
Console.WriteLine("No amount left to borrow for this book: " + BookToBeBorrowed.NameOrTitle);
}
}

}
}
9 changes: 9 additions & 0 deletions DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ public Book(string author, string title, int year, int amount) : base(amount, ye
}
public string Author { get; set; }
}

public class Video : Item
{
public Video(string title, int year, int amount)
: base(amount, year)
{
NameOrTitle = title;
}
}
}
11 changes: 9 additions & 2 deletions DesignPatternsSolution/Exercise/LibObject.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
namespace Exercise
using System.Runtime.Remoting.Messaging;

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(this);
}
}

public enum ObjectType
Expand Down
11 changes: 10 additions & 1 deletion DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ public class Library
{
//implement Singleton to make sure only one library will exist


static Library instance = new Library();

private Library()
{
}

public static Library getInstance()
{
return instance;
}

//Implement Register method by utilizing RegistrationRepository (complete missing parts)
public int Register()
Expand Down
7 changes: 7 additions & 0 deletions DesignPatternsSolution/Exercise/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ public class RegisteredObject
public int Id { get; set; }
public int AvailableAmount { get; set; }

public RegisteredObject(LibObject libObj)
{
Id = libObj.ObjectId;
Info = libObj.NameOrTitle;
AvailableAmount = libObj.AvailableAmount;
}

public override string ToString()
{
return Info + " " + "Available: " + AvailableAmount;
Expand Down