diff --git a/src/main/java/RewardValue.java b/src/main/java/RewardValue.java new file mode 100644 index 000000000..1a18ffe4d --- /dev/null +++ b/src/main/java/RewardValue.java @@ -0,0 +1,31 @@ +public class RewardValue { + private Double cash = null; + private Integer miles = null; + public static final double MILES_TO_CASH = 0.0035; + + public RewardValue(double cashValue) { + if (cashValue >= 0) + cash = cashValue; + else + throw new IllegalArgumentException("cash value must be positive"); + } + + public RewardValue(int milesValue) { + if (milesValue >= 0) + miles = milesValue; + else + throw new IllegalArgumentException("miles value must be positive"); + } + + public double getCashValue() { + if (cash != null) + return cash; + return MILES_TO_CASH * miles; + } + + public int getMilesValue() { + if (miles != null) + return miles; + return (int) (1 / MILES_TO_CASH * cash); + } +} \ No newline at end of file diff --git a/src/test/java/RewardValueTests.java b/src/test/java/RewardValueTests.java index b3a78de7d..725fff64f 100644 --- a/src/test/java/RewardValueTests.java +++ b/src/test/java/RewardValueTests.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; public class RewardValueTests { + public static final double MILES_TO_CASH = 0.0035; @Test void create_with_cash_value() { @@ -20,11 +21,17 @@ void create_with_miles_value() { @Test void convert_from_cash_to_miles() { - assert false; + double cashValue = 100; + var rewardValue = new RewardValue(cashValue); + int milesValue = rewardValue.getMilesValue(); + assertEquals(milesValue, (int) (cashValue / MILES_TO_CASH)); } @Test void convert_from_miles_to_cash() { - assert false; + int milesValue = 1000; + var rewardValue = new RewardValue(milesValue); + double cashValue = rewardValue.getCashValue(); + assertEquals(cashValue, milesValue * MILES_TO_CASH); } }