From c96130301297599338a4e9ebd98a6bf5405ac59e Mon Sep 17 00:00:00 2001 From: Rehan-Pathan Date: Sun, 22 Sep 2024 16:17:30 +0530 Subject: [PATCH 1/2] Implemented RewardValue class for cash and miles conversion --- src/main/java/RewardValue.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/java/RewardValue.java diff --git a/src/main/java/RewardValue.java b/src/main/java/RewardValue.java new file mode 100644 index 000000000..fa11d5a10 --- /dev/null +++ b/src/main/java/RewardValue.java @@ -0,0 +1,33 @@ + // Make sure this matches the actual package name + +public class RewardValue { + // Conversion rate from miles to cash + private static final double MILES_TO_CASH_RATE = 0.0035; + + // Store the value in cash or miles + private double cashValue; + private double milesValue; + + // Constructor that accepts a cash value + public RewardValue(double cashValue) { + this.cashValue = cashValue; + this.milesValue = cashValue / MILES_TO_CASH_RATE; // Convert cash to miles + } + + // Constructor that accepts a miles value + public RewardValue(int milesValue) { + this.milesValue = milesValue; + this.cashValue = milesValue * MILES_TO_CASH_RATE; // Convert miles to cash + } + + // Method to get the cash value + public double getCashValue() { + return this.cashValue; + } + + // Method to get the miles value + public double getMilesValue() { + return this.milesValue; + } +} + From a95a33c558b4b6367b30483c5d43c2806a7fddb3 Mon Sep 17 00:00:00 2001 From: Rehan-Pathan Date: Tue, 24 Sep 2024 15:07:40 +0530 Subject: [PATCH 2/2] Completed unit tests for RewardValue class: added cash-to-miles and miles-to-cash conversion tests --- src/test/java/RewardValueTests.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/test/java/RewardValueTests.java b/src/test/java/RewardValueTests.java index b3a78de7d..7fa895172 100644 --- a/src/test/java/RewardValueTests.java +++ b/src/test/java/RewardValueTests.java @@ -20,11 +20,20 @@ void create_with_miles_value() { @Test void convert_from_cash_to_miles() { - assert false; + double cash=100; + var rewardValue=new RewardValue(cash); + double miles=rewardValue.getMilesValue(); + double milesTest=cash/0.0035; + assertEquals(miles,milesTest); } @Test void convert_from_miles_to_cash() { - assert false; + + int miles=1000; + double cashTest=miles*0.0035; + var rewardValue=new RewardValue(miles); + double cash=rewardValue.getCashValue(); + assertEquals(cash,cashTest); } }