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
6 changes: 3 additions & 3 deletions DesignPatternsSolution/DesignPatternsSolution.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exercise", "Exercise\Exercise.csproj", "{EA98A53A-F8CD-4539-9E57-1FEF2D64F13A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Execise.Tests", "Execise.Tests\Execise.Tests.csproj", "{464FBD03-CA8B-4926-8F69-66726819DA02}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exercise.Tests", "Execise.Tests\Exercise.Tests.csproj", "{464FBD03-CA8B-4926-8F69-66726819DA02}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Documents", "Documents", "{8C88EFFA-EC68-42ED-A482-A7B8964538E8}"
ProjectSection(SolutionItems) = preProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ProjectGuid>{464FBD03-CA8B-4926-8F69-66726819DA02}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Execise.Tests</RootNamespace>
<RootNamespace>Exercise.Tests</RootNamespace>
<AssemblyName>Execise.Tests</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Execise.Tests")]
[assembly: AssemblyTitle("Exercise.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Execise.Tests")]
[assembly: AssemblyProduct("Exercise.Tests")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
Expand Down
27 changes: 21 additions & 6 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Exercise;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Execise.Tests
namespace Exercise.Tests
{
[TestClass]
public class UnitTests
Expand All @@ -10,29 +10,44 @@ 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 libraryInstance1 = Library.GetInstance();
Library libraryInstance2 = Library.GetInstance();

Assert.AreEqual(libraryInstance1, libraryInstance2);
}

//test that an book was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void BookShouldRegister()
{
Book book = new Book("Cash Craig", "Fake Book", 1991, 7);
int result = RegistrationRepository.Register(book);
Assert.AreNotEqual(-1, result);
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
Customer customer = new Customer("Cash Craig", "123 Fake Street");
int result = RegistrationRepository.Register(customer);
Assert.AreNotEqual(-1, result);
}

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

Book myBook = new Book("Cash Craig", "Fake Book", 1991, 7);

int oldCount = myBook.AvailableAmount;

Borrowable bookBorrowable = new Borrowable(null, new Borrow(myBook));
bookBorrowable.BorrowOne();

int newCount = myBook.AvailableAmount;

Assert.AreEqual(oldCount - 1, newCount);
}
}
}
45 changes: 43 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
namespace Exercise
{
public class BookBorrowable
public abstract class ABorrowable
{

public virtual void BorrowOne() { }
protected Borrow Borrow;

protected ABorrowable(Borrow borrow)
{
this.Borrow = borrow;
}
}

public class Borrowable : ABorrowable
{
private readonly ABorrowable _borrowable;

public Borrowable(ABorrowable borrowable, Borrow borrow) : base(borrow)
{
_borrowable = borrowable;
}

public override void BorrowOne()
{
if (_borrowable != null)
{
_borrowable.BorrowOne();
}

Borrow.BorrowOne();
}
}

public class Borrow
{
private readonly LibObject _libObject;

public Borrow(LibObject libObject)
{
this._libObject = libObject;
}

public void BorrowOne()
{
this._libObject.AvailableAmount--;
}
}
}
13 changes: 11 additions & 2 deletions DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@
{
public abstract class Item : LibObject
{
public Item(int amount, int year)
protected Item(int amount, int year)
{
AvailableAmount = amount;
ObjType = ObjectType.Item;
YearCreated = year;
}
}

public class Book : Item
public class Book : Item, IRegistrable
{
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 resgisteredObject = new RegisteredObject();
resgisteredObject.Info = this.NameOrTitle;
resgisteredObject.AvailableAmount = this.AvailableAmount;

return resgisteredObject;
}
}
}
15 changes: 9 additions & 6 deletions DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
{
public class Library
{
//implement Singleton to make sure only one library will exist
private static readonly Library Instance = new Library();


private Library()
{
}

//Implement Register method by utilizing RegistrationRepository (complete missing parts)
public int Register()
public static Library GetInstance()
{
// return RegistrationRepository.Register();
return Instance;
}

//dummy, just to compile
public int Register()
{
return 0;
}
}
Expand Down
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, IRegistrable
{
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 registeredObject = new RegisteredObject();
registeredObject.Info = this.NameOrTitle;
registeredObject.AvailableAmount = this.AvailableAmount;

return registeredObject;
}
}
}
23 changes: 8 additions & 15 deletions DesignPatternsSolution/Exercise/Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,35 @@
using System.Linq;
namespace Exercise
{
public interface IRegistarable
public interface IRegistrable
{
RegisteredObject GetRegistrationInfo();
}

public static class RegistrationRepository
{
//registered objects list
private static List<RegisteredObject> _registeredList = new List<RegisteredObject>();
private static readonly List<RegisteredObject> RegisteredList = new List<RegisteredObject>();

private static int _nextId = 1;

//With BRIDGE pattern, implement Register method so it will accept both a Person and an Item
public static int Register(IRegistarable registarable)
public static int Register(IRegistrable registrable)
{
//get info from an lib object
var info = registarable.GetRegistrationInfo();
var info = registrable.GetRegistrationInfo();
if (info == null) return -1;

//get new id for for the registered object
info.Id = _nextId;

//add to registration repository
_registeredList.Add(info);
RegisteredList.Add(info);

//store next available id
_nextId = _registeredList.Count + 1;
_nextId = RegisteredList.Count + 1;

//return success
return info.Id;
}

public static int DeleteAllRegisteredItems()
{
var size = _registeredList.Count();
_registeredList.RemoveRange(0, size);
var size = RegisteredList.Count();
RegisteredList.RemoveRange(0, size);
_nextId = 1;

return size;
Expand Down