Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created September 26, 2020 14:19
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/b6e6646dd25049920585c00631476d4f to your computer and use it in GitHub Desktop.
Save codecademydev/b6e6646dd25049920585c00631476d4f to your computer and use it in GitHub Desktop.
Codecademy export
import java.util.Arrays;
public class TransitCalculator{
int number_of_days;
int number_of_rides;
int age_number;
String[] days = {"Pay-per-ride", "7-day Unlimited ", "30-day Unlimited "};
double[] prices = {2.75, 33.00, 127.00};
double[] reduced_prices = {1.35, 16.50, 63.50};
public TransitCalculator(int num_days, int num_rides, int age){
number_of_days = num_days;
number_of_rides = num_rides;
age_number = age;
}
public double unlimited7Price(){
int num = 0;
int rounded_num = 0;
double price = 0;
for(int i = 0; i <= 7; i++){
num = number_of_days + i;
if(num % 7 == 0){
rounded_num = num;
}
}
int num_week = rounded_num/7;
double overall_price = (num_week*prices[1])/number_of_rides;
double reduced_overall_price = (num_week*reduced_prices[1])/number_of_rides;
if (age_number < 65){
price = overall_price;
}else{
price = reduced_overall_price;
}
return price;
}
public double unlimited30Price(){
int num = 0;
int rounded_num = 0;
double price = 0;
for(int i = 0; i <= 30; i++){
num = number_of_days + i;
if(num % 30 == 0){
rounded_num = num;
}
}
int num_month = rounded_num/30;
double overall_price = (num_month*prices[2])/number_of_rides;
double reduced_overall_price = (num_month*reduced_prices[2])/number_of_rides;
if (age_number < 65){
price = overall_price;
}else{
price = reduced_overall_price;
}
return price;
}
public double[] getRidePrices(){
double[] price_ride = new double[3];
if (age_number < 65){
price_ride[0] = prices[0];
price_ride[1] = unlimited7Price();
price_ride[2] = unlimited30Price();
}else{
price_ride[0] = reduced_prices[0];
price_ride[1] = unlimited7Price();
price_ride[2] = unlimited30Price();
}
return price_ride;
}
public String getBestFare(){
int num = 0;
for(int i = 1; i < 3; i++){
if (getRidePrices()[i] < getRidePrices()[num]){
num = i;
}
}
return "You should get the " + days[num] + " option at " + getRidePrices()[num] + " per ride.";
}
public static void main(String[] args){
TransitCalculator transport = new TransitCalculator(26, 54, 30);
TransitCalculator reducedTransport = new TransitCalculator(26, 54, 65);
System.out.println(transport.getBestFare());
System.out.println(reducedTransport.getBestFare());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment