-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlightService.java
More file actions
139 lines (120 loc) · 3.99 KB
/
FlightService.java
File metadata and controls
139 lines (120 loc) · 3.99 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FlightService {
private static final String DBCONFIG_FILENAME = "dbconn.properties";
public static void usage() {
/* prints the choices for commands and parameters */
System.out.println();
System.out.println(" *** Please enter one of the following commands *** ");
System.out.println("> login <username> <password>");
System.out.println("> search <origin_city> <destination_city> <direct> <date> <nb itineraries>");
System.out.println("> book <itinerary_id>");
System.out.println("> reservations");
System.out.println("> cancel <reservation_id>");
System.out.println("> quit");
}
public static String[] tokenize(String command) {
String regex = "\"([^\"]*)\"|(\\S+)";
Matcher m = Pattern.compile(regex).matcher(command);
List<String> tokens = new ArrayList<String>();
while (m.find()) {
if (m.group(1) != null) {
tokens.add(m.group(1));
} else {
tokens.add(m.group(2));
}
}
return tokens.toArray(new String[0]);
}
public static void menu(Query q) throws Exception {
/* prepare to read the user's command and parameter(s) */
String command = null;
while (true) {
usage();
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
System.out.print("> ");
command = r.readLine();
String[] tokens = tokenize(command.trim());
if (tokens.length == 0) {
System.out.println("Please enter a command");
continue; // back to top of loop
}
if (tokens[0].equals("login")) {
if (tokens.length == 3) {
/* authenticate the user */
String username = tokens[1];
String password = tokens[2];
q.transaction_login(username,password);
} else {
System.out.println("Error: Please provide a username and password");
}
}
else if (tokens[0].equals("search")) {
/* search for flights */
if (tokens.length == 6) {
String originCity = tokens[1];
// System.out.println(originCity);
String destinationCity = tokens[2];
// System.out.println(destinationCity);
boolean direct = tokens[3].equals("1");
// System.out.println(direct);
Integer day;
Integer count;
try {
day = Integer.valueOf(tokens[4]);
count = Integer.valueOf(tokens[5]);
} catch (NumberFormatException e) {
System.out.println("Failed to parse integer");
continue;
}
System.out.println("Searching for flights");
q.transaction_search_safe(originCity, destinationCity, direct, day, count);
} else {
System.out.println("Error: Please provide all search parameters <origin_city> <destination_city> <direct> <date> <nb itineraries>");
}
}
else if (tokens[0].equals("book")) {
/* book a flight ticket */
if (tokens.length == 2) {
int itinerary_id = Integer.parseInt(tokens[1]);
System.out.println("Booking itinerary.");
q.transaction_book(itinerary_id);
} else {
System.out.println("Error: Please provide an itinerary_id");
}
}
else if (tokens[0].equals("reservations")) {
/* list all reservations */
q.transaction_reservations();
}
else if (tokens[0].equals("cancel")) {
/* cancel a reservation */
if (tokens.length == 2) {
int reservation_id = Integer.parseInt(tokens[1]);
System.out.println("Canceling reservation.");
q.transaction_cancel(reservation_id);
} else {
System.out.println("Error: Please provide a reservation_id");
}
}
else if (tokens[0].equals("quit")) {
System.exit(0);
}
else {
System.out.println("Error: unrecognized command '" + tokens[0] + "'");
}
}
}
public static void main(String[] args) throws Exception {
/* prepare the database connection stuff */
Query q = new Query(DBCONFIG_FILENAME);
q.openConnection();
q.prepareStatements();
menu(q); /* menu(...) does the real work */
q.closeConnection();
}
}