Skip to content

Commit 003a1d2

Browse files
zhmeishiWANG Guanzhi
authored andcommitted
Merged in zshiad/line-chatbot-for-comp3111-1/master (pull request khwang0#15)
Master Approved-by: WANG Guanzhi <gwangaj@ust.hk>
2 parents 5efb44c + 4421ec5 commit 003a1d2

2 files changed

Lines changed: 377 additions & 8 deletions

File tree

sample-spring-boot-kitchensink/src/main/java/com/example/bot/spring/SQLDatabaseEngine.java

Lines changed: 314 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,13 @@ String getTourNames() throws Exception {
281281
throw new Exception("NOT FOUND");
282282
}
283283

284-
boolean tourFound(String tourID) throws Exception {
284+
boolean tourFound(int tourID) throws Exception {
285285
//Write your code here
286286
Connection connection = getConnection();
287287
boolean result=false;
288288
try {
289289
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM line_tour WHERE id = ?;");
290-
stmt.setString(1, tourID);
290+
stmt.setInt(1, tourID);
291291
ResultSet rs = stmt.executeQuery();
292292
if (rs.next()) {
293293
result=true;
@@ -305,15 +305,25 @@ boolean tourFound(String tourID) throws Exception {
305305
throw new Exception("NOT FOUND");
306306
}
307307

308-
String displayTourOffering(String tourID) throws Exception {
308+
String displayTourOffering(int tourID) throws Exception {
309309
//Write your code here
310310
Connection connection = getConnection();
311311
String result="";
312312
try {
313-
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM line_touroffering;");
313+
PreparedStatement stmt = connection.prepareStatement(
314+
"SELECT id,offer_date,hotel,capacity_max FROM line_touroffering WHERE tour_id = ? AND state < 2 AND id IN "
315+
+ "(SELECT id FROM line_touroffering WHERE tour_id = ? EXCEPT "
316+
+ "SELECT line_touroffering.id FROM line_touroffering, line_booking "
317+
+ "WHERE line_touroffering.tour_id = ? AND line_touroffering.id=line_booking.\"tourOffering_id\" "
318+
+ "AND line_booking.state=2 "
319+
+ "GROUP BY line_touroffering.id "
320+
+ "HAVING COUNT(*) >= line_touroffering.capacity_max);");
321+
stmt.setInt(1, tourID);
322+
stmt.setInt(2, tourID);
323+
stmt.setInt(3, tourID);
314324
ResultSet rs = stmt.executeQuery();
315325
while (rs.next()) {
316-
result += (rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" max people:"+rs.getInt(5)+"\n\n");
326+
result += (rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" max people: "+rs.getInt(4)+"\n\n");
317327
}
318328
rs.close();
319329
stmt.close();
@@ -330,14 +340,14 @@ String displayTourOffering(String tourID) throws Exception {
330340
throw new Exception("NOT FOUND");
331341
}
332342

333-
boolean setBookingTourID(String userID, String tourID) throws Exception {
343+
boolean setBufferTourID(String userID, int tourID) throws Exception {
334344
//Write your code here
335345
Connection connection = getConnection();
336346
int result=0;
337347
try {
338348
PreparedStatement stmt2 = connection.prepareStatement("INSERT INTO line_userchoose (user_id, tour_id) VALUES (?, ?);");
339349
stmt2.setString(1, userID);
340-
stmt2.setString(2, tourID);
350+
stmt2.setInt(2, tourID);
341351
result=stmt2.executeUpdate();
342352
stmt2.close();
343353
connection.close();
@@ -353,5 +363,302 @@ boolean setBookingTourID(String userID, String tourID) throws Exception {
353363
throw new Exception("NOT FOUND");
354364
}
355365

366+
boolean deleteBufferBookingEntry(String userID) throws Exception {
367+
//Write your code here
368+
Connection connection = getConnection();
369+
int result=0;
370+
try {
371+
PreparedStatement stmt2 = connection.prepareStatement("DELETE FROM line_userchoose WHERE user_id = ?;");
372+
stmt2.setString(1, userID);
373+
result=stmt2.executeUpdate();
374+
stmt2.close();
375+
connection.close();
376+
} catch (Exception e) {
377+
log.info(e.toString());
378+
} finally {
379+
380+
}
381+
if (result!=0)
382+
return true;
383+
if (result==0)
384+
return false;
385+
throw new Exception("NOT FOUND");
386+
}
387+
388+
boolean deleteBookingEntry(String userID) throws Exception {
389+
//Write your code here
390+
Connection connection = getConnection();
391+
int result=0;
392+
try {
393+
PreparedStatement stmt2 = connection.prepareStatement("DELETE FROM line_booking WHERE user_id = ? AND state = 0;");
394+
stmt2.setString(1, userID);
395+
result=stmt2.executeUpdate();
396+
stmt2.close();
397+
connection.close();
398+
} catch (Exception e) {
399+
log.info(e.toString());
400+
} finally {
401+
402+
}
403+
if (result!=0)
404+
return true;
405+
if (result==0)
406+
return false;
407+
throw new Exception("NOT FOUND");
408+
}
409+
410+
int getBufferTourID(String userID) throws Exception {
411+
//Write your code here
412+
Connection connection = getConnection();
413+
int result=-1;
414+
try {
415+
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM line_userchoose WHERE user_id = ?;");
416+
stmt.setString(1, userID);
417+
ResultSet rs = stmt.executeQuery();
418+
if (rs.next()) {
419+
result=rs.getInt(2);
420+
}
421+
rs.close();
422+
stmt.close();
423+
connection.close();
424+
} catch (Exception e) {
425+
log.info(e.toString());
426+
} finally {
427+
428+
}
429+
430+
if (1==1)
431+
return result;
432+
throw new Exception("NOT FOUND");
433+
}
434+
435+
boolean tourOfferingFound(int tourID,int tourOfferingID)throws Exception {
436+
//Write your code here
437+
Connection connection = getConnection();
438+
boolean result=false;
439+
try {
440+
PreparedStatement stmt = connection.prepareStatement(
441+
"SELECT id,offer_date,hotel,capacity_max FROM line_touroffering WHERE tour_id = ? AND state < 2 AND id = ? AND id IN "
442+
+ "(SELECT id FROM line_touroffering WHERE tour_id = ? EXCEPT "
443+
+ "SELECT line_touroffering.id FROM line_touroffering, line_booking "
444+
+ "WHERE line_touroffering.tour_id = ? AND line_touroffering.id=line_booking.\"tourOffering_id\" "
445+
+ "AND line_booking.state=2 "
446+
+ "GROUP BY line_touroffering.id "
447+
+ "HAVING COUNT(*) >= line_touroffering.capacity_max);");
448+
stmt.setInt(1, tourID);
449+
stmt.setInt(2, tourOfferingID);
450+
stmt.setInt(3, tourID);
451+
stmt.setInt(4, tourID);
452+
ResultSet rs = stmt.executeQuery();
453+
if (rs.next()) {
454+
result=true;
455+
}
456+
rs.close();
457+
stmt.close();
458+
connection.close();
459+
} catch (Exception e) {
460+
log.info(e.toString());
461+
} finally {
462+
463+
}
464+
if (1==1)
465+
return result;
466+
throw new Exception("NOT FOUND");
467+
}
468+
469+
boolean setBookingTourOfferingID(String userID, int tourOfferingID) throws Exception {
470+
//Write your code here
471+
Connection connection = getConnection();
472+
int result=0;
473+
try {
474+
PreparedStatement stmt2 = connection.prepareStatement("INSERT INTO line_booking (user_id, \"tourOffering_id\") VALUES (?, ?);");
475+
stmt2.setString(1, userID);
476+
stmt2.setInt(2, tourOfferingID);
477+
result=stmt2.executeUpdate();
478+
stmt2.close();
479+
connection.close();
480+
} catch (Exception e) {
481+
log.info(e.toString());
482+
} finally {
483+
484+
}
485+
if (result!=0)
486+
return true;
487+
if (result==0)
488+
return false;
489+
throw new Exception("NOT FOUND");
490+
}
491+
492+
boolean setBookingAdultNumber(String userID,int number) throws Exception {
493+
//Write your code here
494+
Connection connection = getConnection();
495+
int result=0;
496+
try {
497+
PreparedStatement stmt2 = connection.prepareStatement("UPDATE line_booking SET adult_num = ? WHERE user_id = ? AND state = 0;");
498+
stmt2.setInt(1, number);
499+
stmt2.setString(2, userID);
500+
result=stmt2.executeUpdate();
501+
stmt2.close();
502+
connection.close();
503+
} catch (Exception e) {
504+
log.info(e.toString());
505+
} finally {
506+
507+
}
508+
if (result!=0)
509+
return true;
510+
if (result==0)
511+
return false;
512+
throw new Exception("NOT FOUND");
513+
}
514+
515+
boolean setBookingChildrenNumber(String userID,int number) throws Exception {
516+
//Write your code here
517+
Connection connection = getConnection();
518+
int result=0;
519+
try {
520+
PreparedStatement stmt2 = connection.prepareStatement("UPDATE line_booking SET child_num = ? WHERE user_id = ? AND state = 0;");
521+
stmt2.setInt(1, number);
522+
stmt2.setString(2, userID);
523+
result=stmt2.executeUpdate();
524+
stmt2.close();
525+
connection.close();
526+
} catch (Exception e) {
527+
log.info(e.toString());
528+
} finally {
529+
530+
}
531+
if (result!=0)
532+
return true;
533+
if (result==0)
534+
return false;
535+
throw new Exception("NOT FOUND");
536+
}
537+
538+
boolean setBookingToddlerNumber(String userID,int number) throws Exception {
539+
//Write your code here
540+
Connection connection = getConnection();
541+
int result=0;
542+
try {
543+
PreparedStatement stmt2 = connection.prepareStatement("UPDATE line_booking SET toddler_num = ? WHERE user_id = ? AND state = 0;");
544+
stmt2.setInt(1, number);
545+
stmt2.setString(2, userID);
546+
result=stmt2.executeUpdate();
547+
stmt2.close();
548+
connection.close();
549+
} catch (Exception e) {
550+
log.info(e.toString());
551+
} finally {
552+
553+
}
554+
if (result!=0)
555+
return true;
556+
if (result==0)
557+
return false;
558+
throw new Exception("NOT FOUND");
559+
}
560+
561+
boolean setBookingSpecialRequest(String userID,String request) throws Exception {
562+
//Write your code here
563+
Connection connection = getConnection();
564+
int result=0;
565+
try {
566+
PreparedStatement stmt2 = connection.prepareStatement("UPDATE line_booking SET special_request = ? WHERE user_id = ? AND state = 0;");
567+
stmt2.setString(1, request);
568+
stmt2.setString(2, userID);
569+
result=stmt2.executeUpdate();
570+
stmt2.close();
571+
connection.close();
572+
} catch (Exception e) {
573+
log.info(e.toString());
574+
} finally {
575+
576+
}
577+
if (result!=0)
578+
return true;
579+
if (result==0)
580+
return false;
581+
throw new Exception("NOT FOUND");
582+
}
583+
584+
String displaytBookingInformation(String userID) throws Exception {
585+
//Write your code here
586+
Connection connection = getConnection();
587+
String result="";
588+
int result2=0;
589+
String special="";
590+
int adult=0;
591+
int child=0;
592+
int toddler=0;
593+
double fee=0;
594+
double price=0;
595+
try {
596+
PreparedStatement stmt = connection.prepareStatement(
597+
"SELECT line_booking.adult_num, line_booking.child_num, line_booking.toddler_num, line_booking.price, line_booking.special_request "//5
598+
+ "line_touroffering.offer_date, line_touroffering.hotel, line_touroffering.capacity_max, line_touroffering.guide_name, line_touroffering.guide_line "//5
599+
+ "line_tour.name, line_tour.decription, line_tour.duration "//3
600+
+ "FROM line_booking, line_touroffering, line_tour "
601+
+ "WHERE line_booking.user_id = ? AND line_booking.state = 0 "
602+
+ "AND line_booking.\"tourOffering_id\"=line_touroffering.id "
603+
+ "AND line_touroffering.tour_id=line_tour.id;");
604+
stmt.setString(1, userID);
605+
ResultSet rs = stmt.executeQuery();
606+
if (rs.next()) {
607+
adult=rs.getInt(1);
608+
child=rs.getInt(2);
609+
toddler=rs.getInt(3);
610+
price=rs.getDouble(4);
611+
special=rs.getString(5);
612+
result=("Tour name: "+rs.getString(11)+"\n\nDescription: "+rs.getString(12)+"\n\nDuration: "+rs.getInt(13)+"\n\nOffer date: "
613+
+rs.getTimestamp(6)+"\n\nHotel: "+rs.getString(7)+"\n\nMax people: "+rs.getInt(8)+"\n\nGuide name: "+rs.getString(9)
614+
+"\n\nGuide line account: "+rs.getString(10)+"\n\nAdult: "+adult+"\n\nChild: "+child+"\n\nToddler: "+toddler
615+
+"\n\nSpecial request: "+special);
616+
fee = price*adult + price*0.8*child;
617+
result+=("\n\nTotal fee: "+fee);
618+
}
619+
rs.close();
620+
stmt.close();
621+
PreparedStatement stmt2 = connection.prepareStatement(
622+
"UPDATE line_booking SET tour_fee = ?, paid_fee = ? WHERE user_id = ? AND state = 0;");
623+
stmt2.setDouble(1, fee);
624+
stmt2.setDouble(2, 0.0);
625+
stmt2.setString(3, userID);
626+
result2=stmt2.executeUpdate();
627+
stmt2.close();
628+
629+
connection.close();
630+
} catch (Exception e) {
631+
log.info(e.toString());
632+
} finally {
633+
634+
}
635+
if (result == "")
636+
result ="null";
637+
if (result != "")
638+
return result;
639+
throw new Exception("NOT FOUND");
640+
}
641+
642+
boolean setBookingConfirmation(String userID) throws Exception {
643+
//Write your code here
644+
Connection connection = getConnection();
645+
int result=0;
646+
try {
647+
PreparedStatement stmt2 = connection.prepareStatement("UPDATE line_booking SET state = 1 WHERE user_id = ? AND state = 0;");
648+
stmt2.setString(1, userID);
649+
result=stmt2.executeUpdate();
650+
stmt2.close();
651+
connection.close();
652+
} catch (Exception e) {
653+
log.info(e.toString());
654+
} finally {
655+
656+
}
657+
if (result!=0)
658+
return true;
659+
if (result==0)
660+
return false;
661+
throw new Exception("NOT FOUND");
662+
}
356663

357664
}

0 commit comments

Comments
 (0)