Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task2 #1464

Open
wants to merge 1 commit into
base: flow
Choose a base branch
from
Open

task2 #1464

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/RewardValue.java
Original file line number Diff line number Diff line change
@@ -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) {
// }


2 changes: 2 additions & 0 deletions src/main/java/RewardsConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ 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);
} catch (NumberFormatException exception) {
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");
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/RewardValueTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}