Repository navigation
Expand file tree
/
Copy pathApp.java
More file actions
81 lines (68 loc) · 1.88 KB
/
Copy pathApp.java
File metadata and controls
81 lines (68 loc) · 1.88 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
import java.util.ArrayList;
import iiitb.ess201a7.a7base.Car;
import iiitb.ess201a7.a7base.Location;
import iiitb.ess201a7.a7base.Trip;
public class App {
private Display disp;
private Platform platform;
private int timeStep = 500; // millisecs - change this as needed
public App(Platform p, Display d) {
platform = p;
disp = d;
disp.setApp(this);
}
// called by Display when user requests a trip
public void handleTripRequest(Location start, Location dest) {
Trip newTrip = new Trip(start, dest);
platform.assignCar(newTrip);
}
// initilize the app - updates the initial state of the display and starts a thread
// to simulate movement of cars
public void init() {
disp.init();
ArrayList<Car> cars = platform.findCars();
for(Car car: cars) {
disp.draw(car);
}
Thread simulator = new Thread(new Runnable() {
public void run() {
for(int i =0;i<1000;i++) {
try {
Thread.sleep(timeStep);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
updateLocations();
updateDisplay();
}
}
});
simulator.start();
}
private void updateDisplay() {
ArrayList<Car> cars = platform.findCars();
for(Car car: cars) {
disp.draw(car);
// if car is not idle,
// if OnTrip, draw line from current location to drop point
// else from current location to destination.
// Use disp.drawLine(loc1, loc2);
switch(car.getStatus()) {
case Car.Booked:
disp.drawLine(car.getLocation(), car.getTrip().getStart());
break;
case Car.OnTrip:
disp.drawLine(car.getLocation(), car.getTrip().getDest());
break;
}
}
disp.update();
}
private void updateLocations() {
ArrayList<Car> cars = platform.findCars();
for(Car car: cars) {
car.updateLocation(timeStep/1000.0);
}
}
}