-
Notifications
You must be signed in to change notification settings - Fork 1
/
Taxi.java
87 lines (67 loc) · 1.91 KB
/
Taxi.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
import java.util.*;
public class Taxi implements Comparable {
private String originPixel;
private int size;
private double startTime;
private double endTime;
private List<Trip> trips;
public Taxi(int tripSize) {
size = tripSize;
startTime = Double.POSITIVE_INFINITY;
endTime = 0;
trips = new ArrayList<Trip>();
}
public void addTrip(Trip trip) {
// make sure the trip originates from the origin pixel of this taxi
if (originPixel != null) {
if (!trip.originPixel().equals(originPixel))
return;
}
else {
originPixel = trip.originPixel();
}
if (trip.size() > size) return; // if trip size is too big for taxi...
trips.add(trip);
// update the start and end time of taxi, if needed
if (trip.departTime() < startTime)
startTime = trip.departTime();
if (trip.returnTime() > endTime)
endTime = trip.returnTime();
}
// add the trips in the other taxi to this taxi
public void combineTaxi(Taxi taxi) {
for (Trip trip : taxi.trips) {
addTrip(trip);
}
}
public Double startTime() {
return (Double)startTime;
}
public int size() {
return size;
}
public Double endTime() {
return (Double)endTime;
}
public boolean contains(Trip trip) {
return trips.contains(trip);
}
public int compareTo(Object obj) {
Taxi other = (Taxi) obj;
return ((Double)startTime).compareTo(other.startTime());
}
public int totalTrips() {
return trips.size();
}
public void show() {
System.out.println("Taxi of size " + size + "leaving at " + startTime + " and returning at " + endTime);
System.out.println("Itinerary:\n=================");
System.out.println();
for (Trip trip : trips) {
System.out.println("Trip from " + trip.originPixel() + " to " + trip.destinationPixel());
System.out.println("===============================");
System.out.println("Depart time: " + trip.departTime() + "\nReturn time: " + trip.returnTime());
}
System.out.println("\n");
}
}