-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransitCalc.java
131 lines (109 loc) · 3.94 KB
/
TransitCalc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import javax.swing.*;
import java.text.DecimalFormat;
/**
* Calculates the best fare given days and rides.
*
* @Author Colin McAllister
* @Version April 30, 2020
*/
public class TransitCalc {
private int numDays;
private int numRides;
// final values of the passes
public final double PAY_PER_RIDE = 2.75;
public final double SEVEN_DAY_UNLIMITED = 33.00;
public final double THIRTY_DAY_UNLIMITED = 127.00;
/**
* Constructor for the TransitCalc
* @param numDaysIn number of days
* @param numRidesIn number of rides
*/
public TransitCalc(int numDaysIn, int numRidesIn) {
numDays = numDaysIn;
numRides = numRidesIn;
}
/**
* Get's the price per ride if purchasing 7-day passes.
* @return double
*/
public double unlimited7Price() {
double pricePerRide = 0;
// total 7-day passes needed to buy
int totalPasses = (numDays / 7) + 1;
double totalPrice = totalPasses * SEVEN_DAY_UNLIMITED;
pricePerRide = totalPrice / numRides;
return pricePerRide;
}
/**
* Get's the price per ride for each ticket type.
* @return double[]
*/
public double[] getRidePrices() {
double[] prices = new double[3];
//price per ride if you pay per ride
prices[0] = PAY_PER_RIDE;
//unlimited 7 price per ride
prices[1] = this.unlimited7Price();
//unlimited 30 per ride
prices[2] = THIRTY_DAY_UNLIMITED / numRides;
return prices;
}
/**
* Get's the best fare given #of rides and #of days
* @return Sting
*/
public String getBestFare() {
DecimalFormat df = new DecimalFormat("$#0.00");
String advice = "You should get the ";
double[] prices = this.getRidePrices();
if(prices[0] <= prices[1] && prices[0] <= prices[2]) {
advice += "Pay-per-ride option at " + df.format(PAY_PER_RIDE) + " per ride.";
} else if (prices[1] <= prices[2]) {
advice += "7-day Unlimited option at " + df.format(prices[1]) + " per ride.";
} else if (prices[2] <= prices[1] && prices[2] <= prices[0]) {
advice += "30-day Unlimited option at " + df.format(prices[2]) + " per ride.";
} else {
advice += "heck out of dodge! Just kidding my program is broken lulz.";
}
return advice;
}
public int getNumDays() {
return numDays;
}
public void setNumDays(int numDays) {
this.numDays = numDays;
}
public int getNumRides() {
return numRides;
}
public void setNumRides(int numRides) {
this.numRides = numRides;
}
/**
* Driver for the Transit Calculator.
* @param args not used
*/
public static void main(String[] args) {
String input = null; // input from user as string
int numDays;
int numRides;
int runAgain = 0;
do { //loops as long as user wants
try {
input = (JOptionPane.showInputDialog("How many days will you be riding?"));
if (input == null) {return;} // if user presses cancel, gtfo
numDays = Integer.parseInt(input);
input = (JOptionPane.showInputDialog("How many rides?"));
if (input == null) {return;} // if user presses cancel, gtfo
numRides = Integer.parseInt(input);
TransitCalc yourRecommendation = new TransitCalc(numDays, numRides);
JOptionPane.showMessageDialog(null, yourRecommendation.getBestFare());
runAgain = JOptionPane.showConfirmDialog(null,
"Calculate again?"); // checks to see if user wants to go again
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,
"Please enter a positive integer.");
}
} while ((runAgain == 0));
}
}