-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment2.java
More file actions
354 lines (333 loc) · 14.2 KB
/
Assignment2.java
File metadata and controls
354 lines (333 loc) · 14.2 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
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
/*
* This code is provided solely for the personal and private use of students
* taking the CSC343H course at the University of Toronto. Copying for purposes
* other than this use is expressly prohibited. All forms of distribution of
* this code, including but not limited to public repositories on GitHub,
* GitLab, Bitbucket, or any other online platform, whether as given or with
* any changes, are expressly prohibited.
*/
import java.sql.*;
import java.util.Date;
import java.util.Arrays;
import java.util.List;
import java.lang.Math;
import java.util.ArrayList;
public class Assignment2 {
/////////
// DO NOT MODIFY THE VARIABLE NAMES BELOW.
// A connection to the database
Connection connection;
// Can use if you wish: seat letters
List<String> seatLetters = Arrays.asList("A", "B", "C", "D", "E", "F");
Assignment2() throws SQLException {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* Connects and sets the search path.
*
* Establishes a connection to be used for this session, assigning it to
* the instance variable 'connection'. In addition, sets the search
* path to 'air_travel, public'.
*
* @param url the url for the database
* @param username the username to connect to the database
* @param password the password to connect to the database
* @return true if connecting is successful, false otherwise
*/
public boolean connectDB(String URL, String username, String password) {
try{
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(URL, username, password);
Statement state = connection.createStatement();
String query = "SET search_path TO air_travel, public;";
state.executeUpdate(query);
} catch (Exception e){
e.printStackTrace();
return false;
}
return true;
}
/**
* Closes the database connection.
*
* @return true if the closing was successful, false otherwise
*/
public boolean disconnectDB() {
// Implement this method!
try{
connection.close();
} catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
/* ======================= Airline-related methods ======================= */
/**
* Attempts to book a flight for a passenger in a particular seat class.
* Does so by inserting a row into the Booking table.
*
* Read handout for information on how seats are booked.
* Returns false if seat can't be booked, or if passenger or flight cannot be found.
*
*
* @param passID id of the passenger
* @param flightID id of the flight
* @param seatClass the class of the seat (economy, business, or first)
* @return true if the booking was successful, false otherwise.
*/
public boolean bookSeat(int passID, int flightID, String seatClass) {
// Implement this method!
int ret = 0;
try{
String query = "SELECT * FROM plane,booking,flight,price WHERE booking.flight_id = flight.id AND flight.plane = plane.tail_number AND price.flight_id = flight.id AND flight.airline = plane.airline AND flight.id = " + flightID + ";";
Statement stat = connection.createStatement();
ResultSet ans = stat.executeQuery(query);
int capacity_first = 0, capacity_eco = 0, capacity_bus = 0, ct_f = 0, ct_b = 0, ct_e = 0, price_e = 0, price_f = 0, price_b = 0;
int row_e = 0, row_f = 0, row_b = 0, id= 0;
String seat_e = "", seat_f = "", seat_b = "";
while(ans.next()){
capacity_first = ans.getInt("capacity_first");
capacity_eco = ans.getInt("capacity_economy");
capacity_bus = ans.getInt("capacity_business");
price_e = ans.getInt("economy");
price_f = ans.getInt("first");
price_b = ans.getInt("business");
if(ans.getString("seat_class").equals("first")){
ct_f += 1;
if(ans.getInt("row")>row_f){
row_f = ans.getInt("row");
seat_f = ans.getString("letter");
}
}
if(ans.getString("seat_class").equals("business")){
ct_b += 1;
if(ans.getInt("row")>row_b){
row_b = ans.getInt("row");
seat_b = ans.getString("letter");
}
}
if(ans.getString("seat_class").equals("economy")){
ct_e += 1;
if(ans.getInt("row") > 0 && ans.getInt("row")>row_e){
row_e = ans.getInt("row");
seat_e = ans.getString("letter");
}
}
}
query = "SELECT * FROM booking";
ans = stat.executeQuery(query);
while(ans.next()){
id = ans.getInt("id");
}
id = id + 1;
if(row_f == 0){
row_f = 1;
}
if(row_b == 0){
row_b = (int)(Math.ceil(capacity_first / 6)) + 1;
}
if(row_e == 0){
row_e = (int)(Math.ceil(capacity_first / 6)) + (int)(Math.ceil(capacity_bus / 6)) + 1;
}
if(seatClass.equals("first")){
if(ct_f < capacity_first * 6){
ret = 1;
if(seat_f.equals("F")){
row_f = row_f + 1;
query = "INSERT INTO booking VALUES (" + id + ", " + passID + ", " + flightID + ", '" + getCurrentTimeStamp() + "', " + price_f + ", " + "'first', " + row_f + ", 'A');";
stat.executeUpdate(query);
}
else{
int pos = seatLetters.indexOf(seat_f) + 1;
query = "INSERT INTO booking VALUES (" + id + ", " + passID + ", " + flightID + ", '" + getCurrentTimeStamp() + "', " + price_f + ", " + "'first', " + row_f + ", '" + seatLetters.get(pos)+ "');";
stat.executeUpdate(query);
}
}
}
else if(seatClass.equals("business")){
if(ct_b < capacity_bus * 6){
ret = 1;
if(seat_b.equals("F")){
row_b = row_b + 1;
query = "INSERT INTO booking VALUES (" + id + ", " + passID + ", " + flightID + ", '" + getCurrentTimeStamp() + "', " + price_b + ", " + "'business', " + row_b + ", 'A');";
stat.executeUpdate(query);
}
else{
int pos = seatLetters.indexOf(seat_b) + 1;
query = "INSERT INTO booking VALUES (" + id + ", " + passID + ", " + flightID + ", '" + getCurrentTimeStamp() + "', " + price_b + ", " + "'business', " + row_b + ", '" + seatLetters.get(pos)+ "');";
stat.executeUpdate(query);
}
}
}
else {
if(ct_e < capacity_eco * 6){
ret = 1;
if(seat_e.equals("F")){
row_e = row_e + 1;
query = "INSERT INTO booking VALUES (" + id + ", " + passID + ", " + flightID + ", '" + getCurrentTimeStamp() + "', " + price_e + ", " + "'economy', " + row_e + ", 'A');";
stat.executeUpdate(query);
System.out.println("Here");
}
else{
int pos = seatLetters.indexOf(seat_e) + 1;
query = "INSERT INTO booking VALUES (" + id + ", " + passID + ", " + flightID + ", '" + getCurrentTimeStamp() + "', " + price_e + ", " + "'economy', " + row_e + ", '" + seatLetters.get(pos)+ "');";
stat.executeUpdate(query);
}
}
else if(ct_e >= capacity_eco * 6 && ct_e < (capacity_eco*6 + 10)){
query = "INSERT INTO booking VALUES (" + id + ", " + passID + ", " + flightID + ", '" + getCurrentTimeStamp() + "', " + price_e + ", " + "'economy',,);";
stat.executeUpdate(query);
}
}
} catch(Exception e){
e.printStackTrace();
return false;
}
if(ret == 1){
return true;
}
return false;
}
/**
* Attempts to upgrade overbooked economy passengers to business class
* or first class (in that order until each seat class is filled).
* Does so by altering the database records for the bookings such that the
* seat and seat_class are updated if an upgrade can be processed.
*
* Upgrades should happen in order of earliest booking timestamp first.
*
* If economy passengers are left over without a seat (i.e. more than 10 overbooked passengers or not enough higher class seats),
* remove their bookings from the database.
*
* @param flightID The flight to upgrade passengers in.
* @return the number of passengers upgraded, or -1 if an error occured.
*/
public int upgrade(int flightID) {
try{
String query = "SELECT * FROM plane,booking,flight,price WHERE booking.flight_id = flight.id AND flight.plane = plane.tail_number AND price.flight_id = flight.id AND flight.airline = plane.airline AND flight.id = " + flightID + ";";
Statement stat = connection.createStatement();
ResultSet ans = stat.executeQuery(query);
int capacity_first = 0, capacity_eco = 0, capacity_bus = 0, ct_f = 0, ct_b = 0, ct_e = 0, price_e = 0, price_f = 0, price_b = 0;
int row_e = 0, row_f = 0, row_b = 0, ct = 0;
String seat_e = "", seat_f = "", seat_b = "";
while(ans.next()){
capacity_first = ans.getInt("capacity_first");
capacity_eco = ans.getInt("capacity_economy");
capacity_bus = ans.getInt("capacity_business");
price_e = ans.getInt("economy");
price_f = ans.getInt("first");
price_b = ans.getInt("business");
if(ans.getString("seat_class").equals("first")){
ct_f += 1;
if(ans.getInt("row")>row_f){
row_f = ans.getInt("row");
seat_f = ans.getString("letter");
}
}
if(ans.getString("seat_class").equals("business")){
ct_b += 1;
if(ans.getInt("row")>row_b){
row_b = ans.getInt("row");
seat_b = ans.getString("letter");
}
}
if(ans.getString("seat_class").equals("economy")){
ct_e += 1;
if(ans.getInt("row") > 0 && ans.getInt("row")>row_e){
row_e = ans.getInt("row");
seat_e = ans.getString("letter");
}
}
}
int num_avail = (capacity_bus * 6) - ct_b + (capacity_first * 6) - ct_f;
int ct_ret = 0;
if(num_avail <= 0){
return 0;
}
System.out.println("Hi");
query = "SELECT * FROM booking WHERE row IS NULL AND flight_id = " + flightID + " ORDER BY datetime;";
ans = stat.executeQuery(query);
List<String> queries = new ArrayList<String>();
System.out.println(queries);
while(ans.next()){
int id = ans.getInt("id");
if(num_avail > 0){
ct_ret += 1;
if(ct_b < capacity_bus * 6){
if(seat_b.equals("F")){
row_b = row_b + 1;
queries.add("UPDATE booking SET seat_class = 'business', row = "+ row_b + ", letter = 'A' WHERE id = " + id + ";");
}
else{
int pos = seatLetters.indexOf(seat_b) + 1;
queries.add("UPDATE booking SET seat_class = 'business', row = "+ row_b + ", letter = '" + seatLetters.get(pos) + "' WHERE id = " + id + ";");
}
}
else{
if(seat_f.equals("F")){
row_f = row_f + 1;
queries.add("UPDATE booking SET seat_class = 'first', row = "+ row_f + ", letter = 'A' WHERE id = " + id + ";");
}
else{
int pos = seatLetters.indexOf(seat_f) + 1;
queries.add("UPDATE booking SET seat_class = 'first', row = "+ row_f + ", letter = '" + seatLetters.get(pos) + "' WHERE id = " + id + ";");
}
}
}
else{
queries.add("DELETE FROM booking WHERE id = " + id + ";");
}
num_avail -= 1;
}
for(String q: queries){
stat.executeUpdate(q);
}
query = "SELECT * FROM booking WHERE row IS NULL AND flight_id = " + flightID + " ORDER BY datetime;";
ans = stat.executeQuery(query);
while(ans.next()){
System.out.println("hi");
}
return ct_ret;
} catch(Exception e){
e.printStackTrace();
return -1;
}
}
/* ----------------------- Helper functions below ------------------------- */
// A helpful function for adding a timestamp to new bookings.
// Example of setting a timestamp in a PreparedStatement:
// ps.setTimestamp(1, getCurrentTimeStamp());
/**
* Returns a SQL Timestamp object of the current time.
*
* @return Timestamp of current time.
*/
private java.sql.Timestamp getCurrentTimeStamp() {
java.util.Date now = new java.util.Date();
return new java.sql.Timestamp(now.getTime());
}
// Add more helper functions below if desired.
/* ----------------------- Main method below ------------------------- */
public static void main(String[] args) {
// You can put testing code in here. It will not affect our autotester.
// System.out.println("Running the code!");
String url = "jdbc:postgresql://localhost:5432/csc343h-gurumur2";
String username = "gurumur2";
String password = "";
try{
Assignment2 a = new Assignment2();
System.out.println(a.connectDB(url, username, password));
// System.out.println("connect done!");
a.bookSeat(6,5,"first");
System.out.println(a.upgrade(5));
// System.out.println("boook done!");
a.disconnectDB();
}
catch(Exception e){}
}
}