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
27 changes: 22 additions & 5 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,46 @@ 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
Library instance1 = Library.getInstance();
Library instance2 = Library.getInstance();

Assert.AreSame(instance1, instance2);
}

//test that an book was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void BookShouldRegister()
{
Library instance = Library.getInstance();
Book myBook = new Book("Orwell, George", "Nineteen Eighty-Four", 1949, 5);

int idValue = instance.Register(myBook);

Assert.IsTrue(idValue != -1);
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
Library instance = Library.getInstance();
Customer customer = new Customer("Christopher MacKinnon", "386 Broadway");

int idValue = instance.Register(customer);

Assert.IsTrue( idValue != -1);
}

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

BookBorrowable borrowableBook = new BookBorrowable(new Book("Orwell, George", "Nineteen Eighty-Four", 1949, 6));
int originalAmount = borrowableBook.AmountToBorrow();

borrowableBook.BorrowOne();

Assert.IsTrue(borrowableBook.AmountToBorrow() < originalAmount);
}
}
}
19 changes: 17 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
namespace Exercise
{
public class BookBorrowable
public class BookBorrowable
{

private Book book;

public BookBorrowable(Book book)
{
this.book = book;
}

public void BorrowOne()
{
book.AvailableAmount--;
}

public int AmountToBorrow()
{
return book.AvailableAmount;
}
}
}
36 changes: 35 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.Net;

namespace Exercise
{
public abstract class Item : LibObject
{
Expand All @@ -8,6 +10,8 @@ public Item(int amount, int year)
ObjType = ObjectType.Item;
YearCreated = year;
}


}

public class Book : Item
Expand All @@ -18,5 +22,35 @@ public Book(string author, string title, int year, int amount) : base(amount, ye
Author = author;
}
public string Author { get; set; }

public override RegisteredObject GetRegistrationInfo()
{
return new RegisteredObject()
{
AvailableAmount = AvailableAmount,
Id = -1,
Info = NameOrTitle
};
}
}

public class Video : Item
{
public Video(string director, string title, int year, int amount) : base(amount, year)
{
NameOrTitle = title;
Director = director;
}
public string Director { get; set; }

public override RegisteredObject GetRegistrationInfo()
{
return new RegisteredObject()
{
AvailableAmount = AvailableAmount,
Id = -1,
Info = NameOrTitle
};
}
}
}
4 changes: 3 additions & 1 deletion DesignPatternsSolution/Exercise/LibObject.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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 abstract RegisteredObject GetRegistrationInfo();
}

public enum ObjectType
Expand Down
16 changes: 9 additions & 7 deletions DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
namespace Exercise
using System.Security.Policy;

namespace Exercise
{
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()
public int Register(IRegistarable registerableObject)
{
// return RegistrationRepository.Register();

//dummy, just to compile
return 0;
return RegistrationRepository.Register(registerableObject);
}
}
}
10 changes: 10 additions & 0 deletions DesignPatternsSolution/Exercise/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,15 @@ public void SetId(int id)
{
ObjectId = id;
}

public override RegisteredObject GetRegistrationInfo()
{
return new RegisteredObject()
{
AvailableAmount = 1,
Id = -1,
Info = NameOrTitle
};
}
}
}