Skip to content

Instantly share code, notes, and snippets.

@iolson
Last active July 16, 2024 14:29
Show Gist options
  • Save iolson/6d5c1d185ebf1dd59f574afb46fd0a19 to your computer and use it in GitHub Desktop.
Save iolson/6d5c1d185ebf1dd59f574afb46fd0a19 to your computer and use it in GitHub Desktop.
/*
* Click `Run` to execute the snippet below!
*/
import java.io.*;
import java.util.*;
/**
* Design a calculator with the following features:
*
* * Add
* * Subtract
* * Multiply
* * Divide
* * Redo (Perform the most recent operation again)
* * Undo (Revert the most recent operation)
*
* If you need more classes, simply define them inline.
*
* You can use System.out.println() to show the output of each operation.
* You do NOT need to use a CLI unless you feel you want to.
*/
class Solution {
public static void main(String[] args) {
// ADD 5 (Ouput should be 5)
// ADD 5 (Output should be 10)
// SUBTRACT 3 (Output should be 7)
// UNDO (Output should be 10)
// REDO (Output should be 15)
// MULTIPLY by 2 (Output should be 30)
// DIVIDE by 3 (Output should be 10)
}
}
/*
* Click `Run` to execute the snippet below!
*/
import java.io.*;
import java.math.BigDecimal;
import java.util.*;
/**
* Design a calculator with the following features:
*
* * Add
* * Subtract
* * Multiply
* * Divide
* * Redo (Perform the most recent operation again)
* * Undo (Revert the most recent operation)
*
* If you need more classes, simply define them inline.
*/
class Solution {
public static void main(String[] args) {
Stack<Operation> stack = new Stack<Operation>();
System.out.println(add(BigDecimal.valueOf(5), stack));
System.out.println(add(BigDecimal.valueOf(5), stack));
System.out.println(subtract(BigDecimal.valueOf(3), stack));
System.out.println(undo(stack));
System.out.println(redo(stack));
System.out.println(multiply(BigDecimal.valueOf(2), stack));
System.out.println(divide(BigDecimal.valueOf(3), stack));
System.out.println(clear(stack));
System.out.println(undo(stack));
}
public static BigDecimal add(BigDecimal value, Stack<Operation> stack) {
return calculateNewTotal(stack, new Operation("add", value));
}
public static BigDecimal subtract(BigDecimal value, Stack<Operation> stack) {
return calculateNewTotal(stack, new Operation("subtract", value));
}
public static BigDecimal multiply(BigDecimal value, Stack<Operation>stack) {
return calculateNewTotal(stack, new Operation("multiply", value));
}
public static BigDecimal divide(BigDecimal value, Stack<Operation> stack) {
return calculateNewTotal(stack, new Operation("divide", value));
}
public static BigDecimal redo(Stack<Operation> stack) {
if (stack.isEmpty()) {
return BigDecimal.ZERO;
}
Operation lastOperation = stack.lastElement();
return calculateNewTotal(stack, lastOperation);
}
public static BigDecimal undo(Stack<Operation> stack) {
if (stack.isEmpty()) {
return BigDecimal.ZERO;
}
stack.pop();
if (stack.isEmpty()) {
return BigDecimal.ZERO;
}
return stack.lastElement().total;
}
public static BigDecimal clear(Stack<Operation> stack) {
stack.clear();
return BigDecimal.ZERO;
}
private static BigDecimal calculateNewTotal(Stack<Operation> stack, Operation op) {
BigDecimal currentTotal = BigDecimal.ZERO;
BigDecimal newTotal = BigDecimal.ZERO;
if (!stack.isEmpty()) {
currentTotal = stack.lastElement().total;
}
switch (op.operation) {
case "add":
newTotal = currentTotal.add(op.value);
break;
case "subtract":
newTotal = currentTotal.subtract(op.value);
break;
case "multiply":
newTotal = currentTotal.multiply(op.value);
break;
case "divide":
newTotal = currentTotal.divide(op.value);
break;
}
// Update operation total before sending adding it.
op.total = newTotal;
stack.add(op);
return newTotal;
}
}
class Operation {
public BigDecimal total;
public String operation;
public BigDecimal value;
public Operation(BigDecimal total, String operation, BigDecimal value) {
this.total = total;
this.operation = operation;
this.value = value;
}
public Operation(String operation, BigDecimal value) {
this.operation = operation;
this.value = value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment