migrated Model to a fields system instead of declaring every field with a separate class variable

git-svn-id: svn://anubis/gvsu@435 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2009-10-03 19:16:11 +00:00
parent dd5b4a7bf1
commit 8800104844
3 changed files with 51 additions and 22 deletions

View File

@ -2,7 +2,7 @@
MAINCLASS := RetirementCalculator MAINCLASS := RetirementCalculator
all: all:
javac $(MAINCLASS).java javac *.java
.PHONY: run .PHONY: run
run: run:

View File

@ -13,6 +13,29 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener
{ {
private RetirementCalculatorModel myModel; private RetirementCalculatorModel myModel;
private HashMap<String, Field> myFields;
public class Field extends JTextField
{
private String myName;
public Field(String name, String dflt)
{
super(dflt);
myName = name;
}
public String getName() { return myName; }
}
public void addField(JPanel addto, String caption, String name, String dflt)
{
addto.add(new JLabel(caption));
Field field = new Field(name, dflt);
myFields.put(name, field);
addto.add(field);
}
public RetirementCalculatorPanel() public RetirementCalculatorPanel()
{ {
JPanel inner = new JPanel(new GridLayout(3, 2)); JPanel inner = new JPanel(new GridLayout(3, 2));

View File

@ -4,37 +4,42 @@ import java.util.*;
public class RetirementCalculatorModel public class RetirementCalculatorModel
{ {
/* input fields */ /* input fields */
private double myCurrentIncome; private HashMap<String, Double> myFields;
private double myIncomeWanted;
private double myEmployerPension;
private double myPartTimeIncome;
private double myOtherIncome;
private double myAge;
private double myRetirementAge;
private double mySavings;
/* calculated fields */
private double mySocialSecurity;
private double myRetirementIncome;
public RetirementCalculatorModel() public RetirementCalculatorModel()
{ {
reset(); reset();
} }
public void setField(String name, double value)
{
myFields.put(name, value);
}
public double getField(String name)
{
return (myFields.containsKey(name)) ? myFields.get(name) : 0;
}
public void reset() public void reset()
{ {
myIncomeWanted = 0; myFields.clear();
myEmployerPension = 0;
myPartTimeIncome = 0; setField("current income", 0);
myOtherIncome = 0; setField("income wanted", 0);
myAge = 30; setField("employer pension", 0);
myRetirementAge = 65; setField("part time income", 0);
setField("other income", 0);
setField("age", 30);
setField("retirement age", 65);
setField("savings", 0);
calculate();
} }
public void calculate() public void calculate()
{ {
mySocialSecurity = calculateSocialSecurity(); setField("social security", calculateSocialSecurity());
} }
private double calculateRetirementFactor() private double calculateRetirementFactor()
@ -44,9 +49,10 @@ public class RetirementCalculatorModel
private double calculateSocialSecurity() private double calculateSocialSecurity()
{ {
if (myCurrentIncome < 25000.0) double current_income = getField("current income");
if (current_income < 25000.0)
return 8000.0; return 8000.0;
else if (myCurrentIncome <= 40000.0) else if (current_income <= 40000.0)
return 12000.0; return 12000.0;
else else
return 14500.0; return 14500.0;