Calculator with Undo in Java

Calculator with Undo in Java Homework Sample

You need to write a calculator that allows you to undo operations. The program does not need to be a GUI based program, but should accept console input. It should support +,-, *, store and restore, and only needs to handle integer values. You need to interface with the supplied code for Calculator and CalculatorCommand. For more Java programming assignments contact us for a quote.

Solution:

AppendDigitCommand.java

package lab6;

public class AppendDigitCommand extends CalculatorCommand {

/**
* internal character storage
*/
private char charData;

public AppendDigitCommand(Calculator c) {
super(c);
}

/**
* set the char
*
* @param charData the charData to set
*/
public void setCharData(char charData) {
this.charData = charData;
}

/**
* @return the charData
*/
public char getCharData() {
return charData;
}

public void execute() {
calculator.appendDigit(charData);
}

public void unexecute() {
if(!Character.isDigit(charData)) {
return;
}
// remove the charData from the end of the display of the calculator
String display = calculator.getDisplay();
display = display.substring(0, display.length()-1);
calculator.setState(display, null, null, false);
}
}
ClearCommand.java

package lab6;

public class ClearCommand extends CalculatorCommand {

/**
* Storage for saving the last state
*/
private String display, accumulator, memory;
private boolean newNumber;

public ClearCommand(Calculator c) {
super(c);
}

/**
* @param accumulator the accumulator to set
*/
public void setAccumulator(String accumulator) {
this.accumulator = accumulator;
}

/**
* @return the accumulator
*/
public String getAccumulator() {
return accumulator;
}

/**
* @param display the display to set
*/
public void setDisplay(String display) {
this.display = display;
}

/**
* @return the display
*/
public String getDisplay() {
return display;
}

/**
* @param memory the memory to set
*/
public void setMemory(String memory) {
this.memory = memory;
}

/**
* @return the memory
*/
public String getMemory() {
return memory;
}
/**
* @param newNumber the newNumber to set
*/
public void setNewNumber(boolean newNumber) {
this.newNumber = newNumber;
}

/**
* @return the newNumber
*/
public boolean getNewNumber() {
return newNumber;
}

public void execute() {
display = calculator.getDisplay();
memory = calculator.getMemory();
accumulator = calculator.getAccumulator();
newNumber = calculator.getNewNumber();

calculator.clear();
}

public void unexecute() {
calculator.setState(display, memory, accumulator, newNumber);
}
}

EnterCommand.java

package lab6;

public class EnterCommand extends CalculatorCommand {

/**
* storage for previous accumulator and newNumber
*/
private String accumulator;
private boolean newNumber;

public EnterCommand(Calculator c) {
super(c);
}

/**
* @param newNmber the newNmber to set
*/
public void setNewNmber(boolean newNmber) {
this.newNumber = newNmber;
}

/**
* @return the newNmber
*/
public boolean getNewNmber() {
return newNumber;
}

/**
* @return the accumulator
*/
public String getAccumulator() {
return accumulator;
}

/**
* @param accumulator the accumulator to set
*/
public void setAccumulator(String accumulator) {
this.accumulator = accumulator;
}

public void execute() {
accumulator = calculator.getAccumulator();
newNumber = calculator.getNewNumber();

calculator.enter();
}

public void unexecute() {
calculator.setState(null, null, accumulator, newNumber);
}
}

Main.java

package lab6;

import java.util.Scanner;

/**
* Interface to calculator, taking commands (see the following code) and
* converting them to calculator operations.
*
* @author R. Hasker
* @date Jan. 2019
*/
public class Main {

/**
* Display user-visible status of calculator. Shows stored memory value if it is
* not 0.
*
* @param c calculator to display
*/
public static void show(Calculator c) {
String display = “Display: ” + c.result();
if (!c.getMemory().equals(“0″))
display += ” [Mem: ” + c.getMemory() + “]”;
System.out.println(display);
}

/**
* Main loop driving calculator: turns input into calculator commands This
* procedure is long because it encapsulates a large case statement.
*
* @param args ignored
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
in.useDelimiter(“\\s*”); // treat whitespace as a word separator
Calculator calculator = new Calculator();
History history = new History();

boolean doContinue = true;
while (doContinue && in.hasNext()) {
String text = in.next();
for (char c : text.toCharArray()) {
if (Character.isDigit(c)) {
// calculator.appendDigit(c);
AppendDigitCommand ac = new AppendDigitCommand(calculator);
ac.setCharData(c);
history.doCommand(ac);
} else {
switch (Character.toLowerCase(c)) {
case ‘q’:
System.out.println(“Quit”);
doContinue = false;
break;
case ‘+’:
System.out.println(“Add”);
PlusCommand pc = new PlusCommand(calculator);
history.doCommand(pc);
// calculator.plus();
show(calculator);
break;
case ‘-‘:
System.out.println(“Subtract”);
MinusCommand mc = new MinusCommand(calculator);
history.doCommand(mc);
// calculator.minus();
show(calculator);
break;
case ‘*’:
System.out.println(“Multiply”);
TimesCommand tc = new TimesCommand(calculator);
history.doCommand(tc);
// calculator.times();
show(calculator);
break;
case ‘,’:
EnterCommand ec = new EnterCommand(calculator);
history.doCommand(ec);
// calculator.enter();
show(calculator);
break;
case ‘s’:
System.out.println(“Save”);
SaveCommand sc = new SaveCommand(calculator);
history.doCommand(sc);
// calculator.saveToMemory();
show(calculator);
break;
case ‘r’:
System.out.println(“Recall”);
// calculator.recallFromMemory();
RecallCommand rc = new RecallCommand(calculator);
history.doCommand(rc);
show(calculator);
break;
case ‘c’:
System.out.println(“Clear”);
ClearCommand cc = new ClearCommand(calculator);
history.doCommand(cc);
// calculator.clear();
show(calculator);
break;
case ‘u’:
System.out.println(“Undo”);
history.undo();
show(calculator);
break;
case ‘d’:
System.out.println(“Redo”);
history.redo();
show(calculator);
break;
default:
if (!Character.isISOControl(c))
System.out.println(“Unrecognized command `” + c + “‘”);
}
}
}
}
// end of loop over input
System.out.println(“Final result: ” + calculator.result());
}
}

MinusCommand.java

package lab6;

public class MinusCommand extends CalculatorCommand {

/**
* storage for previous state
*/
private String accumulator, display;
private boolean newNumber;

public MinusCommand(Calculator c) {
super(c);
}

/**
* @param accumulator the accumulator to set
*/
public void setAccumulator(String accumulator) {
this.accumulator = accumulator;
}

/**
* @return the accumulator
*/
public String getAccumulator() {
return accumulator;
}

/**
* @param display the display to set
*/
public void setDisplay(String display) {
this.display = display;
}

/**
* @return the display
*/
public String getDisplay() {
return display;
}

/**
* @param newNumber the newNumber to set
*/
public void setNewNumber(boolean newNumber) {
this.newNumber = newNumber;
}

/**
* @return the newNumber
*/
public boolean getNewNumber() {
return newNumber;
}

public void execute() {
accumulator = calculator.getAccumulator();
display = calculator.getDisplay();
newNumber = calculator.getNewNumber();

calculator.minus();
}

public void unexecute() {
calculator.setState(display, null, accumulator, newNumber);
}
}

PlusCommand.java

package lab6;

public class PlusCommand extends CalculatorCommand {

/**
* storage for previous state
*/
private String accumulator, display;
private boolean newNumber;

public PlusCommand(Calculator c) {
super(c);
}

/**
* @param accumulator the accumulator to set
*/
public void setAccumulator(String accumulator) {
this.accumulator = accumulator;
}

/**
* @return the accumulator
*/
public String getAccumulator() {
return accumulator;
}

/**
* @param display the display to set
*/
public void setDisplay(String display) {
this.display = display;
}

/**
* @return the display
*/
public String getDisplay() {
return display;
}

/**
* @param newNumber the newNumber to set
*/
public void setNewNumber(boolean newNumber) {
this.newNumber = newNumber;
}

/**
* @return the newNumber
*/
public boolean getNewNumber() {
return newNumber;
}

public void execute() {
accumulator = calculator.getAccumulator();
display = calculator.getDisplay();
newNumber = calculator.getNewNumber();

calculator.plus();
}

public void unexecute() {
calculator.setState(display, null, accumulator, newNumber);
}
}

RecallCommand.java

package lab6;

public class RecallCommand extends CalculatorCommand {

/**
* storage for previous state
*/
private String display;
private boolean newNumber;

public RecallCommand(Calculator c) {
super(c);
}

/**
* @param display the display to set
*/
public void setDisplay(String display) {
this.display = display;
}

/**
* @param newNumber the newNumber to set
*/
public void setNewNumber(boolean newNumber) {
this.newNumber = newNumber;
}

/**
* @return the display
*/
public String getDisplay() {
return display;
}

/**
* @return the newNumber
*/
public boolean getNewNumber() {
return newNumber;
}

public void execute() {
display = calculator.getDisplay();
newNumber = calculator.getNewNumber();

calculator.recallFromMemory();
}

public void unexecute() {
calculator.setState(display, null, null, newNumber);
}
}

SaveCommand.java

package lab6;

public class SaveCommand extends CalculatorCommand {

/**
* Storage for saving last memory state.
*/
private String memory, accumulator;
private boolean newNumber;

public SaveCommand(Calculator c) {
super(c);
}

/**
* @param newNumber the newNumber to set
*/
public void setNewNumber(boolean newNumber) {
this.newNumber = newNumber;
}

/**
* @return the newNumber
*/
public boolean getNewNumber() {
return newNumber;
}

/**
* @param memory the memory to set
*/
public void setMemory(String memory) {
this.memory = memory;
}

/**
* @return the memory
*/
public String getMemory() {
return memory;
}

/**
* @param accumulator the accumulator to set
*/
public void setAccumulator(String accumulator) {
this.accumulator = accumulator;
}

/**
* @return the accumulator
*/
public String getAccumulator() {
return accumulator;
}

public void execute() {
accumulator = calculator.getAccumulator();
memory = calculator.getMemory();
newNumber = calculator.getNewNumber();

calculator.saveToMemory();
}

public void unexecute() {
calculator.setState(null, memory, accumulator, newNumber);
}
}

TimesCommand.java

package lab6;

public class TimesCommand extends CalculatorCommand {

/**
* storage for previous state
*/
private String accumulator, display;
private boolean newNumber;

public TimesCommand(Calculator c) {
super(c);
}

/**
* @param accumulator the accumulator to set
*/
public void setAccumulator(String accumulator) {
this.accumulator = accumulator;
}

/**
* @return the accumulator
*/
public String getAccumulator() {
return accumulator;
}

/**
* @param display the display to set
*/
public void setDisplay(String display) {
this.display = display;
}

/**
* @return the display
*/
public String getDisplay() {
return display;
}

/**
* @param newNumber the newNumber to set
*/
public void setNewNumber(boolean newNumber) {
this.newNumber = newNumber;
}

/**
* @return the newNumber
*/
public boolean getNewNumber() {
return newNumber;
}

public void execute() {
accumulator = calculator.getAccumulator();
display = calculator.getDisplay();
newNumber = calculator.getNewNumber();

calculator.times();
}

public void unexecute() {
calculator.setState(display, null, accumulator, newNumber);
}
}