- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrainManagementSystem.java
More file actions
Latest commit
182 lines (145 loc) · 5.31 KB
/
Copy pathTrainManagementSystem.java
File metadata and controls
182 lines (145 loc) · 5.31 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
importjava.util.*;
importjavax.jms.*;
importjava.io.*;
importjava.lang.*;
publicclassTrainManagementSystem{
publicstaticStringserverUrl;
publicstaticTrainMsgSyncProducerbreakdownMsger;
publicstaticTrainMsgProducerresumeServiceMsger;
publicstaticvoidmain(String[] args){
Scannersc = newScanner(System.in);
booleanrun = true;
serverUrl = "localhost"; //if this is done locally
initializeMsgProducers();
while(run){
System.out.println();
System.out.println();
System.out.println("====================================");
System.out.println(" Train Management System ");
System.out.println("====================================");
System.out.println();
System.out.println();
System.out.println("Server URL: " + serverUrl);
System.out.println("1. Update Server URL");
System.out.println("2. Send Breakdown Message");
System.out.println("3. Send System Resumed Message");
System.out.println("4. Quit Program");
System.out.println();
//cls();
System.out.print("Option: ");
intinput = sc.nextInt();
run = routeInput(input,sc);
}
}
//Working
publicstaticbooleaninitializeMsgProducers(){
breakdownMsger = newTrainMsgSyncProducer(serverUrl,"q.breakdown","q.deployed",TrainMsgProducer.QUEUE);
resumeServiceMsger = newTrainMsgProducer(serverUrl,"q.resumed",TrainMsgProducer.QUEUE);
returntrue;
}
//Route the input from main
publicstaticbooleanrouteInput(intinput, Scannersc){
if(input == 1){
updateURL(sc);
cls();
returntrue;
}
if(input == 2){
messageScreen();
cls();
returntrue;
}
if (input == 3){
systemResumedScreen();
cls();
returntrue;
}
cls();
returnfalse;
}
publicstaticvoidupdateURL(Scannersc){
System.out.println();
System.out.println("Current serverURL: " + serverUrl);
System.out.print("New serverURL: ");
System.out.flush();
serverUrl = sc.next(); //Get the new serverURL and update it
System.out.flush();
//if you update reinitialize the message producers
initializeMsgProducers();
}
publicstaticvoidmessageScreen(){
processAllFiles(); //This process the XML files and send it
}
publicstaticvoidsystemResumedScreen(){
System.out.println("Informing q.resumed that systems has resumed");
resumeServiceMsger.sendMessage("SYSTEM RESUMED");
System.out.println("MESSAGE SENT");
}
publicstaticvoidprocessAllFiles(){
Stringaddress = "C://EI//Project//breakdownReports";
visitAllFiles(newFile(address));
}
//Visit all the files in the directory and process them
publicstaticvoidvisitAllFiles(Filedir) {
if (dir.isDirectory()) {
String[] children = dir.list();
if (children.length == 0) {
System.out.println("There is no order to process.");
}
for (inti = 0; i < children.length; i++) {
visitAllFiles(newFile(dir, children[i]));
}
} else {
readXML(dir);
}
}
/*
* This method reads an XML document and put the content into Vector data
*/
publicstaticvoidreadXML(Filefilename) {
BufferedReaderbufferedReader = null;
try {
//Construct the BufferedReader object
bufferedReader = newBufferedReader(newFileReader(filename));
Stringline = null;
Vectordata = newVector();
while ((line = bufferedReader.readLine()) != null) {
//Add the line from XML file to the message to be sent as JMS msg
data.addElement(line);
}
sendMessage(data);
} catch (FileNotFoundExceptionex) {
ex.printStackTrace();
} catch (IOExceptionex) {
ex.printStackTrace();
} finally {
//Close the BufferedReader
try {
if (bufferedReader != null)
bufferedReader.close();
} catch (IOExceptionex) {
ex.printStackTrace();
}
}
}
privatestaticvoidsendMessage(Vectordata){
StringBufferfinalMessage = newStringBuffer();
inti;
if(data.size() == 0){
System.err.println("***Error: Order file is empty\n");
}else{
finalMessage.append((String)data.elementAt(0));
for(i = 1; i < data.size();i++){
finalMessage.append(' ');
finalMessage.append((String)data.elementAt(i));
}
}
breakdownMsger.sendMessage(finalMessage.toString());
MessageConsumerreplyConsumer;
}
publicstaticvoidcls(){
for (inti = 0 ; i < 10; i ++){
System.out.println();
}
}
}