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: 12 additions & 3 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ 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 lib1 = Library.getInstance();
Library lib2 = Library.getInstance();

Assert.AreSame(lib1, lib2, "Objects are not the same instance!");
}

//test that an book was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void BookShouldRegister()
{
Library library = Library.getInstance();
LibObject book = new Book("Brandon Meilleur", "Best Code Practices", 1994, 20);
Assert.AreNotEqual(-1, library.Register(book));
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
Library library = Library.getInstance();
LibObject customer = new Customer("Brandon Meilleur", "123 Fake St");
Assert.AreNotEqual(-1, library.Register(customer));
}

//test that a book can be borrowed
Expand All @@ -32,7 +40,8 @@ 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 borrowable = new BookBorrowable("Brandon Meilleur", "Cool Things and Stuff", 2000, 15);
Assert.AreEqual(14, borrowable.borrowOne());
}
}
}
23 changes: 21 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
namespace Exercise
{
public class BookBorrowable
public class BookBorrowable : Book
{


public BookBorrowable(string author, string title, int year, int amount)
: base(author, title, year, amount)
{

}

public int borrowOne()
{
if (this.AvailableAmount > 0)
{
this.AvailableAmount -= 1;
return this.AvailableAmount;
}
else
{
return -1;
}

}
}
}
10 changes: 10 additions & 0 deletions DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ public Item(int amount, int year)
ObjType = ObjectType.Item;
YearCreated = year;
}

override public RegisteredObject GetRegistrationInfo()
{
RegisteredObject registerable = new RegisteredObject();
registerable.Info = ObjType.ToString();
registerable.AvailableAmount = AvailableAmount;

return registerable;
}

}

public class Book : Item
Expand Down
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; }

abstract public RegisteredObject GetRegistrationInfo();
}

public enum ObjectType
Expand Down
17 changes: 12 additions & 5 deletions DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@
public class Library
{
//implement Singleton to make sure only one library will exist
static Library instance = new Library();

private Library()
{
//nothing needed here
}

public static Library getInstance()
{
return instance;
}


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

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

override public RegisteredObject GetRegistrationInfo()
{
RegisteredObject registerable = new RegisteredObject();
registerable.Info = ObjType.ToString();

return registerable;
}
}
}