diff --git a/cs623/proj1/Makefile b/cs623/proj1/Makefile index 74533ea..1ce1411 100644 --- a/cs623/proj1/Makefile +++ b/cs623/proj1/Makefile @@ -2,7 +2,7 @@ MAINCLASS := RetirementCalculator all: - javac $(MAINCLASS).java + javac *.java .PHONY: run run: diff --git a/cs623/proj1/RetirementCalculator.java b/cs623/proj1/RetirementCalculator.java index 02a8cfe..2bea63b 100644 --- a/cs623/proj1/RetirementCalculator.java +++ b/cs623/proj1/RetirementCalculator.java @@ -13,6 +13,29 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener { private RetirementCalculatorModel myModel; + private HashMap 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)); diff --git a/cs623/proj1/RetirementCalculatorModel.java b/cs623/proj1/RetirementCalculatorModel.java index 66e2336..44cbd93 100644 --- a/cs623/proj1/RetirementCalculatorModel.java +++ b/cs623/proj1/RetirementCalculatorModel.java @@ -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 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;