Skip to content

Commit 168fde6

Browse files
committed
Main cleanup of SavingsAccountYear and its test class
removed capitalGainsAmount field and substituted by a method calculation added ending capital gains without implementation code as session 9 ending
1 parent a7081d3 commit 168fde6

3 files changed

Lines changed: 108 additions & 54 deletions

File tree

scratchpad.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@ Capital gains
22

33
get rid of SavingsAccountYear initializers
44

5-
clean up _SavingsAccountYearTest
5+
clean up _SavingsAccountYearTest
6+
7+
Fix primitive obsession
8+
Make sure next year is setting all variables properly -- need to carry forward capital gains, for example
9+
Calculate endingCapitalGains()
10+
11+
Consider what to do about withdrawing more than the starting balance -- assert that this is impossible
12+
fix SavingsAccountYear.newAccount abstraction -- use it or dont?
13+
We're passing in capitalGainsTaxRate everywhere -- put it in constructor?

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,61 +3,68 @@
33
public class SavingsAccountYear {
44

55
private int startingBalance = 0;
6-
private int capitalGainsAmount = 0;
76
private int interestRate = 0;
87
private int totalWithdrawn = 0;
98
private int startingPrincipal = 0;
109

11-
public SavingsAccountYear(int startingBalance, int interestRate) {
12-
this.startingBalance = startingBalance;
13-
this.interestRate = interestRate;
14-
}
15-
1610
public SavingsAccountYear(int startingBalance, int startingPrincipal, int interestRate) {
1711
this.startingBalance = startingBalance;
18-
this.startingPrincipal = startingBalance;
19-
this.capitalGainsAmount = startingBalance - startingPrincipal;
12+
this.startingPrincipal = startingPrincipal;
2013
this.interestRate = interestRate;
2114
}
2215

2316
public int startingBalance() {
2417
return startingBalance;
2518
}
2619

27-
2820
public int startingPrincipal() {
29-
return startingBalance - capitalGainsAmount;
21+
return startingPrincipal;
22+
}
23+
24+
public int startingCapitalGains() {
25+
return startingBalance - startingPrincipal;
26+
}
27+
28+
public int interestRate() {
29+
return interestRate;
30+
}
31+
32+
public int interestEarned(int capitalGainsTaxRate) {
33+
return (startingBalance - totalWithdrawnExceptCapitalGainsTax() - capitalGainsTaxIncurred(capitalGainsTaxRate)) * interestRate / 100;
34+
}
35+
36+
public int totalWithdrawnExceptCapitalGainsTax() {
37+
return this.totalWithdrawn;
3038
}
3139

40+
public int totalWithdrawn(int capitalGainsTax) {
41+
return totalWithdrawnExceptCapitalGainsTax() + capitalGainsTaxIncurred(capitalGainsTax);
42+
}
3243

3344
public int endingPrincipal() {
34-
int result = startingPrincipal() - totalWithdrawn();
45+
int result = startingPrincipal() - totalWithdrawnExceptCapitalGainsTax();
3546
return Math.max(0, result);
3647
}
3748

38-
public int interestRate() {
39-
return interestRate;
49+
public int endingCapitalGains() {
50+
return 0;
4051
}
4152

4253
public int endingBalance(int capitalGainsTaxRate) {
43-
int modifiedStart = startingBalance - totalWithdrawn() - capitalGainsTaxIncurred(capitalGainsTaxRate);
44-
return modifiedStart * (100 + interestRate) /100;
54+
int modifiedStart = startingBalance - totalWithdrawnExceptCapitalGainsTax() - capitalGainsTaxIncurred(capitalGainsTaxRate);
55+
return modifiedStart + interestEarned(capitalGainsTaxRate);
4556
}
4657

4758
public SavingsAccountYear nextYear(int capitalGainsTaxRate) {
48-
return new SavingsAccountYear(this.endingBalance(capitalGainsTaxRate),this.interestRate);
59+
return new SavingsAccountYear(this.endingBalance(capitalGainsTaxRate), 0, this.interestRate);
4960
}
5061

5162
public void withdraw(int amount) {
5263
this.totalWithdrawn += amount;
5364
}
5465

55-
public int totalWithdrawn() {
56-
return this.totalWithdrawn;
57-
}
58-
5966
public int capitalGainsWithdrawn() {
60-
int result = -1 * (startingPrincipal() - totalWithdrawn());
67+
int result = -1 * (startingPrincipal() - totalWithdrawnExceptCapitalGainsTax());
6168
return Math.max(0, result);
6269
}
6370

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

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.*;
44

5+
import org.junit.Ignore;
56
import org.junit.Test;
67

78
public class _SavingsAccountYearTest {
@@ -10,68 +11,92 @@ public class _SavingsAccountYearTest {
1011
public void startingBalanceMatchesConstructor() {
1112
assertEquals(10000, newAccount().startingBalance());
1213
}
14+
15+
@Test
16+
public void startingPrincipalMatchesConstructor() {
17+
SavingsAccountYear year = new SavingsAccountYear(10000, 3000, 10);
18+
assertEquals("Starting principal", 3000, year.startingPrincipal());
19+
}
1320

1421
@Test
1522
public void interestRateMatchesConstructor() {
1623
assertEquals(10, newAccount().interestRate());
1724
}
18-
25+
1926
@Test
20-
public void endingBalanceAppliesInterestRate() {
21-
assertEquals(11000, newAccount().endingBalance(25));
27+
public void startingCapitalGainsIsStartingBalanceMinusStartingPrincipal() {
28+
SavingsAccountYear year = new SavingsAccountYear(10000, 3000, 10);
29+
assertEquals (7000, year.startingCapitalGains());
2230
}
2331

2432
@Test
25-
public void nextYearsStartingBalanceEqualsThisYearsEndingBalance() {
26-
SavingsAccountYear thisYear = newAccount();
27-
assertEquals(thisYear.endingBalance(25), thisYear.nextYear(25)
28-
.startingBalance());
33+
public void endingPrincipalConsidersWithdrawals() {
34+
SavingsAccountYear year = new SavingsAccountYear(10000, 3000, 10);
35+
year.withdraw(2000);
36+
assertEquals("Ending principal", 1000, year.endingPrincipal());
2937
}
3038

3139
@Test
32-
public void nextYearsInterestRateEqualsThisYearsInterestRate() {
33-
SavingsAccountYear thisYear = newAccount();
34-
assertEquals(thisYear.interestRate(), thisYear.nextYear(25)
35-
.interestRate());
40+
public void endingPrincipalNeverGoesBelowZero() {
41+
SavingsAccountYear year = new SavingsAccountYear(10000, 3000, 10);
42+
year.withdraw(4000);
43+
assertEquals("ending principal", 0, year.endingPrincipal());
3644
}
37-
45+
46+
@Test
47+
public void interestEarnedIsStartingBalanceCombinedWithInterestRate() {
48+
SavingsAccountYear year = new SavingsAccountYear(10000, 3000, 10);
49+
assertEquals(1000,year.interestEarned(25));
50+
}
51+
3852
@Test
39-
public void withdrawingFundsOccursAtTheBeginningOfTheYear() {
40-
SavingsAccountYear year = new SavingsAccountYear(10000, 10);
53+
public void withdrawingFundsDoNotEarnInterest() {
54+
SavingsAccountYear year = newAccount();
4155
year.withdraw(1000);
42-
assertEquals(9900, year.endingBalance(25));
56+
assertEquals(900, year.interestEarned(25));
4357
}
44-
58+
4559
@Test
46-
public void multipleWithdrawalsInAYear() {
47-
SavingsAccountYear year = new SavingsAccountYear(10000, 10);
60+
public void totalWithdrawnIncludingCapitalGains() {
61+
SavingsAccountYear year = new SavingsAccountYear(10000, 0, 10);
4862
year.withdraw(1000);
49-
year.withdraw(2000);
50-
assertEquals(3000, year.totalWithdrawn());
63+
assertEquals("capital gains tax", 333, year.capitalGainsTaxIncurred(25));
64+
assertEquals("total withdrawn", 1333, year.totalWithdrawn(25));
65+
}
66+
67+
@Test
68+
public void capitalGainsTaxesDoNotEarnInterest() {
69+
SavingsAccountYear year = new SavingsAccountYear(10000, 0, 10);
70+
year.withdraw(1000);
71+
assertEquals("capital gains withdrawn", 1000, year.capitalGainsWithdrawn());
72+
assertEquals("capital gains tax", 333, year.capitalGainsTaxIncurred(25));
73+
// assertEquals("total withdrawn", 1333, year.totalWithdrawnIncludingCapitalGains());
74+
assertEquals("interest earned", 866, year.interestEarned(25));
5175
}
5276

5377
@Test
54-
public void principal() {
78+
@Ignore
79+
public void endingCapitalGainsIncludesInterestEarned() {
5580
SavingsAccountYear year = new SavingsAccountYear(10000, 3000, 10);
56-
assertEquals("Starting principal", 3000, year.startingPrincipal());
81+
assertEquals(7000, year.startingCapitalGains());
82+
assertEquals(3000+1000, year.endingCapitalGains());
5783
}
5884

5985
@Test
60-
public void endingPrincipal() {
61-
SavingsAccountYear year = new SavingsAccountYear(10000, 3000, 10);
62-
year.withdraw(2000);
63-
assertEquals("Ending principal", 1000, year.endingPrincipal());
86+
public void endingBalanceAppliesInterestRate() {
87+
assertEquals(11000, newAccount().endingBalance(25));
6488
}
6589

6690
@Test
67-
public void endingPrincipalNeverGoesBelowZero() {
68-
SavingsAccountYear year = new SavingsAccountYear(10000, 3000, 10);
69-
year.withdraw(4000);
70-
assertEquals("ending principal", 0, year.endingPrincipal());
91+
public void multipleWithdrawalsInAYearAreTotaled() {
92+
SavingsAccountYear year = newAccount();
93+
year.withdraw(1000);
94+
year.withdraw(2000);
95+
assertEquals(3000, year.totalWithdrawnExceptCapitalGainsTax());
7196
}
7297

7398
@Test
74-
public void capitalGainsWithdrawn() {
99+
public void withdrawingMoreThanPrincipalTakesFromCapitalGains() {
75100
SavingsAccountYear year = new SavingsAccountYear(10000, 3000, 10);
76101
year.withdraw(1000);
77102
assertEquals(0, year.capitalGainsWithdrawn());
@@ -95,8 +120,22 @@ public void capitalGainsTaxIsIncludedInEndingBalance() {
95120
assertEquals(10000 - 5000 - 666 + 433, year.endingBalance(25));
96121
}
97122

123+
@Test
124+
public void nextYearsStartingBalanceEqualsThisYearsEndingBalance() {
125+
SavingsAccountYear thisYear = newAccount();
126+
assertEquals(thisYear.endingBalance(25), thisYear.nextYear(25)
127+
.startingBalance());
128+
}
129+
130+
@Test
131+
public void nextYearsInterestRateEqualsThisYearsInterestRate() {
132+
SavingsAccountYear thisYear = newAccount();
133+
assertEquals(thisYear.interestRate(), thisYear.nextYear(25)
134+
.interestRate());
135+
}
136+
98137
private SavingsAccountYear newAccount() {
99-
SavingsAccountYear account = new SavingsAccountYear(10000, 10);
138+
SavingsAccountYear account = new SavingsAccountYear(10000, 10000, 10);
100139
return account;
101140
}
102141

0 commit comments

Comments
 (0)