Input needed please

I’m having difficulty getting this program to run, specifically to getting the value of String bestPrice, if anyone can spot what I’m doing wrong I would greatly appreciate it. Additionally, if anyone has any advice on how to make my code better I’d love to hear ideas.

public class TransitCalculator{

int daysRidden;
int rides;
double price;

public TransitCalculator(int daysRidden, int rides, double price){
this.daysRidden = daysRidden;
this.rides = rides;
this.price = price;
}

public static double payPerRide(int daysRidden, int rides, double price){
double overAll = (daysRiddenrides)price;
return overAll;
}
public static double payPerWeek(int daysRidden,int rides, double price){
int passesNeeded = (daysRidden/7);
if(daysRidden%7 !=0){
passesNeeded++;
}
double overAll = passesNeeded
price;
double averageCost = overAll/(daysRidden
rides);
return averageCost;
}
public static double perThirty(int daysRidden, int rides, double price){
if(daysRidden<30){
daysRidden=30;
}
double averageCost = price / (daysRiddenrides);
return averageCost;
}
public static String getBestFair(double daysRidden, double ridesTaken, double perEachPrice, double perSevenPrice, double perThirtyPrice){
double averageCost1= perEachPrice/(daysRidden
ridesTaken);
double averageCost2= perSevenPrice;
double averageCost3= perThirtyPrice;

if(averageCost1<averageCost2 && averageCost1<averageCost3){
 String bestPrice ="The best price is paying for each ride at an average of " + averageCost1 +"per ride.";
  } else if(averageCost2<averageCost1 && averageCost2<averageCost3){
  String bestPrice = "The best price is paying for 7 day passes at an average of " + averageCost2 + "per ride.";
  } else if(averageCost3<averageCost2 && averageCost3<averageCost1){
  String bestPrice = "The best price is paying for a 30 day pass at an average rate of" + averageCost3 + "per ride.";
  } return bestPrice;

}
public static void main(String args){

String[] options = {"Pay-Per-Ride","7-Day Unlimited", "30-Day Unlimited"};
double[] prices = {2.75, 33.00, 127.00};

double payEach = TransitCalculator.payPerRide(30, 2, 2.75);

System.out.println("The total cost of paying for each ride is " + payEach);

double sevenDay = TransitCalculator.payPerWeek(30, 2, 33.00);
System.out.println("The average cost per ride by paying per week is " + sevenDay);

double thirtyDay = TransitCalculator.perThirty(30, 2, 127.00);
System.out.println("The average cost per ride by paying per 30 days is " + thirtyDay);

String NYCBestValue = getBestFair(30, 2, payEach, sevenDay, thirtyDay);

System.out.println(NYCBestValue);

}

}

1 Like

You have the bestPrice variable declared inside the if-else statements … it may be better to declare it outside of that and just assign a value to the variable inside of the if-statement or else-if statements.

your code for getBestFair method
public static String getBestFair(double daysRidden, double ridesTaken, double perEachPrice, double perSevenPrice, double perThirtyPrice){
  double averageCost1 = perEachPrice/(daysRidden * ridesTaken);
  double averageCost2 = perSevenPrice;
  double averageCost3 = perThirtyPrice;

  if(averageCost1<averageCost2 && averageCost1<averageCost3){
      String bestPrice = "The best price is paying for each ride at an average of " + averageCost1 +"per ride.";
  } else if(averageCost2<averageCost1 && averageCost2<averageCost3){
      String bestPrice = "The best price is paying for 7 day passes at an average of " + averageCost2 + "per ride.";
  } else if(averageCost3<averageCost2 && averageCost3<averageCost1){
      String bestPrice = "The best price is paying for a 30 day pass at an average rate of" + averageCost3 + "per ride.";
  } 
  return bestPrice;
}

Maybe like this:

  String bestPrice;

  if(averageCost1<averageCost2 && averageCost1<averageCost3){
      bestPrice = "The best price is paying for each ride at an average of " + averageCost1 +"per ride.";
  } else if(averageCost2<averageCost1 && averageCost2<averageCost3){
      bestPrice = "The best price is paying for 7 day passes at an average of " + averageCost2 + "per ride.";
  } else if(averageCost3<averageCost2 && averageCost3<averageCost1){
      bestPrice = "The best price is paying for a 30 day pass at an average rate of" + averageCost3 + "per ride.";
  } 
  return bestPrice;

I don’t know if this helps you at all … but this kind of thing has caused errors for me in the past.

My version of the project was very different … The methods I used had fewer parameters, and I called methods inside of other methods more.

parts of my code for the getRidePrices and getBestFare methods
  public double payPerRidePrice = 2.75;

  public double[] getRidePrices() {
    double[] pricePerRide = {0.0, 0.0, 0.0};

    pricePerRide[0] = payPerRidePrice;
    pricePerRide[1] = unlimited7Price();
    pricePerRide[2] = unlimited30Price();
    return pricePerRide;
  }

  public String getBestFare() {
    String[] method = {"Pay-per-ride", "7-day Unlimited", "30-day Unlimited"};
    double[] prices = getRidePrices();
    int indexOfMin = 0;
    if ((prices[0] < prices[1]) && (prices[0] < prices[2])) {
      indexOfMin = 0;
    }
    else if ((prices[1] < prices[0]) && (prices[1] < prices[2])) {
      indexOfMin = 1;
    }
    else if ((prices[2] < prices[0]) && (prices[2] < prices[1])) {
      indexOfMin = 2;
    }
 
    return "You should get the " + method[indexOfMin] + " option at $" + String.format("%.2f", prices[indexOfMin]) + " per ride.";
  }

Thank you for the input, this was my first attempt at the project so I knew going into it my code was going to be fairly clunky lol

I ended up having to simply declare String bestPrice = " " before the if-else statements and removing the word “String” from inside the method.

But the whole thing works as intended now, I’ll definitely have to attempt this again once I have some more experience.