Skip to content

Commit

Permalink
End of episode 204
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesshore committed Sep 7, 2012
1 parent 2562570 commit 8b00155
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 30 deletions.
1 change: 1 addition & 0 deletions scratchpad.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Use Observer pattern between ApplicationModel & UserConfiguration?
-- Implement our own observers?
--> Change ConfigurationPanel to take UserConfiguration rather than ApplicationModel (it won't need the latter)

== Finish basic saving ==
Expand Down
55 changes: 48 additions & 7 deletions src/com/jamesshore/finances/persistence/UserConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jamesshore.finances.persistence;

import java.io.*;
import com.jamesshore.finances.util.*;
import com.jamesshore.finances.values.*;

public class UserConfiguration {
Expand All @@ -11,12 +12,12 @@ public class UserConfiguration {
public static final UserEnteredDollars DEFAULT_STARTING_COST_BASIS = new UserEnteredDollars("7000");
public static final UserEnteredDollars DEFAULT_YEARLY_SPENDING = new UserEnteredDollars("695");

private Observer observer = null;
private UserEnteredDollars startingBalance = DEFAULT_STARTING_BALANCE;
private UserEnteredDollars startingCostBasis = DEFAULT_STARTING_COST_BASIS;
private UserEnteredDollars yearlySpending = DEFAULT_YEARLY_SPENDING;
private File path = null;

public UserEnteredDollars startingBalance = DEFAULT_STARTING_BALANCE;
public UserEnteredDollars startingCostBasis = DEFAULT_STARTING_COST_BASIS;
public UserEnteredDollars yearlySpending = DEFAULT_YEARLY_SPENDING;

public File lastSavedPathOrNullIfNeverSaved() {
return path;
}
Expand All @@ -32,9 +33,9 @@ private void writeFile(File path) throws IOException {
Writer writer = new BufferedWriter(new FileWriter(path));
try {
writeLine(writer, "com.jamesshore.finances,1");
writeLine(writer, startingBalance.getUserText());
writeLine(writer, startingCostBasis.getUserText());
writeLine(writer, yearlySpending.getUserText());
writeLine(writer, getStartingBalance().getUserText());
writeLine(writer, getStartingCostBasis().getUserText());
writeLine(writer, getYearlySpending().getUserText());
}
finally {
writer.close();
Expand All @@ -46,4 +47,44 @@ private void writeLine(Writer writer, String line) throws IOException {
line = line.replace("\n", "\\n");
writer.write(line + "\n");
}

public void setStartingBalance(UserEnteredDollars startingBalance) {
this.startingBalance = startingBalance;
notifyObserver();
}

public UserEnteredDollars getStartingBalance() {
return startingBalance;
}

public void setStartingCostBasis(UserEnteredDollars startingCostBasis) {
this.startingCostBasis = startingCostBasis;
notifyObserver();
}

public UserEnteredDollars getStartingCostBasis() {
return startingCostBasis;
}

public void setYearlySpending(UserEnteredDollars yearlySpending) {
this.yearlySpending = yearlySpending;
notifyObserver();
}

public UserEnteredDollars getYearlySpending() {
return yearlySpending;
}

public void setObserver(Observer observer) {
Require.that(this.observer == null, "Tried to set a second observer; only one is supported so far");
this.observer = observer;
}

private void notifyObserver() {
if (observer != null) observer.configurationUpdated();
}

public interface Observer {
public void configurationUpdated();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.*;
import org.junit.*;
import org.junit.rules.*;
import com.jamesshore.finances.util.*;
import com.jamesshore.finances.values.*;

public class _UserConfigurationTest {
Expand All @@ -23,9 +24,52 @@ public void setup() {

@Test
public void shouldHaveDefaults() {
assertEquals("starting balance", UserConfiguration.DEFAULT_STARTING_BALANCE, userConfiguration.startingBalance);
assertEquals("starting cost basis", UserConfiguration.DEFAULT_STARTING_COST_BASIS, userConfiguration.startingCostBasis);
assertEquals("yearly spending", UserConfiguration.DEFAULT_YEARLY_SPENDING, userConfiguration.yearlySpending);
assertEquals("starting balance", UserConfiguration.DEFAULT_STARTING_BALANCE, userConfiguration.getStartingBalance());
assertEquals("starting cost basis", UserConfiguration.DEFAULT_STARTING_COST_BASIS, userConfiguration.getStartingCostBasis());
assertEquals("yearly spending", UserConfiguration.DEFAULT_YEARLY_SPENDING, userConfiguration.getYearlySpending());
}

class TestObserver implements UserConfiguration.Observer {
public boolean updateTriggered = false;

@Override
public void configurationUpdated() {
updateTriggered = true;
}
}

@Test(expected = RequireException.class)
public void mayBeObservedButOnlyByOneObserverAtATime() {
TestObserver observer = new TestObserver();
userConfiguration.setObserver(observer);
userConfiguration.setObserver(observer);
}

@Test
public void observerNotifiedWhenStartingBalanceChanges() {
TestObserver observer = new TestObserver();

userConfiguration.setObserver(observer);
userConfiguration.setStartingBalance(new UserEnteredDollars("333"));
assertTrue("observer should be notified", observer.updateTriggered);
}

@Test
public void observerNotifiedWhenCostBasisChanges() {
TestObserver observer = new TestObserver();

userConfiguration.setObserver(observer);
userConfiguration.setStartingCostBasis(new UserEnteredDollars("333"));
assertTrue("observer should be notified", observer.updateTriggered);
}

@Test
public void observerNotifiedWhenYearlySpendingChanges() {
TestObserver observer = new TestObserver();

userConfiguration.setObserver(observer);
userConfiguration.setYearlySpending(new UserEnteredDollars("333"));
assertTrue("observer should be notified", observer.updateTriggered);
}

@Test
Expand Down Expand Up @@ -91,9 +135,9 @@ private void doSave() throws IOException {
}

private void doSave(UserEnteredDollars startingBalance, UserEnteredDollars costBasis, UserEnteredDollars yearlySpending) throws IOException {
userConfiguration.startingBalance = startingBalance;
userConfiguration.startingCostBasis = costBasis;
userConfiguration.yearlySpending = yearlySpending;
userConfiguration.setStartingBalance(startingBalance);
userConfiguration.setStartingCostBasis(costBasis);
userConfiguration.setYearlySpending(yearlySpending);

userConfiguration.save(path);
}
Expand Down
9 changes: 6 additions & 3 deletions src/com/jamesshore/finances/ui/ApplicationModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import java.io.*;
import com.jamesshore.finances.domain.*;
import com.jamesshore.finances.persistence.*;
import com.jamesshore.finances.persistence.UserConfiguration.Observer;
import com.jamesshore.finances.values.*;

public class ApplicationModel {
public class ApplicationModel implements Observer {

public static final Year DEFAULT_STARTING_YEAR = new Year(2010);
public static final Year DEFAULT_ENDING_YEAR = new Year(2050);
Expand All @@ -22,6 +23,7 @@ public class ApplicationModel {

public ApplicationModel() {
this.configuration = new UserConfiguration();
this.configuration.setObserver(this);
this.stockMarketTableModel = new StockMarketTableModel(stockMarketProjection());
}

Expand All @@ -33,13 +35,14 @@ public StockMarketTableModel stockMarketTableModel() {
return stockMarketTableModel;
}

@Override
public void configurationUpdated() {
stockMarketTableModel.setProjection(stockMarketProjection());
}

public StockMarketProjection stockMarketProjection() {
StockMarketYear firstYear = new StockMarketYear(startingYear, configuration.startingBalance, configuration.startingCostBasis, growthRate, capitalGainsTaxRate);
return new StockMarketProjection(firstYear, endingYear, configuration.yearlySpending);
StockMarketYear firstYear = new StockMarketYear(startingYear, configuration.getStartingBalance(), configuration.getStartingCostBasis(), growthRate, capitalGainsTaxRate);
return new StockMarketProjection(firstYear, endingYear, configuration.getYearlySpending());
}

public void save(File path) throws IOException {
Expand Down
12 changes: 6 additions & 6 deletions src/com/jamesshore/finances/ui/ConfigurationPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,32 @@ private void addField(String name, DollarsTextField field) {
}

public DollarsTextField startingBalanceField() {
final DollarsTextField field = new DollarsTextField(userConfiguration.startingBalance);
final DollarsTextField field = new DollarsTextField(userConfiguration.getStartingBalance());
field.addTextChangeListener(new ChangeListener() {
public void textChanged() {
userConfiguration.startingBalance = field.getDollars();
userConfiguration.setStartingBalance(field.getDollars());
applicationModel.configurationUpdated();
}
});
return field;
}

private DollarsTextField costBasisField() {
final DollarsTextField field = new DollarsTextField(userConfiguration.startingCostBasis);
final DollarsTextField field = new DollarsTextField(userConfiguration.getStartingCostBasis());
field.addTextChangeListener(new ChangeListener() {
public void textChanged() {
userConfiguration.startingCostBasis = field.getDollars();
userConfiguration.setStartingCostBasis(field.getDollars());
applicationModel.configurationUpdated();
}
});
return field;
}

private DollarsTextField yearlySpendingField() {
final DollarsTextField field = new DollarsTextField(userConfiguration.yearlySpending);
final DollarsTextField field = new DollarsTextField(userConfiguration.getYearlySpending());
field.addTextChangeListener(new ChangeListener() {
public void textChanged() {
userConfiguration.yearlySpending = field.getDollars();
userConfiguration.setYearlySpending(field.getDollars());
applicationModel.configurationUpdated();
}
});
Expand Down
3 changes: 1 addition & 2 deletions src/com/jamesshore/finances/ui/_ApplicationModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public void shouldOnlyHaveOneInstanceOfStockMarketTableModel() {

@Test
public void configurationUpdated_ResultsInStockMarketTableModelChanging() {
configuration.yearlySpending = new UserEnteredDollars("423");
model.configurationUpdated();
configuration.setYearlySpending(new UserEnteredDollars("423"));
assertEquals("stock market table model", new ValidDollars(423), model.stockMarketTableModel().yearlySpending());
}

Expand Down
12 changes: 6 additions & 6 deletions src/com/jamesshore/finances/ui/_ConfigurationPanelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ private void assertFormField(String message, Component label, Component field) {

@Test
public void fieldsInitializeToModelValue() {
assertEquals("starting balance field text", userConfiguration.startingBalance, startingBalanceField().getDollars());
assertEquals("cost basis field text", userConfiguration.startingCostBasis, costBasisField().getDollars());
assertEquals("yearly spending field text", userConfiguration.yearlySpending, yearlySpendingField().getDollars());
assertEquals("starting balance field text", userConfiguration.getStartingBalance(), startingBalanceField().getDollars());
assertEquals("cost basis field text", userConfiguration.getStartingCostBasis(), costBasisField().getDollars());
assertEquals("yearly spending field text", userConfiguration.getYearlySpending(), yearlySpendingField().getDollars());
}

@Test
Expand All @@ -67,7 +67,7 @@ public void startingBalanceFieldUpdatesModel() {
panel = new ConfigurationPanel(mockModel);

startingBalanceField().setText("668");
assertEquals("user configuration should be updated", new ValidDollars(668), mockModel.userConfiguration().startingBalance);
assertEquals("user configuration should be updated", new ValidDollars(668), mockModel.userConfiguration().getStartingBalance());
assertTrue("applicationModel should be updated", mockModel.configurationUpdatedCalled);
}

Expand All @@ -77,7 +77,7 @@ public void costBasisFieldUpdatesModel() {
panel = new ConfigurationPanel(mockModel);

costBasisField().setText("670");
assertEquals("user configuration should be updated", new ValidDollars(670), mockModel.userConfiguration().startingCostBasis);
assertEquals("user configuration should be updated", new ValidDollars(670), mockModel.userConfiguration().getStartingCostBasis());
assertTrue("applicationModel should be updated", mockModel.configurationUpdatedCalled);
}

Expand All @@ -87,7 +87,7 @@ public void yearlySpendingFieldUpdatesModel() {
panel = new ConfigurationPanel(mockModel);

yearlySpendingField().setText("672");
assertEquals("user configuration should be updated", new ValidDollars(672), mockModel.userConfiguration().yearlySpending);
assertEquals("user configuration should be updated", new ValidDollars(672), mockModel.userConfiguration().getYearlySpending());
assertTrue("applicationModel should be updated", mockModel.configurationUpdatedCalled);
}

Expand Down

0 comments on commit 8b00155

Please sign in to comment.