-
-
Save codecademydev/dbf7a3069256468b5b5e3950058da582 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; | |
public class TransitCalculator { | |
int numberOfDays; | |
int numberOfRides; | |
String[] fareOptions = new String[3]; | |
double[] fareOptionsPrices = new double[3]; | |
public TransitCalculator(int days, int rides) { | |
numberOfDays = days; | |
numberOfRides = rides; | |
fareOptions[0] = "Pay-per-ride"; | |
fareOptions[1] = "7-day Unlimited Rides"; | |
fareOptions[2] = "30-day Unlimited Rides"; | |
fareOptionsPrices[0] = 2.75; | |
fareOptionsPrices[1] = 33; | |
fareOptionsPrices[2] = 127; | |
} | |
public double unlimited7Price() { | |
double pricePerRide = 0; | |
int numberOf7DaysTickets = 0; | |
if (numberOfDays % 7 == 0) { | |
numberOf7DaysTickets = numberOfDays / 7; | |
} else { | |
numberOf7DaysTickets = numberOfDays / 7 + 1; | |
} | |
pricePerRide = numberOf7DaysTickets * fareOptionsPrices[1] / numberOfRides; | |
return pricePerRide; | |
} | |
public double[] getRidePrices() { | |
double[] pricePerRideArray = new double[3]; | |
pricePerRideArray[0] = fareOptionsPrices[0]; | |
pricePerRideArray[1] = unlimited7Price(); | |
pricePerRideArray[2] = fareOptionsPrices[2] / numberOfRides; | |
return pricePerRideArray; | |
} | |
public String getBestFare() { | |
int indexOfLowestPricePerRide = 0; | |
double lowestPricePerRide = getRidePrices()[0]; | |
for (int i = 1; i < 3; i++) { | |
if (getRidePrices()[i] < lowestPricePerRide) { | |
lowestPricePerRide = getRidePrices()[i]; | |
indexOfLowestPricePerRide = i; | |
} | |
} | |
return "You should get the " + fareOptions[indexOfLowestPricePerRide] + " option at " + getRidePrices()[indexOfLowestPricePerRide] + " per ride."; | |
} | |
public static void main(String[] args) { | |
TransitCalculator person1 = new TransitCalculator(19, 20); | |
TransitCalculator person2 = new TransitCalculator(22, 50); | |
TransitCalculator person3 = new TransitCalculator(6, 14); | |
TransitCalculator person4 = new TransitCalculator(26, 54); | |
TransitCalculator person5 = new TransitCalculator(5, 12); | |
System.out.println(person1.unlimited7Price()); | |
System.out.println(person2.unlimited7Price()); | |
System.out.println(person3.unlimited7Price()); | |
System.out.println(Arrays.toString(person1.getRidePrices())); | |
System.out.println(Arrays.toString(person2.getRidePrices())); | |
System.out.println(Arrays.toString(person3.getRidePrices())); | |
System.out.println(person1.getBestFare()); | |
System.out.println(person2.getBestFare()); | |
System.out.println(person3.getBestFare()); | |
System.out.println(person4.getBestFare()); | |
System.out.println(person5.getBestFare()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment