filled in calculate() and reset() to synchronize View to Model

git-svn-id: svn://anubis/gvsu@446 45c1a28c-8058-47b2-ae61-ca45b979098e
This commit is contained in:
josh 2009-10-05 02:59:06 +00:00
parent edfc5a9b31
commit 1bbc1a7c6e

View File

@ -21,6 +21,35 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener
private JButton myResetButton;
private JLabel myResultBox;
class ComboChoice
{
private String myDesc;
private int myVal;
ComboChoice(int val, String desc)
{
myDesc = desc;
myVal = val;
}
public String toString() { return myDesc; }
public int getVal() { return myVal; }
}
private final ComboChoice[] retirement_age_choices = {
new ComboChoice(55, "55"),
new ComboChoice(60, "60"),
new ComboChoice(65, "65"),
new ComboChoice(70, "70")
};
private final ComboChoice[] life_expectancy_choices = {
new ComboChoice(82, "82 (Male; 50th percentile)"),
new ComboChoice(86, "86 (Female; 50th percentile)"),
new ComboChoice(89, "89 (Male; 75th percentile)"),
new ComboChoice(92, "92 (Female; 75th percentile)"),
new ComboChoice(94, "94 (Male; 90th percentile)"),
new ComboChoice(97, "97 (Female; 90th percentile)")
};
public RetirementCalculatorPanel()
{
super(new BorderLayout());
@ -28,18 +57,6 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener
myModel = new RetirementCalculatorModel();
myFields = new HashMap<String, JComponent>();
JPanel inner = new JPanel(new GridBagLayout());
class ComboChoice
{
private String myDesc;
private int myVal;
ComboChoice(int val, String desc)
{
myDesc = desc;
myVal = val;
}
public String toString() { return myDesc; }
public int getVal() { return myVal; }
}
JComponent field;
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
@ -139,12 +156,6 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener
SwingConstants.RIGHT), c);
c.gridx++;
c.weightx = 1.0;
ComboChoice[] retirement_age_choices = {
new ComboChoice(55, "55"),
new ComboChoice(60, "60"),
new ComboChoice(65, "65"),
new ComboChoice(70, "70")
};
field = new JComboBox(retirement_age_choices);
myFields.put("retirement age", field);
inner.add(field, c);
@ -155,14 +166,6 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener
p.setAlignmentX(Component.RIGHT_ALIGNMENT);
p.add(new JLabel("What is your life expectancy?",
SwingConstants.RIGHT), BorderLayout.CENTER);
ComboChoice[] life_expectancy_choices = {
new ComboChoice(82, "82 (Male; 50th percentile)"),
new ComboChoice(86, "86 (Female; 50th percentile)"),
new ComboChoice(89, "89 (Male; 75th percentile)"),
new ComboChoice(92, "92 (Female; 75th percentile)"),
new ComboChoice(94, "94 (Male; 90th percentile)"),
new ComboChoice(97, "97 (Female; 90th percentile)")
};
field = new JComboBox(life_expectancy_choices);
myFields.put("life expectancy", field);
p.add(field, BorderLayout.EAST);
@ -206,6 +209,8 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
add(scrollpane);
reset();
}
public void actionPerformed(ActionEvent e)
@ -220,12 +225,64 @@ class RetirementCalculatorPanel extends JPanel implements ActionListener
private void calculate()
{
Iterator it = myFields.keySet().iterator();
while (it.hasNext())
{
String key = (String) it.next();
JComponent comp = myFields.get(key);
if (comp.getClass() == JTextField.class)
{
JTextField tf = (JTextField) comp;
myModel.setField(key, Double.parseDouble(tf.getText()));
}
else if (comp.getClass() == JComboBox.class)
{
JComboBox cb = (JComboBox) comp;
myModel.setField(key,
((ComboChoice) cb.getSelectedItem()).getVal());
}
}
myModel.calculate();
myResultBox.setText("The total amount of savings needed at retirement is $" + myModel.getField("total additional savings needed") + ". The annual amount of savings needed is $" + myModel.getField("annual savings needed"));
}
private void reset()
{
myModel.reset();
Iterator it = myFields.keySet().iterator();
while (it.hasNext())
{
String key = (String) it.next();
JComponent comp = myFields.get(key);
if (comp.getClass() == JTextField.class)
{
JTextField tf = (JTextField) comp;
double val = myModel.getField(key);
tf.setText((new Double(val)).toString());
}
else if (comp.getClass() == JComboBox.class)
{
JComboBox cb = (JComboBox) comp;
int val = (int) myModel.getField(key);
ComboChoice[] ccp = null;
if (key.equals("retirement age"))
ccp = retirement_age_choices;
else if (key.equals("life expectancy"))
ccp = life_expectancy_choices;
if (ccp != null)
{
for (ComboChoice cc : ccp)
{
if (cc.getVal() == val)
{
cb.setSelectedItem(cc);
break;
}
}
}
}
}
}
private void print()