-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBus.java
More file actions
74 lines (64 loc) · 1.63 KB
/
Copy pathBus.java
File metadata and controls
74 lines (64 loc) · 1.63 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package collegetransportapplication;
/**
*
* @author soundarya
*/
public class Bus implements Runnable {
private String name;
private Commute commute;
public Bus (int id, Commute commute)
{
setName("Bus-" + id);
this.commute = commute;
new Thread(this).start();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void messsage(String m)
{
System.out.println(getName() + ": " + m);
}
//sleeps a random amount of time within provided range
private void simulatedWait(int range, int minTime)
{
int sleepTime = ((int)(Math.random()*range + minTime));
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
msg("Died :(");
}
}
public void run()
{
while(commute.getnumStudents() > 0) //while there are still students running
{
if(commute.groupReady())
{
msg("Loading bus");
try {
Thread.sleep(simulatedBusTime());
} catch (InterruptedException e) {
e.printStackTrace();
}
commute.busPickUp();
msg("Returning for more students. " + commute.getnumStudents() + " students left");
try {
Thread.sleep((int)(Math.random()*2000 +1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
msg("No more students to bus. Finished job. Students left:" + commute.getnumStudents() + " . ");
return;
}
}