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
3 changes: 2 additions & 1 deletion DesignPatternsSolution/Execise.Tests/Execise.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
<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>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
24 changes: 19 additions & 5 deletions DesignPatternsSolution/Execise.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,43 @@ 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
Assert.AreEqual(Library.getInstance(), Library.getInstance());
}

//test that an book was registered successfully by checking the returned Id value is not -1
[TestMethod]
public void BookShouldRegister()
{
Library library = Library.getInstance();

Book book = new Book("author", "title", 1950, 2, new LibObjectRegistrationInfoAPI());

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();

Customer customer = new Customer("name", "address", new LibObjectRegistrationInfoAPI());

Assert.AreNotEqual(-1, library.Register(customer));
}

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

Library library = Library.getInstance();

Book book = new Book("author", "title", 1950, 2, new LibObjectRegistrationInfoAPI());
BookBorrowable borrowable = new BookBorrowable(null, book);

borrowable.BorrowOne();

Assert.AreEqual(1, book.AvailableAmount);
}
}
}
30 changes: 28 additions & 2 deletions DesignPatternsSolution/Exercise/BookBorrowable.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
namespace Exercise
{
public class BookBorrowable
public abstract class Borrowable
{

abstract public void BorrowOne();
}

public class BookBorrowable : Borrowable
{
private Book _book;
private Borrowable _borrowable;

public BookBorrowable(Borrowable borrowable, Book book)
{
this._borrowable = borrowable;
this._book = book;
}

override public void BorrowOne()
{
// note that this is not decorating a "book", but a "Borrowable"
if(_borrowable != null)
{
_borrowable.BorrowOne();
}

if (this._book.AvailableAmount > 0)
{
this._book.AvailableAmount--;
}
}
}
}
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, IRegistrationInfoAPI registerableInfoBridge)
: base(registerableInfoBridge)
{
NameOrTitle = name;
Address = addr;
Expand Down
7 changes: 5 additions & 2 deletions DesignPatternsSolution/Exercise/Exercise.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Exercise</RootNamespace>
<AssemblyName>Exercise</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -47,11 +48,13 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BookBorrowable.cs" />
<Compile Include="IRegistrationInfoAPI.cs" />
<Compile Include="Item.cs" />
<Compile Include="LibObject.cs" />
<Compile Include="Library.cs" />
<Compile Include="Person.cs" />
<Compile Include="Customer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="LibObjectRegistrationInfoAPI.cs" />
<Compile Include="Registration.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
13 changes: 13 additions & 0 deletions DesignPatternsSolution/Exercise/IRegistrationInfoAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise
{
public interface IRegistrationInfoAPI
{
RegisteredObject GetRegistrationInfo(LibObject objectIn);
}
}
4 changes: 2 additions & 2 deletions DesignPatternsSolution/Exercise/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public abstract class Item : LibObject
{
public Item(int amount, int year)
public Item(int amount, int year, IRegistrationInfoAPI registerableInfoBridge) : base(registerableInfoBridge)
{
AvailableAmount = amount;
ObjType = ObjectType.Item;
Expand All @@ -12,7 +12,7 @@ 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, IRegistrationInfoAPI registerableInfoBridge) : base(amount, year, registerableInfoBridge)
{
NameOrTitle = title;
Author = author;
Expand Down
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 IRegistrationInfoAPI _registerableInfoBridge;

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 _registerableInfoBridge.GetRegistrationInfo(this);
}

public LibObject(IRegistrationInfoAPI registerableInfoBridge)
{
this._registerableInfoBridge = registerableInfoBridge;
}
}

public enum ObjectType
Expand Down
22 changes: 22 additions & 0 deletions DesignPatternsSolution/Exercise/LibObjectRegistrationInfoAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Exercise
{
public class LibObjectRegistrationInfoAPI : IRegistrationInfoAPI
{
public LibObjectRegistrationInfoAPI() { }

public RegisteredObject GetRegistrationInfo(LibObject objectIn)
{
RegisteredObject regObject = new RegisteredObject();
regObject.Info = objectIn.NameOrTitle;
regObject.AvailableAmount = objectIn.AvailableAmount;
regObject.Id = objectIn.ObjectId;
return regObject;
}
}
}
18 changes: 13 additions & 5 deletions DesignPatternsSolution/Exercise/Library.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
{
public class Library
{
private static Library instance;

//implement Singleton to make sure only one library will exist
public static Library getInstance()
{
if (instance == null)
{
instance = new Library();
}
return instance;
}

private Library() {}


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

//dummy, just to compile
return 0;
return RegistrationRepository.Register(registerable);
}
}
}