Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 25, 2020 10:35
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/caff562bdedc732f77d187cce2fcd6bc to your computer and use it in GitHub Desktop.
Save codecademydev/caff562bdedc732f77d187cce2fcd6bc to your computer and use it in GitHub Desktop.
Codecademy export
//determine best fare option during a visit to NYC. When travelling for different reasons, the period of stay and total number of rides vary. Given that there are several price plans for tickets (i.e. pay-per-ride, 7daysUnlimited, 30daysUnlimited), I made the code to not just find the best plan, but the best combination of plans to maximize savings. You're Welcome!
import java.util.ArrayList;
import java.util.Arrays;
public class TransitCalculator {
//declare instances
protected double numDays;
protected double totalRides;
//constructor method
public TransitCalculator(double days, double rides) {
numDays= days;
totalRides= rides;
}
//options[pay-per-ride, 7-day Unlimited, 30-day Unlimited]
double[] options= {2.75, 33.0, 127.0}; //change price here for cities or age groups
String a1;
String a024;
String a03;
String a04;
String a24;
String a3;
String a4;
//the number behind is equal to the index of the prices that are added together. Refer to ArrayList below.
//getInfo method
public void getInfo(){
System.out.println(totalRides+" rides and "+numDays+" days; Calculating by average rides per day..."+"\n");
ArrayList<Double> cost= new ArrayList<Double>();
/* ArrayList will hold 5 items, with indices [0]-[4]. The order will be STRICTLY FOLLOWS:
{full30++, full30 ONLY, full7++, full7 ONLY, singleppr}
full30 means perfect sets of 30, same for full7
++ means that this index will add the value of another index
ONLY means that this index terminates the summation of values
singleppr is automatic final termination
Therefore possible sums can be seen by Strings declared above. */
if(totalRides>3*numDays/2) {
if(numDays>=30) {
System.out.println("There are at least 30x days!"+"\n");
double full30Lower= numDays;
while(full30Lower%30!=0.0) {
full30Lower--;
}
cost.add(full30Lower*options[2]/30); //index[0]
double full30Upper= full30Lower+30;
cost.add(full30Upper*options[2]/30); //index[1]
//final= [1]
a1= "Recommended Purchase: "+full30Upper/30+" pass(es) of 30-day Unlimited Rides at a price of "+cost.get(1)+". "+"\n"+"Price per ride: "+cost.get(1)/totalRides+". ";
double r= numDays-full30Lower;
if(r>=7) {
System.out.println("The remainder has at least 7x days!"+"\n");
double full7Lower=r;
while(full7Lower%7!=0.0) {
full7Lower--;
}
cost.add(full7Lower*options[1]/7); //index[2]
double full7Upper= full7Lower+7;
cost.add(full7Upper*options[1]/7); //index[3]
double r2= r-full7Lower;
double ridesLeft= Math.ceil(r2*totalRides/numDays);
cost.add(ridesLeft*options[0]); //index[4]
//final= [0]+[2]+[4] OR [0]+[3]
double cost024= cost.get(0)+cost.get(2)+cost.get(4);
double cost03= cost.get(0)+cost.get(3);
a024= "Recommended Purchase: "+full30Lower/30+" pass(es) of 30-day Unlimited Rides, "+full7Lower/7+" pass(es) of 7-day Unlimited Rides and "+ridesLeft+" pay-per-ride pass(es) at a price of "+cost024+". "+"\n"+"Price per ride: "+cost024/totalRides+". ";
a03= "Recommended Purchase: "+full30Lower/30+" pass(es) of 30-day Unlimited Rides and "+full7Upper/7+" pass(es) of 7-day Unlimited Rides at a price of "+cost03+". "+"\n"+"Price per ride: "+cost03/totalRides+". ";
} else {
System.out.println("The remainder is below 7 days. "+"\n");
cost.add(0.0); //index[2]
cost.add(options[1]); //index[3]
double ridesLeft= Math.ceil(r*totalRides/numDays);
cost.add(ridesLeft*options[0]); //index[4]
//final= [0]+[3] OR [0]+[4](+[2])
double cost03= cost.get(0)+cost.get(3);
double cost04= cost.get(0)+cost.get(4);
a03= "Recommended Purchase: "+full30Lower/30+" pass(es) of 30-day Unlimited Rides and 1 pass(es) of 7-day Unlimited Rides at a price of "+cost03+". "+"\n"+"Price per ride: "+cost03/totalRides+". ";
a04= "Recommended Purchase: "+full30Lower/30+" pass(es) of 30-day Unlimited Rides and "+ridesLeft+" pay-per-ride pass(es) at a price of "+cost04+". "+"\n"+"Price per ride: "+cost04/totalRides+". ";
}
} else {
System.out.println("Less than 30 days, check to see if the price is worth it. "+"\n");
cost.add(0.0); //index[0]
cost.add(options[2]); //index[1]
//final= [1]
a1= "Recommended Purchase: 1 pass(es) of 30-day Unlimited Rides at a price of "+cost.get(1)+". "+"\n"+"Price per ride: "+cost.get(1)/totalRides+". ";
if(numDays>=7) {
System.out.println("Still more than 7 days though!"+"\n");
double full7Lower= numDays;
while(full7Lower%7!=0.0) {
full7Lower--;
}
cost.add(full7Lower*options[1]/7); //index[2]
double full7Upper= full7Lower+7;
cost.add(full7Upper*options[1]/7); //index[3]
//final= [3](+[0])
a3= "Recommended Purchase: "+full7Upper/7+" pass(es) of 7-day Unlimited Rides at a price of "+cost.get(3)+". "+"\n"+"Price per ride: "+cost.get(3)/totalRides+". ";
double r= numDays-full7Lower;
double ridesLeft= Math.ceil(r*totalRides/numDays);
cost.add(ridesLeft*options[0]); //index[4]
//final= [2]+[4](+[0])
double cost24= cost.get(2)+cost.get(4);
a24= "Recommended Purchase: "+full7Lower/7+" pass(es) of 7-day Unlimited Rides and "+ridesLeft+" pay-per-ride pass(es) at a price of "+cost24+". "+"\n"+"Price per ride: "+cost24/totalRides+". ";
} else {
System.out.println("Not even 7 days :(, come for another visit!"+"\n");
cost.add(0.0); //index[2]
cost.add(options[1]); //index[3]
cost.add(totalRides*options[0]); //index[4]
//final= [3](+[0]) OR [4](+[0]+[2])
a3= "Recommended Purchase: 1 pass(es) of 7-day Unlimited Rides at a price of "+cost.get(3)+". "+"\n"+"Price per ride: "+cost.get(3)/totalRides+". ";
a4= "Recommended Purchase: "+totalRides+" pay-per-ride pass(es) at a price of "+cost.get(4)+". "+"\n"+"Price per ride: "+cost.get(4)/totalRides+". ";
}
}
} else {
System.out.println("You shoud really use the Transit System more!"+"\n");
cost.add(0.0); //index[0]
cost.add(0.0); //index[1]
cost.add(0.0); //index[2]
cost.add(0.0); //index[3]
cost.add(totalRides*options[0]); //index[4]
a4= "Recommended Purchase: "+totalRides+" pay-per-ride pass(es) at a price of "+cost.get(4)+". "+"\n"+"Price per ride: "+cost.get(4)/totalRides+". ";
}
double cost1= cost.get(1);
double cost2= cost.get(0)+cost.get(2)+cost.get(4);
double cost3= cost.get(0)+cost.get(3);
System.out.println(cost1);
System.out.println(cost2);
System.out.println(cost3);
if(cost1<cost2 && cost1!=0.0) {
if(cost1<cost3 || cost3==0) {
System.out.println(a1);
} else if(cost.get(0)!=0) {
System.out.println(a03);
} else {
System.out.println(a3);
}
} else if(cost3==0 || (cost2<cost3 && cost2!=0.0)) {
if(cost.get(0)!=0 && cost.get(2)!=0) {
System.out.println(a024);
} else if(cost.get(0)!=0) {
System.out.println(a04);
} else if(cost.get(2)!=0) {
System.out.println(a24);
} else {
System.out.println(a4);
}
} else if(cost.get(0)!=0) {
System.out.println(a03);
} else {
System.out.println(a3);
}
}
public static void main(String[] args) {
//test scenario
TransitCalculator adam= new TransitCalculator(10, 34);
adam.getInfo();
}
}
@JohnsonLuu
Copy link

How long did this take? Haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment