-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrainManagementSystem.java
More file actions
182 lines (145 loc) · 5.31 KB
/
TrainManagementSystem.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
import java.util.*;
import javax.jms.*;
import java.io.*;
import java.lang.*;
public class TrainManagementSystem{
public static String serverUrl;
public static TrainMsgSyncProducer breakdownMsger;
public static TrainMsgProducer resumeServiceMsger;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
boolean run = 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: ");
int input = sc.nextInt();
run = routeInput(input,sc);
}
}
//Working
public static boolean initializeMsgProducers(){
breakdownMsger = new TrainMsgSyncProducer(serverUrl,"q.breakdown","q.deployed",TrainMsgProducer.QUEUE);
resumeServiceMsger = new TrainMsgProducer(serverUrl,"q.resumed",TrainMsgProducer.QUEUE);
return true;
}
//Route the input from main
public static boolean routeInput(int input, Scanner sc){
if(input == 1){
updateURL(sc);
cls();
return true;
}
if(input == 2){
messageScreen();
cls();
return true;
}
if (input == 3){
systemResumedScreen();
cls();
return true;
}
cls();
return false;
}
public static void updateURL(Scanner sc){
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();
}
public static void messageScreen(){
processAllFiles(); //This process the XML files and send it
}
public static void systemResumedScreen(){
System.out.println("Informing q.resumed that systems has resumed");
resumeServiceMsger.sendMessage("SYSTEM RESUMED");
System.out.println("MESSAGE SENT");
}
public static void processAllFiles(){
String address = "C://EI//Project//breakdownReports";
visitAllFiles(new File(address));
}
//Visit all the files in the directory and process them
public static void visitAllFiles(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
if (children.length == 0) {
System.out.println("There is no order to process.");
}
for (int i = 0; i < children.length; i++) {
visitAllFiles(new File(dir, children[i]));
}
} else {
readXML(dir);
}
}
/*
* This method reads an XML document and put the content into Vector data
*/
public static void readXML(File filename) {
BufferedReader bufferedReader = null;
try {
//Construct the BufferedReader object
bufferedReader = new BufferedReader(new FileReader(filename));
String line = null;
Vector data = new Vector();
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 (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
//Close the BufferedReader
try {
if (bufferedReader != null)
bufferedReader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private static void sendMessage(Vector data){
StringBuffer finalMessage = new StringBuffer();
int i;
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());
MessageConsumer replyConsumer;
}
public static void cls(){
for (int i = 0 ; i < 10; i ++){
System.out.println();
}
}
}