import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; /** * This is the controller for the Unit Converter application. */ public class UnitConvController { private UnitConvModel model; private UnitConvView view; /** * Initialises this controller with the passed model and view. * @param model the model to associate with this controller * @param view the view to associate with this controller */ public UnitConvController(UnitConvModel model, UnitConvView view) { this.model = model; this.view = view; this.view.addSaveListener(new SaveListener()); this.view.addCtoFListener(new CToFListener()); // TODO: Add listeners for reset and the 5 remaining buttons. ; } /** * Returns the model associated with this controller. * @return the model */ public UnitConvModel getModel() { return model; } /** * Returns the view associated with this controller. * @return the view */ public UnitConvView getView() { return view; } /** * Abstract base class for classes that listen for conversion events. */ private abstract class ConversionListener implements ActionListener { /** * Defines how to perform the conversion. * @param inputValue the value to convert */ protected abstract void doOperation(double inputValue); /** * Parses the user's input, delegates to doOperation, then updates * the result value. If the user's input is not parsable as a double, * use Double.NaN instead. */ @Override public void actionPerformed(ActionEvent action) { double userValue = Double.NaN; try { userValue = Double.parseDouble(getView().getInputValue()); } catch(NumberFormatException ex) {} doOperation(userValue); getView().setResultValue("" + getModel().getResultValue()); } } private class CToFListener extends ConversionListener { @Override protected void doOperation(double userValue) { getModel().cToF(userValue); } } private class FToCListener extends ConversionListener { @Override protected void doOperation(double userValue) { // TODO (1 line of code): Delegate conversion to the model. // Use CToFListener as a guide. ; } } private class CmToInchesListener extends ConversionListener { @Override protected void doOperation(double userValue) { // TODO (1 line of code): Delegate conversion to the model. // Use CToFListener as a guide. ; } } private class InchesToCmListener extends ConversionListener { @Override protected void doOperation(double userValue) { // TODO (1 line of code): Delegate conversion to the model. // Use CToFListener as a guide. ; } } private class KgToLbsListener extends ConversionListener { @Override protected void doOperation(double userValue) { // TODO (1 line of code): Delegate conversion to the model. // Use CToFListener as a guide. ; } } private class LbsToKgListener extends ConversionListener { @Override protected void doOperation(double userValue) { // TODO (1 line of code): Delegate conversion to the model. // Use CToFListener as a guide. ; } } /** * Abstract base class for classes that listen for menu events */ private abstract class MenuListener implements ActionListener { ; } private class SaveListener extends MenuListener { @Override public void actionPerformed(ActionEvent action) { File f = getView().getSaveFile(); getModel().save(f); } } private class ResetListener extends MenuListener { @Override public void actionPerformed(ActionEvent action) { // TODO (3 lines of code): // 1) Clear the model // 2) Set the input value view based on the model's current state // 3) Set the result value view based on the model's current state ; } } }