-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBus.java
More file actions
98 lines (73 loc) · 2.64 KB
/
Copy pathBus.java
File metadata and controls
98 lines (73 loc) · 2.64 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
//The Bus code
//This is were the action is going to be happenning
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class Bus implements Runnable {
//Kamva
private final int busId;
private final CampusRoute route;
private final BlockingQueue<Passenger> passengerQueue;
private Passenger currentPassenger;
private int currentX;
private int currentY;
public Bus(int busId, CampusRoute route) {
this.busId = busId;
this.route = route;
this.passengerQueue = new LinkedBlockingQueue<>();
// for uniform the buses should start at Stop 0
int[] coords = route.getStopCoordinates(0);
this.currentX = coords[0];
this.currentY = coords[1];
}
@Override
public void run() {
while (true) {
try {
currentPassenger = passengerQueue.take();
pickUpPassenger(currentPassenger);
driveToDestination(currentPassenger);
currentPassenger = null;
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
public void pickUpPassenger(Passenger passenger) {
System.out.println("Bus " + busId + " picked up passenger at Stop " + passenger.getStartingStop());
}
public void driveToDestination(Passenger passenger) {
int stopIndex = passenger.getDestinationStop();
int[] coords = route.getStopCoordinates(stopIndex);
moveTowards(coords[0], coords[1]);
System.out.println("Bus " + busId + " dropped passenger at Stop " + stopIndex);
}
private void moveTowards(int destX, int destY) {
while (currentX != destX || currentY != destY) {
if (currentX < destX) currentX++;
else if (currentX > destX) currentX--;
if (currentY < destY) currentY++;
else if (currentY > destY) currentY--;
try {
Thread.sleep(10);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}
}
public void addPassenger(Passenger passenger) {
passengerQueue.offer(passenger);
}
public int getCurrentX() {
return currentX;
}
public int getCurrentY() {
return currentY;
}
public int getBusId() {
return busId;
}
}