Merge pull request #86 from Weltenbummler397/exercism-sync/2e121aaacf2b47cc

[Sync Iteration] java/calculator-conundrum/1
This commit is contained in:
Weltenbummler397 2026-05-04 09:20:00 +02:00 committed by GitHub
commit 06fcc7ca81
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));
}
}
}