-
-
Save codecademydev/2eb7b8a07b2f336636d835875e7fd6b7 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
package com.company.com; | |
import java.util.Arrays; | |
import java.lang.Math; | |
public class TransitCalculator { | |
int days; | |
int rides; | |
String[] rideTypes = {"Pay-per-ride", "7-day Unlimited", "30-day Unlimited"}; | |
double[] ridePrices = {2.75, 33.00, 127.00}; | |
public TransitCalculator(int daysOnTransit, int individualRides) { | |
days = daysOnTransit; | |
rides = individualRides; | |
} | |
public double unlimited7Price() { | |
double sevenDayPayment = Math.round(days / 7); | |
sevenDayPayment = ((sevenDayPayment * ridePrices[1]) / rides); | |
return sevenDayPayment; | |
} | |
public double[] getRidePrices(){ | |
double[] pricePerRide = {}; | |
pricePerRide[0] = ridePrices[0]; | |
pricePerRide[1] = unlimited7Price(); | |
pricePerRide[2] = (ridePrices[2] / rides); | |
return pricePerRide; | |
} | |
public String getBestFare(){ | |
double[] ridePrices = getRidePrices(); | |
int winningIndex = 0; | |
for (int i = 0; i < ridePrices.length; i++) { | |
if (ridePrices[i] < ridePrices[winningIndex]) { | |
winningIndex = i; | |
} | |
} | |
String bestFare = "You should get the " + rideTypes[winningIndex] + " option at " + ridePrices[winningIndex] + " per ride."; | |
return bestFare; | |
} | |
public static void main(String[] args){ | |
TransitCalculator person = new TransitCalculator(23, 45); | |
System.out.println(person.getBestFare()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think the problem is this :line 22.
double[] pricePerRide = {};
You declared an array with zero element in it.
You cannot change the number of elements once you declare an array.
Instead of that, try this.
double[] pricePerRide = new double[3]; //You should put the numbers of elements here.