-
Notifications
You must be signed in to change notification settings - Fork 0
/
Taxi4
107 lines (87 loc) · 2.1 KB
/
Taxi4
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
import java.util.*;
public class Taxi implements Comparable {
private Station origin;
private int maxSize;
private double startTime;
private double endTime;
private double returnTime;
private double emptyMiles;
private List<Trip> trips;
public Taxi(int tripSize) {
maxSize = tripSize;
startTime = Double.POSITIVE_INFINITY;
endTime = Double.NEGATIVE_INFINITY;
returnTime = Double.NEGATIVE_INFINITY;
emptyMiles = 0;
trips = new ArrayList<Trip>();
origin = null;
}
public void assignTo(Station station) {
if (origin == null)
origin = station;
}
public void combine(Taxi t) {
if (t.size() < this.size()) {
System.out.println("size inconsistency");
return;
}
for (Trip trip: t.trips()) {
this.addTrip(trip);
}
emptyMiles += t.emptyMiles;
}
public void addTrip(Trip trip) {
// if (trip.size() > maxSize) return; // if trip size is too big for taxi...
trips.add(trip);
// update the start time of taxi, if needed
if (trip.departTime() < startTime)
startTime = trip.departTime();
// update teh end time if taxi, if needed
if (trip.arriveTime() > endTime)
endTime = trip.arriveTime();
}
// update when this taxi will return to station
public void updateReturn(double time) {
if (time > returnTime)
returnTime = time;
}
public void updateEmptyMiles(double miles) {
emptyMiles += miles;
}
public Double startTime() {
return (Double)startTime;
}
public int size() {
return maxSize;
}
public Double endTime() {
return (Double)endTime;
}
public Double returnTime() {
return (Double)returnTime;
}
public boolean contains(Trip trip) {
return trips.contains(trip);
}
//compare starttime with another taxi
public int compareTo(Object obj) {
Taxi other = (Taxi) obj;
return ((Double)startTime).compareTo(other.startTime());
}
public String oStation() {
return origin.center();
}
public List<Trip> trips() {
return trips;
}
public double emptyMiles() {
return emptyMiles;
}
public double vehicleMiles() {
double vehicleMiles = 0;
for (Trip trip : trips) {
vehicleMiles += trip.tripMiles();
}
return vehicleMiles;
}
}