/*
    CalculatorModel.java

    Calculator application Model portion of MVC pattern. Manages state of the calculator.
 */

package calculator;

public class CalculatorModel {

    private CalculatorView view;

    private long value;
    private long typed;
    private String display;
    private boolean overwrite;

    private boolean add;
    private boolean subtract;
    private boolean multiply;
    private boolean divide;

    CalculatorModel(CalculatorView view) {
        this.view = view;
        clear();
    }

    public void clear() {
        value = 0L;
        typed = 0L;
        display = "0";
        overwrite = true;

        add = false;
        subtract = false;
        multiply = false;
        divide = false;

        update(value);
    }

    public void equals() {
        if (add) {
            value += typed;
        } else if (subtract) {
            value -= typed;
        } else if (multiply) {
            value *= typed;
        } else if (divide) {
            value /= typed;
        }
        overwrite = true;
        update(value);
    }

    public void add() {
        operator();
        add = true;
    }

    public void subtract() {
        operator();
        subtract = true;
    }

    public void multiply() {
        operator();
        multiply = true;
    }

    public void divide() {
        operator();
        divide = true;
    }
    
    private void operator() {
        value = typed;
        overwrite = true;

        add = false;
        subtract = false;
        multiply = false;
        divide = false;
    }

    public void type(int input) {
        if (overwrite) {
            typed = (long) input;
            overwrite = false;
        } else {
            typed = 10L*typed + input;
        }

        update(typed);
    }

    private void update(long value) {
        view.setDisplay("" + value);
    }
}
@@ -310,16 +310,43 @@
 erwrite;
+%0A    private boolean fresh;
 %0A%0A    pr
@@ -652,32 +652,54 @@
 verwrite = true;
+%0A        fresh = true;
 %0A%0A        add = 
@@ -835,24 +835,130 @@
  equals() %7B%0A
+        evaluate();%0A        overwrite = true;%0A        update(value);%0A    %7D%0A%0A    private void evaluate() %7B%0A
         if (
@@ -1181,57 +1181,8 @@
   %7D%0A
-        overwrite = true;%0A        update(value);%0A
     
@@ -1540,22 +1540,126 @@
     
-value = typed;
+if (fresh) %7B %0A            value = typed;%0A            fresh = false;%0A        %7D else %7B%0A            evaluate();%0A        %7D
 %0A   
@@ -1958,32 +1958,32 @@
 *typed + input;%0A
+
         %7D%0A%0A     
@@ -1972,25 +1972,24 @@
 ;%0A        %7D%0A
-%0A
         upda

Correct incorrect behavior related to order in which operator buttons are pressed.