-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlightService.java
135 lines (125 loc) · 4.81 KB
/
FlightService.java
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
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 {
public 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("> create <username> <password> <Email-ID>");
System.out.println("> login <username> <password>");
System.out.println("> logout");
System.out.println("> add");
System.out.println("> view");
System.out.println("> commit <message>");
System.out.println("> versions <project id>");
System.out.println("> delete <project id>");
System.out.println("> push");
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<>();
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 String execute(Query q, String command) {
String response;
String[] tokens = tokenize(command.trim());
if (tokens.length == 0) response = "Please enter a command";
else if (tokens[0].equals("login")) {
if (tokens.length == 3) {
/* authenticate the user */
String username = tokens[1];
String password = tokens[2];
response = q.loginUser(username, password);
} else response = "Error: Please provide a username and password";
} else if (tokens[0].equals("create")) {
/* create a new customer */
if (tokens.length == 4) {
String username = tokens[1];
String password = tokens[2];
String email = tokens[3];
response = q.createUser(username, password, email);
} else response = "Error: Please provide a username, password, and email for the account";
} else if (tokens[0].equals("logout")) {
response = q.logOut();
} else if (tokens[0].equals("add")) {
response = q.add();
} else if (tokens[0].equals("view")) {
response = q.view();
} else if (tokens[0].equals("delete")) {
if (tokens.length == 2) {
int project_id = Integer.parseInt(tokens[1]);
response = q.delete(project_id);
} else {
response = "Error: Please provide a project_id";
}
} else if (tokens[0].equals("commit")) {
if (tokens.length == 2) {
String message = tokens[1];
response = q.commit(message);
} else {
response = "Error: Please provide a commit message";
}
} else if (tokens[0].equals("push")) {
response = q.push();
} else if (tokens[0].equals("versions")) {
if (tokens.length == 2) {
String id = tokens[1];
response = q.versions(Integer.parseInt(id));
} else {
response = "Error: Please provide a version number";
}
}
// } else if (tokens[0].equals("reservations")) {
// /* list all reservations */
// response = q.transaction_reservations();
// } else if (tokens[0].equals("pay")) {
// /* pay for an unpaid reservation */
// if (tokens.length == 2) {
// int reservation_id = Integer.parseInt(tokens[1]);
// // System.out.println("Paying reservation.");
// response = q.transaction_pay(reservation_id);
// } else response = "Error: Please provide a reservation_id";
// } 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.");
// response = q.transaction_cancel(reservation_id);
// } else response = "Error: Please provide a reservation_id";
// }
else if (tokens[0].equals("quit")) response = "Goodbye\n";
else response = "Error: unrecognized command '" + tokens[0] + "'";
return response;
}
/* REPL (Read-Execute-Print-Loop) */
public static void menu(Query q) throws Exception {
while (true) {
usage();
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
System.out.print("> ");
String command = r.readLine();
String response = execute(q, command);
System.out.print(response);
if (response.equals("Goodbye\n")) break;
}
}
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();
}
}