From f5bd27c74bd9c7e4379c65bb136a66c6fd13977a Mon Sep 17 00:00:00 2001 From: sakunthala Date: Tue, 5 Apr 2011 17:31:17 +0530 Subject: [PATCH] : Added interest percentage validation in Structure. --- Src/AnujBank/AnujBank/Structure.cs | 8 ++++ Src/AnujBank/TestAnujBank/TestClient.cs | 15 +++++-- Src/AnujBank/TestAnujBank/TestStructure.cs | 45 ++++++++++++++++++--- Src/AnujBank/TestAnujBank/TestStructures.cs | 16 ++++++-- 4 files changed, 72 insertions(+), 12 deletions(-) diff --git a/Src/AnujBank/AnujBank/Structure.cs b/Src/AnujBank/AnujBank/Structure.cs index 006a5f5..9c66c99 100644 --- a/Src/AnujBank/AnujBank/Structure.cs +++ b/Src/AnujBank/AnujBank/Structure.cs @@ -13,9 +13,17 @@ public class Structure public Structure(ClientAccounts sourceClientAccounts, List 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) diff --git a/Src/AnujBank/TestAnujBank/TestClient.cs b/Src/AnujBank/TestAnujBank/TestClient.cs index e3d2379..b3a0da0 100644 --- a/Src/AnujBank/TestAnujBank/TestClient.cs +++ b/Src/AnujBank/TestAnujBank/TestClient.cs @@ -1,4 +1,5 @@ -using AnujBank; +using System.Collections.Generic; +using AnujBank; using NUnit.Framework; namespace TestAnujBank @@ -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 getAllocation() + { + var clientId = new ClientId("ABC123"); + var account1 = new Account(new AccountId(43214321), clientId); + return new List { new Allocation(account1, 100) }; + } } } diff --git a/Src/AnujBank/TestAnujBank/TestStructure.cs b/Src/AnujBank/TestAnujBank/TestStructure.cs index 55a5fc1..6b560bb 100644 --- a/Src/AnujBank/TestAnujBank/TestStructure.cs +++ b/Src/AnujBank/TestAnujBank/TestStructure.cs @@ -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 { 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)); @@ -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()); } @@ -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(); } @@ -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(); } @@ -124,6 +127,36 @@ public void ShouldAllocateInterestAmountToPooledAccount() } + + [Test] + public void ShouldNotCreateStructureWithout100PercentAllocation() + { + var mock = new Mock(); + + 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 { 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 getAllocation() + { + var clientId = new ClientId("ABC123"); + var account1 = new Account(new AccountId(43214321), clientId); + return new List { new Allocation(account1, 100) }; + } + private static Structure GetTestStructure(double balance1, double balance2, List allocations, InterestRates interestRates) { var clientId = new ClientId("ABC123"); diff --git a/Src/AnujBank/TestAnujBank/TestStructures.cs b/Src/AnujBank/TestAnujBank/TestStructures.cs index 3dda55f..83bc6a2 100644 --- a/Src/AnujBank/TestAnujBank/TestStructures.cs +++ b/Src/AnujBank/TestAnujBank/TestStructures.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using AnujBank; using NUnit.Framework; @@ -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); @@ -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(() => structures.Add(structure2)); } + + + private List getAllocation() + { + var clientId = new ClientId("ABC123"); + var account1 = new Account(new AccountId(43214321), clientId); + return new List { new Allocation(account1, 100) }; + } + } }