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
28 changes: 23 additions & 5 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Exercise;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace Execise.Tests
{
Expand All @@ -10,29 +11,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 myLib = Library.getInstance();
Library secondLib = Library.getInstance();

Assert.AreSame(myLib, secondLib);
}

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

}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
Console.WriteLine("WHAT");
Library myLib = Library.getInstance();
Customer customer = new Customer("Name", "Address");
int ret_code = myLib.Register(customer);
Assert.AreNotEqual(-1, ret_code);
}

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

//Didn't do decorator.. this is more reliable.
int start_book_count = 30;
Book borrowBook = new Book("Author2", "Title2", 1990, start_book_count);
Library myLib = Library.getInstance();
int book_id = myLib.Register(borrowBook);
myLib.borrow(book_id);
int book_count = myLib.getAvailableAmount(book_id);
Assert.AreEqual(start_book_count - 1, book_count);
}
}
}
4 changes: 2 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Exercise
{
public class BookBorrowable
public interface IBorrowable
{

}
}
11 changes: 10 additions & 1 deletion DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,22 @@ public Item(int amount, int year)
}
}

public class Book : Item
public class Book : Item, IRegistarable, IBorrowable
{
public Book(string author, string title, int year, int amount) : base(amount, year)
{
NameOrTitle = title;
Author = author;
}

public RegisteredObject GetRegistrationInfo()
{
RegisteredObject obj = new RegisteredObject();
obj.AvailableAmount = this.AvailableAmount;
obj.Info = "Stuff about this book";
obj.obj = this;
return obj;
}
public string Author { get; set; }
}
}
25 changes: 20 additions & 5 deletions DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,31 @@
public class Library
{
//implement Singleton to make sure only one library will exist

private static Library library = new Library();

private Library() {

}

public static Library getInstance()
{
return library;
}

//Implement Register method by utilizing RegistrationRepository (complete missing parts)
public int Register()
public int Register(IRegistarable registarable)
{
// return RegistrationRepository.Register();
return RegistrationRepository.Register(registarable);
}

//dummy, just to compile
return 0;
public int borrow(int book_id)
{
return RegistrationRepository.BorrowBook(book_id);
}

public int getAvailableAmount(int book_id)
{
return RegistrationRepository.GetAvailableAmount(book_id);
}
}
}
11 changes: 10 additions & 1 deletion DesignPatternsSolution/Exercise/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Exercise
{
public class Customer : LibObject
public class Customer : LibObject, IRegistarable
{
public string Address { get; set; }
public DateTime RegisteredAt { get; set; }
Expand All @@ -19,5 +19,14 @@ public void SetId(int id)
{
ObjectId = id;
}

public RegisteredObject GetRegistrationInfo()
{
RegisteredObject obj = new RegisteredObject();
obj.AvailableAmount = this.AvailableAmount;
obj.Info = "Stuff about this person";
obj.obj = this;
return obj;
}
}
}
28 changes: 28 additions & 0 deletions DesignPatternsSolution/Exercise/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,41 @@ public static int DeleteAllRegisteredItems()

return size;
}

public static int BorrowBook(int book_id)
{
foreach(RegisteredObject obj in _registeredList) {
if (obj.Id == book_id)
{
if (obj.obj is IBorrowable && obj.AvailableAmount > 0)
{
obj.AvailableAmount -= 1;
return 0;
}
}
}
return -1;
}

public static int GetAvailableAmount(int book_id)
{
foreach (RegisteredObject obj in _registeredList)
{
if (obj.Id == book_id)
{
return obj.AvailableAmount;
}
}
return -1;
}
}

public class RegisteredObject
{
public string Info { get; set; }
public int Id { get; set; }
public int AvailableAmount { get; set; }
public IRegistarable obj { get; set; }

public override string ToString()
{
Expand Down