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
2 changes: 1 addition & 1 deletion DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Execise.Tests</RootNamespace>
<AssemblyName>Execise.Tests</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
Expand Down
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,25 @@ 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.ReferenceEquals(lib1, lib2);
}

//test that an book was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void BookShouldRegister()
{
Book book = new Book("John Smith", "Adventures in Coding", 2017, 1, new Exercise.RegistrationApi());
Assert.IsTrue(RegistrationRepository.Register(book) > -1);
}

//test that an customer was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void CustomerShouldRegister()
{
Customer customer = new Customer("Jane Doe", "386 Broadway", new Exercise.RegistrationApi());
Assert.IsTrue(RegistrationRepository.Register(customer) > -1);
}

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

int totalAmount = 10;
Book book = new Book("John Smith", "Adventures in Coding", 2017, totalAmount, new Exercise.RegistrationApi());
BookBorrowable borrowable = new BookBorrowable(null, book);
borrowable.BorrowOne();
Assert.IsTrue(book.AvailableAmount == (totalAmount - 1));
}
}
}
38 changes: 36 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
namespace Exercise
{
public class BookBorrowable
public abstract class Borrowable
{

public virtual void BorrowOne() {}

protected LibObject LibObject;
public Borrowable(LibObject libObject)
{
LibObject = libObject;
}
}

public class BookBorrowable : Borrowable
{
private Borrowable borrowable;

public BookBorrowable(Borrowable borrowable, Book book)
: base(book)
{
this.borrowable = borrowable;
}

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

if (LibObject.AvailableAmount > 0)
{
LibObject.AvailableAmount--;
}
else
{
throw new System.Exception("No books left to borrow.");
}
}
}
}
3 changes: 2 additions & 1 deletion DesignPatternsSolution/Exercise/Exercise.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Exercise</RootNamespace>
<AssemblyName>Exercise</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
Expand Down Expand Up @@ -53,6 +53,7 @@
<Compile Include="Person.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Registration.cs" />
<Compile Include="RegistrationApi.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
9 changes: 7 additions & 2 deletions DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{
public abstract class Item : LibObject
{
public Item(int amount, int year)
public Item(int amount, int year, IRegistrationApi registrationApi)
: base(registrationApi)
{
AvailableAmount = amount;
ObjType = ObjectType.Item;
Expand All @@ -12,11 +13,15 @@ public Item(int amount, int year)

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

public string Author { get; set; }


}
}
14 changes: 13 additions & 1 deletion DesignPatternsSolution/Exercise/LibObject.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
namespace Exercise
{
public abstract class LibObject
public abstract class LibObject : IRegistarable
{
private IRegistrationApi registrationApi;

protected LibObject(IRegistrationApi registrationApi)
{
this.registrationApi = registrationApi;
}

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 RegisteredObject GetRegistrationInfo()
{
return registrationApi.GetRegistrationInfo(this);
}
}

public enum ObjectType
Expand Down
17 changes: 11 additions & 6 deletions DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
public class Library
{
//implement Singleton to make sure only one library will exist
private static 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
return 0;
//Implement Register method by utilizing RegistrationRepository (complete missing parts)
public int Register(IRegistarable registrable)
{
return RegistrationRepository.Register(registrable);
}
}
}
3 changes: 2 additions & 1 deletion DesignPatternsSolution/Exercise/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ public class Customer : LibObject
public string Address { get; set; }
public DateTime RegisteredAt { get; set; }

public Customer(string name, string addr)
public Customer(string name, string addr, IRegistrationApi registrationApi)
: base(registrationApi)
{
NameOrTitle = name;
Address = addr;
Expand Down
30 changes: 30 additions & 0 deletions DesignPatternsSolution/Exercise/RegistrationApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise
{
public interface IRegistrationApi
{
RegisteredObject GetRegistrationInfo(LibObject libObject);
}

public class RegistrationApi : IRegistrationApi
{
public RegistrationApi()
{
}

public RegisteredObject GetRegistrationInfo(LibObject libObject)
{
return new RegisteredObject
{
Info = libObject.NameOrTitle,
AvailableAmount = libObject.AvailableAmount,
Id = libObject.ObjectId
};
}
}
}