Skip to content
Open

Done #18

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
21 changes: 17 additions & 4 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@ 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
var lib = Library.getInstance();
var rib = Library.getInstance();

Assert.AreEqual<Library>(lib, rib);
}

//test that an book was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void BookShouldRegister()
{
Book b = new Book("Guy", "title", 1990, 100);
var lib = Library.getInstance();

Assert.AreNotEqual(lib.Register(b), -1);
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
Customer c = new Customer("Guy", "777 Broadway Ave");
var lib = Library.getInstance();
Assert.AreNotEqual(lib.Register(c), -1);
}

//test that a book can be borrowed
Expand All @@ -32,7 +41,11 @@ 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.

}
Book b = new Book("Guy", "title", 1990, 100);
BookBorrowable bb = new BookBorrowable(b, 10);
var x = bb.borrowOne();
Assert.AreEqual(bb.numberOfBooksLeft(), 9);

}
}
}
21 changes: 19 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
namespace Exercise
{
public class BookBorrowable
public class BookBorrowable
{

private Book book;
private int numberOfBooks = 0;
public BookBorrowable(Book b, int amount)
{
book = b;
numberOfBooks = amount;
}

public Book borrowOne()
{

numberOfBooks--;
return book;
}
public int numberOfBooksLeft()
{
return numberOfBooks;
}
}
}
14 changes: 12 additions & 2 deletions DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@ public Item(int amount, int year)
ObjType = ObjectType.Item;
YearCreated = year;
}

}

public class Book : Item
public class Book : Item, IRegistarable
{
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)
{
NameOrTitle = title;
Author = author;
}
public string Author { get; set; }

public RegisteredObject GetRegistrationInfo()
{
RegisteredObject x = new RegisteredObject();
x.obj = this;
return x;
}

}
}
21 changes: 17 additions & 4 deletions DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,30 @@
{
public class Library
{
private BookBorrowable bbook;
private int numberOfBooks = 0;

static Library instance = new Library();
//implement Singleton to make sure only one library will exist
private Library() { }


public static Library getInstance()
{
return instance;
}



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


return RegistrationRepository.Register(obj);

//dummy, just to compile
return 0;
//return 0;
}

}
}
9 changes: 8 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,12 @@ public void SetId(int id)
{
ObjectId = id;
}

public RegisteredObject GetRegistrationInfo()
{
RegisteredObject x = new RegisteredObject();
x.obj = this;
return x;
}
}
}
1 change: 1 addition & 0 deletions DesignPatternsSolution/Exercise/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class RegisteredObject
public string Info { get; set; }
public int Id { get; set; }
public int AvailableAmount { get; set; }
public LibObject obj { get; set; }

public override string ToString()
{
Expand Down