-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ride.java
141 lines (127 loc) · 4.53 KB
/
Ride.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
132
133
134
135
136
137
138
139
140
141
/**
* Abstract class representing an amusement park ride.
* @author jvogt33
* @version 1.00
*/
public abstract class Ride {
protected final String id;
protected double earnings;
protected int runsSinceInspection;
protected String[] passengers;
/**
* Constructor of new Ride with specified params.
* @param id the name of this Ride
* @param runsSinceInspection Runs since Ride's last inspectRide() invocation
* @param passengers list of passengers on the Ride
*/
public Ride(String id, int runsSinceInspection, String[] passengers) {
this.id = id;
this.earnings = 0.0;
this.runsSinceInspection = runsSinceInspection;
this.passengers = new String[passengers.length];
for (int i = 0; i < passengers.length; i++) {
this.passengers[i] = passengers[i];
}
}
/**
* Constructor of new Ride with default runsSinceInspection of 0.
* @param id the name of this Ride
* @param passengers list of passengers on this Ride
*/
public Ride(String id, String[] passengers) {
this(id, 0, passengers);
}
/**
* Virtual method to test whether Ride can run. Overridden in subclasses.
* @param stops Number of stops to run the ride
* @return boolean whether the ride can run number of stops given
*/
public abstract boolean canRun(int stops);
/**
* Virtual method to inspect current Ride. Overridden in subclasses.
* runsSinceInspection should also be reset if inspection passes.
* @param components Components to inspect on this ride
* @return boolean whether inspection was successful.
*/
public abstract boolean inspectRide(String[] components);
/**
* Virtual method to calculate cost per passenger. Overridden in subclasses.
* @param stops number of stops a passenger has requested to run.
* @return double-precision floating point of ride cost.
*/
public abstract double costPerPassenger(int stops);
/**
* Virtual method to add passengers to ride. Overridden in subclasses.
* @param stops Number of stops passengers will ride
* @param passToAdd List of passengers to add
* @return Boolean whether adding passengers was successful
*/
public abstract boolean addPassengers(int stops, String[] passToAdd);
/**
* String representation of passengers[].
* @return String representing list of passengers
*/
public String getPassengerList() {
String o = "";
o += String.format("Passenger List for %s:", id);
for (int i = 0; i < passengers.length; i++) {
if (passengers[i] != null) {
o += String.format("%n%s", passengers[i]);
}
}
return o;
}
/**
* Charges passengers for a specified number of stops.
* This method is final and should not be overridden.
* @param stops Number of stops to charge passenger for
*/
public final void chargePassenger(int stops) {
this.earnings += costPerPassenger(stops);
}
/**
* Removes passenger from the ride. Removes first occurrence of passenger.
* @param name The name of the passenger to remove
* @return boolean whether the passenger was successfully removed
*/
public boolean removePassenger(String name) {
for (int i = 0; i < this.passengers.length; i++) {
if (this.passengers[i] != null) {
if (this.passengers[i].toLowerCase().equals(name.toLowerCase())) {
this.passengers[i] = null;
return true;
}
}
}
return false;
}
/**
* Overrides Object.equals(). Checks whether two Rides are equal.
* @param other the object to test against
* @return boolean representing equality of the objects
*/
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (this.getClass() == other.getClass()) {
return (this.id == ((Ride) other).id
&& this.runsSinceInspection == ((Ride) other).runsSinceInspection);
}
return false;
}
/**
* String representation of this Ride.
* @return String representing this Ride
*/
@Override
public String toString() {
String o = String.format("%s has run %d times and has earned $%.2f.",
this.id, this.runsSinceInspection, this.earnings);
return o;
}
}