-
Notifications
You must be signed in to change notification settings - Fork 0
/
SchedulingService.java
385 lines (314 loc) · 13.8 KB
/
SchedulingService.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package flightscheduleryjw5018.services;
import flightscheduleryjw5018.data.Booking;
import flightscheduleryjw5018.data.Flight;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
/**
*
* @author Yingjie(Chelsea) Wang
*/
public class SchedulingService {
private static final String DATABASE_URL = "jdbc:derby:https://localhost:1527/FlightSchedulerDBYJW5018";
private static final String USERNAME = "java";
private static final String PASSWORD = "java";
private Connection connection;
public SchedulingService() {
try {
connection = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
} catch (SQLException sqlException) {
sqlException.printStackTrace();
System.exit(1);
}
}
public Collection<Flight> findFlights() {
ArrayList<Flight> flights = new ArrayList();
try {
// JDBC code goes here
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT NAME, SEAT FROM FLIGHT ORDER BY NAME");
while (rs.next()) {
Flight flight = new Flight(rs.getString("NAME"), rs.getInt("SEAT"));
flights.add(flight);
}
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return flights;
}
public Collection<Date> getDays() {
ArrayList<Date> days = new ArrayList();
try {
// JDBC code goes here
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT DATE FROM DAY ORDER BY DATE");
while (rs.next()) {
days.add(rs.getDate("DATE"));
}
} catch (Exception e) {
e.printStackTrace();
}
return days;
}
public boolean bookFlight(String flight, Date day, String customer) {
try {
// Check if there's a seat
PreparedStatement stmt = connection.prepareStatement("SELECT (SEAT - b.BOOKED) AS AVAIL FROM FLIGHT f\n"
+ "CROSS JOIN (SELECT COUNT(CUSTOMER) BOOKED FROM Booking WHERE FLIGHT = ? AND DATE = ? AND BOOKED = 'Y') b\n"
+ "WHERE NAME = ?");
stmt.setString(1, flight);
stmt.setDate(2, new java.sql.Date(day.getTime()));
stmt.setString(3, flight);
ResultSet rs = stmt.executeQuery();
int availableSeats = 0;
if (rs.next()) {
availableSeats = rs.getInt("AVAIL");
}
rs.close();
stmt.close();
PreparedStatement insertStmt = connection.prepareStatement("INSERT INTO BOOKING (FLIGHT, DATE, CUSTOMER, BOOKED, ENTERED) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP)");
insertStmt.setString(1, flight);
insertStmt.setDate(2, new java.sql.Date(day.getTime()));
insertStmt.setString(3, customer);
if (availableSeats > 0) {
insertStmt.setString(4, "Y");
} else {
insertStmt.setString(4, "N");
}
insertStmt.executeUpdate();
insertStmt.close();
return availableSeats > 0;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public Collection<Booking> getFlightStatus(String flight, Date day) {
ArrayList<Booking> bookings = new ArrayList();
try {
PreparedStatement stmt = connection.prepareStatement("SELECT FLIGHT, DATE, CUSTOMER, ENTERED FROM BOOKING WHERE FLIGHT = ? AND DATE = ? AND BOOKED = 'Y' ORDER BY ENTERED ASC");
stmt.setString(1, flight);
stmt.setDate(2, new java.sql.Date(day.getTime()));
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String flightName = rs.getString("FLIGHT");
Date flightDate = rs.getDate("DATE");
String customer = rs.getString("CUSTOMER");
Date entered = rs.getTimestamp("ENTERED");
Booking booking = new Booking(flightName, flightDate, customer, true, (Timestamp) entered);
bookings.add(booking);
}
} catch (Exception e) {
e.printStackTrace();
}
return bookings;
}
public Collection<Booking> getWaitingList(Date day) {
ArrayList<Booking> bookings = new ArrayList();
try {
PreparedStatement stmt = connection.prepareStatement("SELECT FLIGHT, DATE, CUSTOMER, ENTERED FROM BOOKING WHERE DATE = ? AND BOOKED = 'N' ORDER BY ENTERED ASC");
stmt.setDate(1, new java.sql.Date(day.getTime()));
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String flightName = rs.getString("FLIGHT");
Date flightDate = rs.getDate("DATE");
String customer = rs.getString("CUSTOMER");
Date entered = rs.getTimestamp("ENTERED");
Booking booking = new Booking(flightName, flightDate, customer, false, (Timestamp) entered);
bookings.add(booking);
}
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return bookings;
}
public boolean addFlight(String flight, int numberOfSeats) {
try {
PreparedStatement add = connection.prepareStatement("INSERT INTO FLIGHT (NAME, SEAT) VALUES (?, ?)");
add.setString(1, flight);
add.setInt(2, numberOfSeats);
add.executeUpdate();
add.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public String findAlternativeFlight(Date day, String flight) {
try {
PreparedStatement stmt = connection.prepareStatement("SELECT f.NAME AS FLIGHT, (f.SEAT - CASE WHEN b.BOOKED IS NULL THEN 0 ELSE b.BOOKED END) AS AVAIL\n"
+ "FROM FLIGHT f\n"
+ "LEFT OUTER JOIN\n"
+ "(\n"
+ " SELECT FLIGHT, COUNT(*) BOOKED FROM BOOKING WHERE DATE = ? GROUP BY FLIGHT, DATE\n"
+ ") b ON f.NAME = b.FLIGHT WHERE f.NAME != ?");
stmt.setDate(1, new java.sql.Date(day.getTime()));
stmt.setString(2, flight);
ResultSet rs = stmt.executeQuery();
String newFlight = null;
while (rs.next()) {
if (rs.getInt("AVAIL") > 0) {
newFlight = rs.getString("FLIGHT");
break;
}
}
rs.close();
stmt.close();
return newFlight;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public Collection<Booking> dropFlight(String flight) {
ArrayList<Booking> bookings = new ArrayList();
try {
// Try and rebook all customers booked for this flight
PreparedStatement stmt1 = connection.prepareStatement("SELECT CUSTOMER, DATE FROM BOOKING WHERE FLIGHT = ? AND BOOKED = 'Y' ORDER BY DATE ASC");
stmt1.setString(1, flight);
ResultSet rs = stmt1.executeQuery();
while (rs.next()) {
String customer = rs.getString("CUSTOMER");
Date day = rs.getDate("DATE");
String newFlight = findAlternativeFlight(day, flight);
if (newFlight != null) {
bookFlight(newFlight, day, customer);
}
Booking booking = new Booking(newFlight, day, customer, (newFlight != null), new Timestamp(System.currentTimeMillis()));
bookings.add(booking);
}
// Delete all bookings
PreparedStatement stmt2 = connection.prepareStatement("DELETE FROM BOOKING WHERE FLIGHT = ?");
stmt2.setString(1, flight);
stmt2.executeUpdate();
stmt2.close();
// Delete the flight
PreparedStatement stmt3 = connection.prepareStatement("DELETE FROM FLIGHT WHERE NAME = ?");
stmt3.setString(1, flight);
stmt3.executeUpdate();
stmt3.close();
} catch (Exception e) {
e.printStackTrace();
}
return bookings;
}
public boolean addDate(Date date) {
try {
PreparedStatement add = connection.prepareStatement("INSERT INTO DAY (DATE) VALUES (?)");
add.setDate(1, new java.sql.Date(date.getTime()));
add.executeUpdate();
add.close();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public Collection<Booking> cancelFlight(String customer, Date day) {
ArrayList<Booking> bookings = new ArrayList();
try {
// Find all flights the customer booked on this day
PreparedStatement stmt = connection.prepareStatement("SELECT FLIGHT FROM BOOKING WHERE CUSTOMER = ? AND DATE = ? AND BOOKED = 'Y'");
stmt.setString(1, customer);
stmt.setDate(2, new java.sql.Date(day.getTime()));
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String flight = rs.getString("FLIGHT");
// Remove the booking
PreparedStatement stmt2 = connection.prepareStatement("DELETE FROM BOOKING WHERE FLIGHT = ? AND DATE = ? AND CUSTOMER = ? AND BOOKED = 'Y'");
stmt2.setString(1, flight);
stmt2.setDate(2, new java.sql.Date(day.getTime()));
stmt2.setString(3, customer);
stmt2.executeUpdate();
stmt2.close();
// Book a customer from the waiting list
Booking booking = bookWaitlistedCustomer(flight, day);
if (booking != null) {
bookings.add(booking);
}
}
rs.close();
stmt.close();
// Delete all waiting list records as well
PreparedStatement stmt3 = connection.prepareStatement("DELETE FROM BOOKING WHERE CUSTOMER = ? AND DATE = ? AND BOOKED = 'N'");
stmt3.setString(1, customer);
stmt3.setDate(2, new java.sql.Date(day.getTime()));
stmt3.executeUpdate();
stmt3.close();
} catch (Exception e) {
e.printStackTrace();
}
return bookings;
}
public Booking bookWaitlistedCustomer(String flight, Date day) {
try {
// Get the first customer on the waiting list
PreparedStatement stmt = connection.prepareStatement("SELECT CUSTOMER\n"
+ "FROM BOOKING\n"
+ "WHERE FLIGHT = ? AND DATE = ? AND BOOKED = 'N'\n"
+ "ORDER BY ENTERED ASC\n"
+ "FETCH FIRST 1 ROWS ONLY");
stmt.setString(1, flight);
stmt.setDate(2, new java.sql.Date(day.getTime()));
ResultSet rs = stmt.executeQuery();
String customer = null;
if (rs.next()) {
customer = rs.getString("CUSTOMER");
}
rs.close();
stmt.close();
if (customer != null) {
// Book the waitlisted customer
PreparedStatement stmt2 = connection.prepareStatement("UPDATE BOOKING SET BOOKED = 'Y' WHERE FLIGHT = ? AND DATE = ? AND CUSTOMER = ?");
stmt2.setString(1, flight);
stmt2.setDate(2, new java.sql.Date(day.getTime()));
stmt2.setString(3, customer);
stmt2.executeUpdate();
stmt2.close();
Booking booking = new Booking(flight, day, customer, true, new Timestamp(System.currentTimeMillis()));
return booking;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Collection<Booking> getCustomerStatus(String customerName) {
ArrayList<Booking> bookings = new ArrayList();
try {
PreparedStatement stmt = connection.prepareStatement("SELECT FLIGHT, DATE, CUSTOMER, BOOKED, ENTERED FROM BOOKING WHERE CUSTOMER = ? ORDER BY ENTERED ASC");
stmt.setString(1, customerName);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String flightName = rs.getString("FLIGHT");
Date flightDate = rs.getDate("DATE");
String customer = rs.getString("CUSTOMER");
String booked = rs.getString("BOOKED");
Date entered = rs.getTimestamp("ENTERED");
Booking booking = new Booking(flightName, flightDate, customer, "Y".equals(booked) ? true : false, (Timestamp) entered);
bookings.add(booking);
}
rs.close();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return bookings;
}
}