-
-
Save codecademydev/2265dcf568a0f1198d69777e52983321 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
/** | |
* TransitCalculator Project | |
* In this project you will write a Java program that determines the best fare option | |
* for someone visiting New York City who plans to use the NYC transit system. | |
* The program should use constructors, methods, conditionals, loops, and arrays. | |
*/ | |
import java.util.*; | |
import java.util.Arrays; | |
public class TransitCalculator { | |
int riderAge, numOfDays, numOfRides; | |
String[] ridePlans = {"Pay-per-ride", "7-day Unlimited Rides", "30-day Unlimited Rides"}; | |
double[] planRates = {2.75, 33.00, 127.00}; | |
double rate, bestPrice; | |
String bestPlan; | |
// Constructor for objects of class TransitCalculator | |
public TransitCalculator(int age, int days, int rides) { | |
riderAge = age; | |
numOfDays = days; | |
numOfRides = rides; | |
} | |
// payPerRidePrice method | |
// Returns the price per ride of using the pay per ride rate | |
public double payPerRidePrice() { | |
if (riderAge < 65) { | |
return planRates[0]; | |
} else { | |
return planRates[0] * 0.5; | |
} | |
} | |
// unlimited7Price method | |
//Returns the price per ride of using the 7-day rate | |
public double unlimited7Price() { | |
if (riderAge < 65) { | |
return Math.ceil(numOfDays / 7.0) * planRates[1] / numOfRides; | |
} else { | |
return Math.ceil(numOfDays / 7.0) * (planRates[1] * 0.5 / numOfRides); | |
} | |
} | |
// unlimited30Price method | |
// Returns the price per ride of using the 30-day rate | |
public double unlimited30Price() { | |
if (riderAge < 65) { | |
return planRates[2] / numOfRides; | |
} else { | |
return planRates[2] * 0.5 / numOfRides; | |
} | |
} | |
// getBestFare() method | |
// Determine the lowest price & the best fare plan | |
// Return a String that communicates the findings | |
public String getBestFare() { | |
if (payPerRidePrice() < unlimited7Price() && payPerRidePrice() < unlimited30Price()) { | |
bestPrice = Math.round(payPerRidePrice() * 100.0) / 100.0; | |
bestPlan = ridePlans[0]; | |
} else if (unlimited7Price() < payPerRidePrice() && unlimited7Price() < unlimited30Price()) { | |
bestPrice = Math.round(unlimited7Price() * 100.0) / 100.0; | |
bestPlan = ridePlans[1]; | |
} else { | |
bestPrice = Math.round(unlimited30Price() * 100.0) / 100.0; | |
bestPlan = ridePlans[2]; | |
} | |
return "You should get the " + bestPlan + " option at $" + bestPrice + " per ride."; | |
} | |
// main method | |
public static void main(String[] args) { | |
int myAge = 35; | |
int myDays = 7; | |
int myRides = 14; | |
TransitCalculator calc = new TransitCalculator(myAge, myDays, myRides); | |
System.out.println(calc.getBestFare()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment