[Sync Iteration] java/calculator-conundrum/1

This commit is contained in:
exercism-solutions-syncer[bot] 2026-04-21 10:03:28 +00:00 committed by GitHub
parent f80fbca12e
commit d4111001a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -0,0 +1,18 @@
class CalculatorConundrum {
public String calculate(int operand1, int operand2, String operation) {
switch (operation) {
case null : throw new IllegalArgumentException("Operation cannot be null");
case "" : throw new IllegalArgumentException("Operation cannot be empty");
case "+" : return operand1 + " + " + operand2 + " = " + (operand1 + operand2);
case "*" : return operand1 + " * " + operand2 + " = " + (operand1 * operand2);
case "/" :
try {
return operand1 + " / " + operand2 + " = " + (operand1 / operand2);
} catch (ArithmeticException e) {
throw new IllegalOperationException("Division by zero is not allowed", e);
}
default: throw new IllegalOperationException(String.format("Operation '%s' does not exist", operation));
}
}
}