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
all:
javac $(MAINCLASS).java
javac *.java
.PHONY: run
run:

View File

@ -13,6 +13,29 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener
{
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()
{
JPanel inner = new JPanel(new GridLayout(3, 2));

View File

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