From 3f4153f90d45676c2031241728dcf17c01eb2a0d Mon Sep 17 00:00:00 2001 From: JoanU Date: Mon, 7 Oct 2024 06:56:12 -0400 Subject: [PATCH] commit and push changes --- src/main/java/RewardValue.java | 43 +++++++++++++++++++++++++++++ src/main/java/RewardsConverter.java | 2 ++ src/test/java/RewardValueTests.java | 10 +++++-- 3 files changed, 53 insertions(+), 2 deletions(-) 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..ae6cb1fb5 --- /dev/null +++ b/src/main/java/RewardValue.java @@ -0,0 +1,43 @@ +public class RewardValue { + //fields + private final double cashValue; + public static final double MILES_TO_CASH_CONVERSION_RATE = 0.0035; + + //constructors + public RewardValue(double cashValue) { + this.cashValue = cashValue; + } + + public RewardValue(int milesValue) { + this.cashValue = convertToCash(milesValue); + } + + private static int convertToMiles(double cashValue) { + return (int) (cashValue / MILES_TO_CASH_CONVERSION_RATE); + } + + private static double convertToCash(int milesValue) { + return milesValue * MILES_TO_CASH_CONVERSION_RATE; + } + + //getters + public double getCashValue() { + return cashValue; + } + + public int getMilesValue() { + return convertToMiles(this.cashValue); + } +} + /* + +This class must satisfy the following requirements: +RewardValue must have two constructors: one that accepts a cash value and one that accepts a value in miles. +RewardValue must have a getCashValue() method, which returns the cash value of the RewardValue. +RewardValue must have a getMilesValue() method, which returns how many miles the RewardValue is worth. +RewardValue must convert from miles to cash at a rate of 0.0035. + */ + // public RewardValue(double cashValue) { + // } + + diff --git a/src/main/java/RewardsConverter.java b/src/main/java/RewardsConverter.java index 2c35d11a5..699e2fd81 100644 --- a/src/main/java/RewardsConverter.java +++ b/src/main/java/RewardsConverter.java @@ -6,6 +6,7 @@ public static void main(String[] args) { System.out.println("Welcome to the Credit Card Rewards Converter!"); System.out.println("Please enter a cash value to convert to airline miles: "); var input_value = scanner.nextLine(); + double cashValue; try { cashValue = Double.parseDouble(input_value); @@ -13,6 +14,7 @@ public static void main(String[] args) { System.out.println("Could not parse input value as a double, exiting"); return; } + System.out.println("converting $" + input_value + " to miles"); var rewardsValue = new RewardValue(cashValue); System.out.println("$" + input_value + " is worth " + rewardsValue.getMilesValue() + " miles"); diff --git a/src/test/java/RewardValueTests.java b/src/test/java/RewardValueTests.java index b3a78de7d..41641f506 100644 --- a/src/test/java/RewardValueTests.java +++ b/src/test/java/RewardValueTests.java @@ -20,11 +20,17 @@ void create_with_miles_value() { @Test void convert_from_cash_to_miles() { - assert false; + double cashValue = 100; + int expectedMilesValue = (int) (cashValue / RewardValue.MILES_TO_CASH_CONVERSION_RATE); + var rewardValue = new RewardValue(cashValue); + assertEquals(expectedMilesValue, rewardValue.getMilesValue()); } @Test void convert_from_miles_to_cash() { - assert false; + int milesValue = 10000; + double expectedCashValue = milesValue * RewardValue.MILES_TO_CASH_CONVERSION_RATE; + var rewardValue = new RewardValue(milesValue); + assertEquals(expectedCashValue, rewardValue.getCashValue()); } }