Skip to content

Commit

Permalink
<Shakunthala/devesh>: Added interest percentage validation in Structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
sakunthala committed Apr 5, 2011
1 parent 7d48227 commit f5bd27c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
8 changes: 8 additions & 0 deletions Src/AnujBank/AnujBank/Structure.cs
Expand Up @@ -13,9 +13,17 @@ public class Structure
public Structure(ClientAccounts sourceClientAccounts, List<Allocation> allocations, InterestRates interestRates)
{
if(sourceClientAccounts.Count < 2) throw new ArgumentException("A structure must have at least 2 source accounts.");

float totalPercentage = 0;
allocations.ForEach(al => totalPercentage += al.GetAllocationPercentage());

if (totalPercentage != 100)
throw new ArgumentException("A structure should have 100% allocation.");

this.sourceClientAccounts = sourceClientAccounts;
this.allocations = allocations;
this.interestRates = interestRates;

}

public bool SharesASourceAccountWith(Structure newStructure)
Expand Down
15 changes: 12 additions & 3 deletions Src/AnujBank/TestAnujBank/TestClient.cs
@@ -1,4 +1,5 @@
using AnujBank;
using System.Collections.Generic;
using AnujBank;
using NUnit.Framework;

namespace TestAnujBank
Expand All @@ -15,11 +16,19 @@ public void ShouldBeAbleAddAStructure()
var clientAccounts = new ClientAccounts();
clientAccounts.Add(account1);
clientAccounts.Add(account2);
var structure = new Structure(clientAccounts, null,null);
var structure = new Structure(clientAccounts, getAllocation(), null);
var client = new Client(clientId, clientAccounts);
client.AddStructure(structure);
Assert.IsTrue(client.Contains(structure));
}
}


private List<Allocation> getAllocation()
{
var clientId = new ClientId("ABC123");
var account1 = new Account(new AccountId(43214321), clientId);
return new List<Allocation> { new Allocation(account1, 100) };
}
}

}
45 changes: 39 additions & 6 deletions Src/AnujBank/TestAnujBank/TestStructure.cs
Expand Up @@ -31,17 +31,20 @@ public void ShouldBeAbleToFindIfTwoStructuresHaveSharedAccounts()
var clientAccounts1 = new ClientAccounts();
clientAccounts1.Add(account1);
clientAccounts1.Add(account2);
var structure1 = new Structure(clientAccounts1, null,null);

var allocations = new List<Allocation> { new Allocation(account1, 30), new Allocation(account2, 70) };

var structure1 = new Structure(clientAccounts1, allocations, null);

var clientAccounts2 = new ClientAccounts();
clientAccounts2.Add(account1);
clientAccounts2.Add(account3);
var structure2 = new Structure(clientAccounts2, null,null);
var structure2 = new Structure(clientAccounts2, allocations, null);

var clientAccounts3 = new ClientAccounts();
clientAccounts3.Add(account4);
clientAccounts3.Add(account3);
var structure3 = new Structure(clientAccounts3, null,null);
var structure3 = new Structure(clientAccounts3, allocations, null);

Assert.IsTrue(structure1.SharesASourceAccountWith(structure2));
Assert.IsFalse(structure1.SharesASourceAccountWith(structure3));
Expand All @@ -51,7 +54,7 @@ public void ShouldBeAbleToFindIfTwoStructuresHaveSharedAccounts()
[Test]
public void ShouldCalculateNetBalance()
{
Structure structure = GetTestStructure(1000.0, -500, null, null);
Structure structure = GetTestStructure(1000.0, -500, getAllocation(), null);
Assert.AreEqual(500.0d, structure.NetBalance());
}

Expand All @@ -64,7 +67,7 @@ public void ShouldComputeInterestOnPositiveNetBalance()

mock.Setup(i => i.PositiveInterestRate()).Returns(2.0);

Assert.AreEqual(expected, GetTestStructure(1000.0, -500.0, null, mock.Object).NetInterest().ToString().Substring(0, 5));
Assert.AreEqual(expected, GetTestStructure(1000.0, -500.0, getAllocation(), mock.Object).NetInterest().ToString().Substring(0, 5));

mock.VerifyAll();
}
Expand All @@ -78,7 +81,7 @@ public void ShouldComputeInterestOnNegativeNetBalance()

mock.Setup(i => i.NegativeInterestRate()).Returns(3.0);

Assert.AreEqual(expected, GetTestStructure(-1000.0, 500.0, null, mock.Object).NetInterest().ToString().Substring(0, 5));
Assert.AreEqual(expected, GetTestStructure(-1000.0, 500.0, getAllocation(), mock.Object).NetInterest().ToString().Substring(0, 5));

mock.VerifyAll();
}
Expand Down Expand Up @@ -124,6 +127,36 @@ public void ShouldAllocateInterestAmountToPooledAccount()

}


[Test]
public void ShouldNotCreateStructureWithout100PercentAllocation()
{
var mock = new Mock<InterestRates>();

mock.Setup(i => i.PositiveInterestRate()).Returns(2.0);
var clientId = new ClientId("ABC123");
var account1 = new Account(new AccountId(12341234), clientId);
var account2 = new Account(new AccountId(12341235), clientId);
var allocations = new List<Allocation> { new Allocation(account1, 30) , new Allocation(account2, 30)};

try
{
GetTestStructure(1000.0, -500.0, allocations, mock.Object);
Assert.Fail("Should Not create Structure with Allocation interest percent sum not equal to 100");
}
catch (ArgumentException)
{
}

}

private List<Allocation> getAllocation()
{
var clientId = new ClientId("ABC123");
var account1 = new Account(new AccountId(43214321), clientId);
return new List<Allocation> { new Allocation(account1, 100) };
}

private static Structure GetTestStructure(double balance1, double balance2, List<Allocation> allocations, InterestRates interestRates)
{
var clientId = new ClientId("ABC123");
Expand Down
16 changes: 13 additions & 3 deletions Src/AnujBank/TestAnujBank/TestStructures.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using AnujBank;
using NUnit.Framework;

Expand All @@ -16,7 +17,7 @@ public void ShouldBeAbleToAddAStructure()
var clientAccounts = new ClientAccounts();
clientAccounts.Add(account1);
clientAccounts.Add(account2);
var structure = new Structure(clientAccounts, null,null);
var structure = new Structure(clientAccounts, getAllocation(), null);
var structures = new Structures();
structures.Add(structure);

Expand All @@ -38,13 +39,22 @@ public void ShouldNotBeAbleToCreateClientStructuresWithTwoRepeatingAccounts()
clientAccounts2.Add(account1);
clientAccounts2.Add(account3);

var structure1 = new Structure(clientAccounts1, null,null);
var structure2 = new Structure(clientAccounts2, null, null);
var structure1 = new Structure(clientAccounts1, getAllocation(), null);
var structure2 = new Structure(clientAccounts2, getAllocation(), null);

var structures = new Structures();
structures.Add(structure1);

Assert.Throws<ArgumentException>(() => structures.Add(structure2));
}


private List<Allocation> getAllocation()
{
var clientId = new ClientId("ABC123");
var account1 = new Account(new AccountId(43214321), clientId);
return new List<Allocation> { new Allocation(account1, 100) };
}

}
}

0 comments on commit f5bd27c

Please sign in to comment.