Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 12, 2020 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/bd40d4497a828a17aba64524e50776f5 to your computer and use it in GitHub Desktop.
Save codecademydev/bd40d4497a828a17aba64524e50776f5 to your computer and use it in GitHub Desktop.
Codecademy export
public class TransitCalculator {
int numberOfDays;
int numberOfRides;
String[] rideName = {"Pay-per-ride", "7-day Unlimited", "30-day Unlimited"};
double[] ridePrice = {2.75, 33.00, 127.00};
TransitCalculator(int numberOfDaysIn, int numberOfRidesIn) {
numberOfDays = numberOfDaysIn;
numberOfRides = numberOfRidesIn;
}
double unlimited7Price() {
double weeks = Math.ceil(numberOfDays / 7.0);
double totalCost = ridePrice[1] * weeks;
return totalCost / numberOfRides;
}
double[] getRidePrices() {
double weeks = Math.ceil(numberOfDays / 7.0);
double thirtyDays = Math.ceil(numberOfDays / 30.0);
double payPerRideCost = ridePrice[0] * numberOfRides;
double unlimited30Cost = thirtyDays * ridePrice[2] / numberOfRides;
double[] prices = {Math.round(payPerRideCost * 100) / 100.0, Math.round(unlimited7Price() * 100) / 100.0, Math.round(unlimited30Cost * 100) / 100.0};
return prices;
}
String getBestFare() {
double[] prices = getRidePrices();
double smallestPrice = prices[0];
int smallestIndex = 0;
for (int i = 1; i < 3; i++) {
if (prices[i] < smallestPrice) {
smallestPrice = prices[i];
smallestIndex = i;
continue;
}
}
System.out.println(smallestIndex);
return "You should get the " + rideName[smallestIndex] + " option at $" + smallestPrice + " per ride.";
}
public static void main(String[] args) {
TransitCalculator tC = new TransitCalculator(100, 30);
System.out.println(tC.getBestFare());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment