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
17 changes: 15 additions & 2 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public class UnitTests
[TestMethod]
public void LibraryShouldBeSingleton()
{
Library lib1 = Library.getLib();
Library lib2 = Library.getLib();
Assert.ReferenceEquals(lib1, lib2);
//check that when you create a Library instance second time,
//you get exactly the same instance as for a very first time
}
Expand All @@ -18,21 +21,31 @@ public void LibraryShouldBeSingleton()
[TestMethod]
public void BookShouldRegister()
{
Book book = new Book("Gary", "Program coding", 2017, 1, new Exercise.RegistrationApi());
Assert.AreNotEqual(-1, RegistrationRepository.Register(book));
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
Customer customer = new Customer("Gagandeep", "Winnipeg", new Exercise.RegistrationApi());
Assert.AreNotEqual(-1, RegistrationRepository.Register(customer));
}

//test that a book can be borrowed
[TestMethod]
public void CanBorrowBook()
{
Book book1 = new Book("Gary2", "Program coding", 2017, 200, new Exercise.RegistrationApi());
BookBorrowable bookborrow = new BookBorrowable(book1, new Decorator());

bookborrow.Borrow();
Assert.AreEqual(199, book1.AvailableAmount);

//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.
}

}
}
}
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 book;
private Decorator decorator;

public BookBorrowable(Book book, Decorator decorator)
: base(book.Author, book.NameOrTitle, book.YearCreated, book.AvailableAmount, new Exercise.RegistrationApi())
{
this.book = book;
this.decorator = decorator;
}

public void Borrow()
{
decorator.Borrow(book);
}
}

public class Decorator
{
public void Borrow(Book book)
{
book.AvailableAmount--;
}
}
}
8 changes: 6 additions & 2 deletions DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
{
public abstract class Item : LibObject
{
public Item(int amount, int year)
//public Item(int amount, int year)
public Item(int amount, int year, IRegistrationApi registrationApi)
: base(registrationApi)
{
AvailableAmount = amount;
ObjType = ObjectType.Item;
Expand All @@ -12,7 +14,9 @@ public Item(int amount, int year)

public class Book : Item
{
public Book(string author, string title, int year, int amount) : base(amount, year)
//public Book(string author, string title, int year, int amount) : base(amount, year)
public Book(string author, string title, int year, int amount, IRegistrationApi registrationApi)
: base(amount, year, registrationApi)
{
NameOrTitle = title;
Author = author;
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
{
private IRegistrationApi registrationApi;
protected LibObject(IRegistrationApi registrationApi)
{
this.registrationApi = registrationApi;
}
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 registrationApi.GetRegistrationInfo(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,14 +4,23 @@ public class Library
{
//implement Singleton to make sure only one library will exist



static Library lib = new Library();

private Library() { }
//Implement Register method by utilizing RegistrationRepository (complete missing parts)

public static Library getLib()
{
return lib;
}
public int Register()
{
// return RegistrationRepository.Register();

//dummy, just to compile


return 0;
}
}
Expand Down
4 changes: 3 additions & 1 deletion DesignPatternsSolution/Exercise/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public class Customer : LibObject
public string Address { get; set; }
public DateTime RegisteredAt { get; set; }

public Customer(string name, string addr)
//public Customer(string name, string addr)
public Customer(string name, string addr, IRegistrationApi registrationApi)
: base(registrationApi)
{
NameOrTitle = name;
Address = addr;
Expand Down
25 changes: 25 additions & 0 deletions DesignPatternsSolution/Exercise/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,31 @@
using System.Linq;
namespace Exercise
{


public interface IRegistrationApi
{
RegisteredObject GetRegistrationInfo(LibObject libObject);
}

public class RegistrationApi : IRegistrationApi
{
public RegistrationApi()
{
}

public RegisteredObject GetRegistrationInfo(LibObject libObject)
{
return new RegisteredObject
{
Info = libObject.NameOrTitle,
AvailableAmount = libObject.AvailableAmount,
Id = libObject.ObjectId
};
}
}


public interface IRegistarable
{
RegisteredObject GetRegistrationInfo();
Expand Down