-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
89 lines (80 loc) · 1.85 KB
/
Copy pathStudent.java
File metadata and controls
89 lines (80 loc) · 1.85 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
/*
* 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 Student implements Runnable {
private String name;
private Commute commute;
public Student (int id, Commute commute)
{
setName("Student-" + id);
this.commute = commute;
new Thread(this).start();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void msg(String m)
{
System.out.println( getName() + ": " + m);
}
//decides whether the student will go by bus or car
private boolean isBussed()
{
if(Math.random() < 0.5)
{
msg("Goes by car");
return false;
}
else
{
msg("Goes by bus");
return true;
}
}
//generates a random time between 1000 and 3000 milliseconds
private int simulatedDriveTime()
{
int driveTime = ((int)(Math.random()*2000 +1000));
msg("Commuting in car for " + driveTime + " ms"); //that was a quick drive!
return driveTime;
}
//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()
{
boolean bussed = isBussed();
if(bussed)
{
msg("Waiting on bus");
commute.findBus();
msg("Got off bus");
}
else
{
msg("Left home by car");
simulatedWait(2000, 2000);
msg("Arrived at drop-off, waiting for Coordinator");
commute.findCoordinator();
}
commute.decrementnumStudents();
msg("Arrived at college assembly hall");
}
}