Compare commits
1 Commits
main
...
lab3-start
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c35462b8e3 |
@ -0,0 +1,19 @@
|
||||
package io.cloudops.freight.ops;
|
||||
|
||||
/** Binary addition operator used to add fixed surcharges in Challenge Lab 3. */
|
||||
public class AddSurchargeOp {
|
||||
|
||||
/** Precedence level lower than multiplication. */
|
||||
public static final int PRECEDENCE = 1;
|
||||
|
||||
/**
|
||||
* Adds two monetary or numeric operands.
|
||||
*
|
||||
* @param left left-hand operand
|
||||
* @param right right-hand operand
|
||||
* @return sum of operands
|
||||
*/
|
||||
public double apply(final double left, final double right) {
|
||||
return left + right;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package io.cloudops.freight.ops;
|
||||
|
||||
/** Binary multiplication operator used for zone/weight rates in Challenge Lab 3. */
|
||||
public class MultiplyRateOp {
|
||||
|
||||
/** Higher precedence level than additive surcharge operators. */
|
||||
public static final int PRECEDENCE = 2;
|
||||
|
||||
/**
|
||||
* Multiplies two numeric operands.
|
||||
*
|
||||
* @param left left-hand operand
|
||||
* @param right right-hand operand
|
||||
* @return product of operands
|
||||
*/
|
||||
public double apply(final double left, final double right) {
|
||||
return left * right;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
package io.cloudops.freight.ops;
|
||||
|
||||
/**
|
||||
* Starter left-to-right tariff formula evaluator for Challenge Lab 3 (TDD).
|
||||
*
|
||||
* <p>Currently ignores operator precedence ({@code +} vs {@code *}); refactor via TDD
|
||||
* so multiplication binds tighter than addition.
|
||||
*/
|
||||
public class TariffFormulaEvaluator {
|
||||
|
||||
private final AddSurchargeOp addOp = new AddSurchargeOp();
|
||||
private final MultiplyRateOp multiplyOp = new MultiplyRateOp();
|
||||
|
||||
/**
|
||||
* Evaluates a space-delimited arithmetic formula such as {@code "5 + 3 * 2.5"}.
|
||||
*
|
||||
* @param formula space-separated tokens
|
||||
* @return evaluated total
|
||||
*/
|
||||
public double evaluate(final String formula) {
|
||||
if (formula == null || formula.isBlank()) {
|
||||
throw new IllegalArgumentException("Formula must not be blank");
|
||||
}
|
||||
String[] tokens = formula.trim().split("\\s+");
|
||||
if (tokens.length % 2 == 0) {
|
||||
throw new IllegalArgumentException("Malformed formula: " + formula);
|
||||
}
|
||||
|
||||
double acc = Double.parseDouble(tokens[0]);
|
||||
for (int i = 1; i < tokens.length; i += 2) {
|
||||
String op = tokens[i];
|
||||
double rhs = Double.parseDouble(tokens[i + 1]);
|
||||
acc = switch (op) {
|
||||
case "+" -> addOp.apply(acc, rhs);
|
||||
case "*" -> multiplyOp.apply(acc, rhs);
|
||||
default -> throw new IllegalArgumentException("Unsupported operator: " + op);
|
||||
};
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
package io.cloudops.freight.ops;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Starter failing RED test for Challenge Lab 3 TDD precedence refactoring. */
|
||||
class TariffFormulaEvaluatorTest {
|
||||
|
||||
private final TariffFormulaEvaluator evaluator = new TariffFormulaEvaluator();
|
||||
|
||||
@Test
|
||||
void multiplicationTakesPrecedenceOverAddition() {
|
||||
// Expected: 5 + (3 * 4) = 17.0; starter left-to-right evaluator yields 32.0 (RED).
|
||||
assertEquals(17.0d, evaluator.evaluate("5 + 3 * 4"), 0.0001d);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user