Skip to content

Commit 9aaae46

Browse files
committed
Submitting changes after session 5
Added starting and ending Principal methods and changed withdraw method to track the total withdrawn amount.
1 parent bc9f511 commit 9aaae46

3 files changed

Lines changed: 54 additions & 9 deletions

File tree

scratchpad.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Capital gains
22

3-
get rid of SavingsAccountYear initializers
3+
get rid of SavingsAccountYear initializers
4+
5+
clean up _SavingsAccountYearTest

src/info/javierliarte/letsPlayTDD/finances/SavingsAccountYear.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public class SavingsAccountYear {
55
private int startingBalance = 0;
66
private int capitalGainsAmount = 0;
77
private int interestRate = 0;
8+
private int totalWithdrawn = 0;
89

910
public SavingsAccountYear(int startingBalance, int interestRate) {
1011
this.startingBalance = startingBalance;
@@ -21,20 +22,32 @@ public int startingBalance() {
2122
return startingBalance;
2223
}
2324

25+
26+
public int startingPrincipal() {
27+
return startingBalance - capitalGainsAmount;
28+
}
29+
30+
31+
public int endingPrincipal() {
32+
int result = startingPrincipal() - totalWithdrawn;
33+
return (result < 0) ? 0 : result;
34+
}
35+
2436
public int interestRate() {
2537
return interestRate;
2638
}
2739

2840
public int endingBalance() {
29-
return startingBalance * (100 + interestRate) /100;
41+
int modifiedStart = startingBalance - totalWithdrawn;
42+
return modifiedStart * (100 + interestRate) /100;
3043
}
3144

3245
public SavingsAccountYear nextYear() {
3346
return new SavingsAccountYear(this.endingBalance(),this.interestRate);
3447
}
3548

3649
public void withdraw(int amount) {
37-
startingBalance -= amount;
50+
this.totalWithdrawn += amount;
3851
}
3952

4053
}

src/info/javierliarte/letsPlayTDD/finances/_SavingsAccountYearTest.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,44 @@ public void withdrawingFundsOccursAtTheBeginningOfTheYear() {
4040
}
4141

4242
@Test
43-
public void withdrawingMoreThanPrincipalInccursCapitalGainsTax() {
44-
SavingsAccountYear year = new SavingsAccountYear(10000, 7000, 10);
45-
year.withdraw(3000);
43+
public void multipleWithdrawalsInAYear () {
44+
SavingsAccountYear year = new SavingsAccountYear(10000, 10);
45+
year.withdraw(1000);
46+
year.withdraw(2000);
4647
assertEquals(7700, year.endingBalance());
47-
year.withdraw(5000);
48-
assertEquals(2000 + 200 - (1250), year.endingBalance());
49-
5048
}
49+
50+
@Test
51+
public void principal() {
52+
SavingsAccountYear year = new SavingsAccountYear(10000, 7000, 10);
53+
assertEquals (3000, year.startingPrincipal());
54+
}
55+
56+
@Test
57+
public void endingPrincipal() {
58+
SavingsAccountYear year = new SavingsAccountYear(10000, 7000, 10);
59+
assertEquals ("Starting principal", 3000, year.startingPrincipal());
60+
year.withdraw(2000);
61+
assertEquals ("Ending principal", 1000, year.endingPrincipal());
62+
}
63+
64+
@Test
65+
public void endingPrincipalNeverGoesBelowZero() {
66+
SavingsAccountYear year = new SavingsAccountYear(10000, 7000, 10);
67+
assertEquals ("Starting principal", 3000, year.startingPrincipal());
68+
year.withdraw(4000);
69+
assertEquals("ending principal", 0, year.endingPrincipal());
70+
}
71+
72+
// @Test
73+
// public void withdrawingMoreThanPrincipalInccursCapitalGainsTax() {
74+
// SavingsAccountYear year = new SavingsAccountYear(10000, 7000, 10);
75+
// year.withdraw(3000);
76+
// assertEquals(7700, year.endingBalance());
77+
// year.withdraw(5000);
78+
// assertEquals(2000 + 200 - (1250), year.endingBalance());
79+
//
80+
// }
5181

5282
private SavingsAccountYear newAccount() {
5383
SavingsAccountYear account = new SavingsAccountYear(10000, 10);

0 commit comments

Comments
 (0)