Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 25, 2020 20:10
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/22b29b41268e675386b7dc77f64ec103 to your computer and use it in GitHub Desktop.
Save codecademydev/22b29b41268e675386b7dc77f64ec103 to your computer and use it in GitHub Desktop.
Codecademy export
import java.util.Arrays;
public class TransitCalculator {
String[] fareOptions = {
"Pay-per-ride ride", "7-day Unlimited", "30-day Unlimited"};
String location;
int rides;
int days;
int age;
boolean disabled;
public TransitCalculator (String cityLocation,int numRides, int numDays, int riderAge, boolean isDisabled) {
location = cityLocation;
rides = numRides;
days = numDays;
age = riderAge;
disabled = isDisabled;
}
public double[] getFares(){
double[] fares = new double[2];
//New York Rates
double[] nycFares = {2.75, 33.00, 127.00};
double[] nycReducedFares = {1.35, 16.50, 63.50};
//Chicago Rates
double[] chicagoFares = {2.50, 28.00, 105.00};
double[] chicagoReducedFares = {1.25,28.00,50.00};
if (location.equals("New York")){
if (disabled = false || age<65){
fares = nycFares;
} else {fares = nycReducedFares;}
}
else if (location.equals("Chicago")){
if (disabled = false || age<65){
fares = chicagoFares;
} else {fares = chicagoReducedFares;}
}
return fares;
}
public double unlimited7Price(){
double [] fares = getFares();
int passesNeeded;
if(days%7 == 0){
passesNeeded = days/7;
}
else{
passesNeeded = days/7 + 1;
}
double total7Price = passesNeeded * fares[1];
return Math.ceil(total7Price/rides * 100) / 100;
}
public double[] getRidePrices(){
double [] fares = getFares();
double singleRidePrice;
double unlimited7Price = unlimited7Price();
double unlimited30Price;
singleRidePrice = fares[0];
unlimited30Price = Math.ceil(fares[2]/ rides * 100) / 100;
double [] prices = {singleRidePrice,unlimited7Price,unlimited30Price};
return prices;
}
public String getBestFare() {
double [] ridePrices = getRidePrices();
int bestFareIndex = 0;
for (int i=0 ; i < ridePrices.length; i++) {
if (ridePrices[i] < ridePrices[bestFareIndex]){
bestFareIndex = i;}
}
return "You should get the " + fareOptions[bestFareIndex] + " option at $" + ridePrices [bestFareIndex] + " per ride.";
}
public static void main(String[] args){
//Location options: New York, Chicago:
String myLocation = "New York";
int myRides = 54;
int myDays = 26;
int myAge = 30;
boolean hasDisability = false;
TransitCalculator myFares = new TransitCalculator(myLocation,myRides,myDays,myAge,hasDisability);
System.out.println(Arrays.toString(myFares.getRidePrices()));
System.out.println(myFares.getBestFare());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment