Implement calculator logic
This commit is contained in:
parent
84f0058272
commit
a0d1e71ed5
|
@ -74,8 +74,13 @@ public class Calc {
|
|||
* @throws IllegalStateException if the operand stack is empty
|
||||
*/
|
||||
public double performOperation(UnaryOperator<Double> op) throws IllegalStateException {
|
||||
// TODO
|
||||
return 0.0;
|
||||
// if (this.getOperandCount() == 0)
|
||||
if (this.operandStack.isEmpty())
|
||||
throw new IllegalStateException("Cannot perform operation " + op + " because of missing operands");
|
||||
|
||||
Double result = op.apply(this.popOperand());
|
||||
this.pushOperand(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -103,7 +108,13 @@ public class Calc {
|
|||
* @throws IllegalStateException if the operand count is less than two
|
||||
*/
|
||||
public void swap() {
|
||||
if (this.getOperandCount() < 2)
|
||||
throw new IllegalStateException("Not enough operands to swap");
|
||||
|
||||
Double x = this.popOperand();
|
||||
Double y = this.popOperand();
|
||||
this.pushOperand(x);
|
||||
this.pushOperand(y);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,6 +123,9 @@ public class Calc {
|
|||
* @throws IllegalStateException if the operand stack is empty
|
||||
*/
|
||||
public void dup() {
|
||||
// TODO
|
||||
if (this.operandStack.isEmpty())
|
||||
throw new IllegalStateException("Cannot duplicate top operand because there are no operands left");
|
||||
|
||||
this.pushOperand(this.peekOperand());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue