Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 30, 2021 03:20
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/4ee3ce86632c13821f3b101f8e198f39 to your computer and use it in GitHub Desktop.
Save codecademydev/4ee3ce86632c13821f3b101f8e198f39 to your computer and use it in GitHub Desktop.
Codecademy export
import java.util.Arrays;
class TransitCalculator {
int days;
int rides;
String[] transitOptions = {"Pay-per-ride", "7-day Unlimited Rides", "30-day Unlimited Rides"};
double[] price = {2.75, 33.00, 127.00};
TransitCalculator(int numDays, int numRides) {
days = numDays;
rides = numRides;
}
public double unlimited7Price() {
return (Math.ceil(days/7)*price[1])/rides;
}
public double[] getRidePrices() {
double unlimited30Price = (Math.ceil(days/30)*price[2])/rides;
double[] prices = {price[0] , unlimited7Price(), unlimited30Price};
return prices;
}
public String getBestFare(){
int num = 0;
for (int i = 0; i < getRidePrices().length; i++) {
if (getRidePrices()[i] < getRidePrices()[num]) {
num = i;
}
}
return "You should get the " + transitOptions[num] + " option at $" + Math.round((getRidePrices()[num]*100.0))/100.0 + " per ride.";
}
public static void main(String[] args) {
TransitCalculator vancouver = new TransitCalculator(60, 120);
System.out.println(vancouver.getBestFare());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment