import javax.swing.JPanel; import javax.swing.JComboBox; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class AgeChooser extends JPanel { // OVERVIEW: AgeChooser manipulates the interface for selecting ages // and maintains state for the current object type. // // When clicking on a spot on the grid, it will be set ages to selected. // Rep: private int selectedClass; private JComboBox combolist; private String[] classes; public String[] ages; public int age; AgeChooser() // EFFECT: Initializes this to an AgeChooser with no age. { selectedClass = 1; classes = null; combolist = new JComboBox(); combolist.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JComboBox cb = (JComboBox) e.getSource(); String classname = (String) cb.getSelectedItem(); setSelectedClass(classname); } }); combolist.addItem("Baby"); combolist.addItem("Adult"); combolist.addItem("Senior"); add(combolist); } public int getSelectedAge() // EFFECTS: Returns the currently selected class (possibly null). { return selectedClass; } public void setSelectedClass(String classname) // MODIFIES: this // EFFECTS: Sets the currently selected class to the class associated // with the button press (described by the ActionEvent). { int newClass = stringToInt(classname); selectedClass = newClass; } public int stringToInt(String c1) { //Effect: returns int value for string else 1; if (c1 == "Baby") return 1; if (c1 == "Adult") return 6; if (c1 == "Senior") return 16; return (1); } public void setAgeChooser(String c1) // MODIFIES: this // EFFECTS: Sets the currently selected class to the class associated // with the button press (described by the ActionEvent). { this.age = stringToInt(c1); System.err.println("Set aget to: " + this.age); //selectedClass = newClass; } }