-
-
Save codecademydev/4ee3ce86632c13821f3b101f8e198f39 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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