-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShuttle.java
More file actions
172 lines (154 loc) · 4.81 KB
/
Copy pathShuttle.java
File metadata and controls
172 lines (154 loc) · 4.81 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package model;
import java.awt.Image;
import java.util.Iterator;
import java.util.List;
import java.util.LinkedList;
import javax.swing.ImageIcon;
/**
* A shuttle is able to carry multiple passengers.
* This implementation is non-functional.
*
* @author David J. Barnes and Michael Kolling. Modified ZHENG yujie, SU yu
* @version 2017.03.25
*/
public class Shuttle extends Vehicle implements DrawableItem
{
// The maximum number of passengers for this shuttle.
private final int MAXPASSENGERS;
private Passenger passenger;
// The list of destinations for the shuttle.
private List<Location> destinations;
// The list of passengers on the shuttle.
private List<Passenger> passengers;
private int nbPassenger;
private Image shuttleImage;
private Image passengerImage;
/**
* Constructor for objects of class Shuttle
* @param maxPassengers The max number of passengers. Must be positive.
* @param company The taxi company. Must not be null.
* @param location The vehicle's starting point. Must not be null.
* @throws NullPointerException If company or location is null.
*/
public Shuttle( TaxiCompany company, Location location)
{
super(company, location);
MAXPASSENGERS = 10;
destinations = new LinkedList<>();
passengers = new LinkedList<>();
shuttleImage = new ImageIcon(getClass().getResource(
"/images/bus.jpg")).getImage();
passengerImage = new ImageIcon(getClass().getResource(
"/images/bus+persons.jpg")).getImage();
}
public Shuttle(int maxPassengers, TaxiCompany company, Location location, Image PassengerImage)
{
super(company, location);
MAXPASSENGERS = maxPassengers;
destinations = new LinkedList<>();
passengers = new LinkedList<>();
shuttleImage = new ImageIcon(getClass().getResource(
"/images/taxi.jpg")).getImage();
passengerImage = new ImageIcon(getClass().getResource(
"/images/bus+persons.jpg")).getImage();
}
/**
* Carry out a shuttle's actions.
*/
public void act()
{
chooseTargetLocation();
Location target = super.getTargetLocation();
if(target != null) {
// Find where to move to next.
Location next = getLocation().nextLocation(target);
setLocation(next);
if(next.equals(target)) {
if(passenger != null) {
notifyPassengerArrival(passenger);
offloadPassenger();
}
else {
notifyPickupArrival();
}
}
}
else {
incrementIdleCount();
}
}
/**
* @return Whether or not this vehicle is free.
*/
public boolean isFree()
{
if (nbPassenger<MAXPASSENGERS){
return true;}
return false;
}
/**
* Receive a pickup location.
* @location The pickup location.
*/
public void setPickupLocation(Location location)
{
destinations.add(location);
chooseTargetLocation();
}
/**
* Receive a passenger.
* Add the destination to the list.
* @param passenger The passenger.
*/
public void pickup(Passenger passenger)
{
this.passenger = passenger;
passengers.add(passenger);
destinations.remove(0);
if (!destinations.contains(passenger.getDestination())){
destinations.add(passenger.getDestination());
}
}
/**
* Decide where to go next, based on the list of
* possible destinations.
*/
private void chooseTargetLocation()
{
if(!destinations.isEmpty()){
super.setTargetLocation(destinations.get(0));
}
}
/**
* Offload all passengers whose destination is the
* current location.
*/
public void offloadPassenger()
{
Iterator< Passenger> it = passengers.iterator();
while (it.hasNext()){
Passenger passengerShutter=it.next();
if (passengerShutter.getDestination().equals(super.getLocation())){
passengers.remove(passengerShutter);
destinations.remove(super.getLocation());
}
}
passenger = null;
clearTargetLocation();
}
public Image getImage()
{
if(passengers.isEmpty()) {
return shuttleImage ;
}
else {
return passengerImage;
}
}
/**
* @return The maximum number of passengers for this shuttle
*/
public int getMaxPassengers() {
return MAXPASSENGERS;
}
}