| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class CalculatorController implements ActionListener { CalculatorModel model; CalculatorView_2 view; public CalculatorController(CalculatorModel model, CalculatorView_2 view) { this.model = model; this.view = view; // Give the model a start situation as per what the GUI will show. model.setAnswer(0.0); model.setInitialNumber(0.0); // Add the action listener from this class on to the buttons of the view. view.buttonActionListeners(this); } // This deals with the interactions performed on the View. // It covers addition, subtraction, division and multiplication. public void actionPerformed(ActionEvent ae) { String action_com = ae.getActionCommand(); if(action_com.equals("+")) { model.doAddition(view.getFieldText()); } else if (action_com.equals("-")) { model.doSubtraction(view.getFieldText()); } else if (action_com.equals("*")) { model.doMultiply(view.getFieldText()); } else if (action_com.equals("/")) { model.doDivision(view.getFieldText()); } view.setFieldText(""+model.getAnswer()); |
| 9 10 11 12 13 14 15 16 17 18 19 20 | public CalculatorController(CalculatorModel model, CalculatorView_2 view) { this.model = model; this.view = view; // Give the model a start situation as per what the GUI will show. model.setAnswer(0.0); model.setInitialNumber(0.0); // Add the action listener from this class on to the buttons of the view. view.buttonActionListeners(this); } |
| 26 27 28 29 | String action_com = ae.getActionCommand(); if(action_com.equals("+")) { |
| 45 | view.setFieldText(""+model.getAnswer()); |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Calculator { public static void main(String[] args){ // Creates a model of the system logic. CalculatorModel model = new CalculatorModel(); // Creaties a view for the system logic. CalculatorView view = new CalculatorView(); // Creates a controller that links the two. CalculatorController controller = new CalculatorController(model, view); } } |