-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBus.java
More file actions
113 lines (90 loc) · 2.42 KB
/
Bus.java
File metadata and controls
113 lines (90 loc) · 2.42 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
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;
public class Bus implements Runnable {
static int TIME=0;
static Vector<Station> path;
int id;
Station loc;
public Bus() {
this.path=new Vector<>();
InitStation();
}
public void InitStation() {
for (int i = 0; i < Station.names.length; i++) {
Station sta = new Station();
path.add(sta);
TIME+=sta.time_outside;
}
path.elementAt(0).pass=true;
path.elementAt(path.size()-1).pass=true;
//System.out.println(path.elementAt(path.size()-1)+","+path.elementAt(path.size()-1).pass);
}
@Override
public void run() {
busStart();
}
private void busStart() {
loc=path.elementAt(0);
while(loc!=path.elementAt(path.size()-1)) {
System.out.println("bus go from "+loc.name+" to "+getNext().name+" driving for "+loc.time_outside+" minutes");
try {
Thread.sleep(loc.time_outside);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(getNext().pass==true) {
try {
System.out.println("the bus entered in the station named "+getNext());
TIME+=loc.time_enter;
Thread.sleep(loc.time_enter);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TIME-=loc.time_enter;
}
TIME-=loc.time_outside;
System.out.println("fin: remain "+TIME+" minutes");
loc.pass=false;
loc=getNext();
}
}
public Station getNext(Station station ) {
return path.get(path.indexOf(station)+1);
}
public Station getNext() {
return path.get(path.indexOf(loc)+1);
}
public static void OrderStation(String obj) {
System.out.println("le choix est "+obj);
int k = Bus.getIndex((String)obj);
Station s=Bus.path.elementAt(k);
System.out.println("a user ordered a bus at the station"+s);
Bus.path.get(Bus.path.indexOf(s)).pass=true;
System.out.println(Bus.path);
}
public static int getIndex(String s) {
for (int i = 1; i < path.size(); i++) {
if(path.get(i).name.equals(s)) {
//System.out.println("["+path.get(i).name+"],["+s+"]");
return i;
}
}
System.err.println("not found");
return 1;
}
public static int getTime(int station) {
int sum=0;
for (int i = 1; i < station; i++) {
Station sta = path.elementAt(i);
sum+=sta.time_outside;
if(sta.pass) {
sum+=sta.time_enter;
}
}
return sum;
}
}