Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.61 KB | None | 0 0
  1. import java.io.File;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. import java.time.LocalDate;
  7. import java.time.format.DateTimeFormatter;
  8. import java.util.Scanner;
  9.  
  10.  
  11.  
  12. public class Main {
  13.  
  14.     public static Connection con;
  15.  
  16.     static {
  17.         try {
  18.             con = DBClass.getConnection();
  19.         } catch (ClassNotFoundException e) {
  20.             e.printStackTrace();
  21.         } catch (SQLException e) {
  22.             e.printStackTrace();
  23.         }
  24.     }
  25.  
  26.     public static void main(String[] args) throws SQLException, ClassNotFoundException {
  27.         Scanner input = new Scanner(System.in);
  28.         Statement state;
  29.         String response = "";
  30.         boolean DBExists = false;
  31.         try {
  32.             con = DBClass.getConnection();
  33.             DBClass.buildDatabase(DBExists);
  34.             state = con.createStatement();
  35.             input.nextLine();
  36.  
  37.             while (!response.equals("stop")) {
  38.                 System.out.println("\n\nWhat do you want to do? You can type 'reset' to reset the passengers " +
  39.                         "and schedules tables, 'file' to load records from a " +
  40.                         "file. Type 'stop' to exit. Do not type the quotations");
  41.                 response = input.nextLine();
  42.                 switch (response) {
  43.                     case "reset":
  44.                         DBClass.tableReset();
  45.                         break;
  46.                     case "file":
  47.                         System.out.println("What is the name of the file you want to load in. Example: 'sample.txt' without the quotations");
  48.                         Scanner sc = new Scanner(new File(input.next()));
  49.  
  50.                         while (sc.hasNextLine()) {
  51.                             String line = sc.nextLine();
  52.                             if (line.charAt(0) == 'P') {
  53.                                 insertIntoPassenger(line, state);
  54.                             } else if (line.charAt(0) == 'S') {
  55.                                 insertIntoScheduleTable(line);
  56.                             }
  57.                         }
  58.                         sc.close();
  59.                         break;
  60.                 }
  61.             }
  62.  
  63.             } catch(Exception e){
  64.                 e.printStackTrace();
  65.             }
  66.             finally {
  67.             con.close();
  68.         }
  69.  
  70.     }
  71.  
  72.     public static void insertIntoPassenger(String str, Statement state) throws SQLException {
  73.         String[] strArr = str.split(" ");
  74.         String sqlIns = "INSERT INTO Passengers_Table VALUES(" + strArr[1] + ",'" +
  75.                 strArr[2] + "','" + strArr[3] + "','" + strArr[4] + "','" + strArr[5] + "');";
  76.         state.executeUpdate(sqlIns);
  77.         state.close();
  78.     }
  79.  
  80.     public static void insertIntoScheduleTable(String str) throws SQLException, ClassNotFoundException {
  81.         Statement state2 = con.createStatement();
  82.         Statement state3 = con.createStatement();
  83.         Statement state4 = con.createStatement();
  84.  
  85.         DateTimeFormatter frmt = DateTimeFormatter.ofPattern("MM/dd/yyyy");
  86.         String[] line = str.split(" ");
  87.         int passengerID = Integer.parseInt(line[1]);
  88.         int planePref;
  89.         String seatType = line[3];
  90.         String prefDate = line[4];
  91.         ResultSet rs;
  92.         int count = 0;
  93.  
  94.         if(!line[2].toUpperCase().equals("X")){
  95.             planePref = Integer.parseInt(line[2]);
  96.  
  97.             if(planePref > 3){
  98.                 planePref = 1;
  99.                 LocalDate ld = LocalDate.parse(prefDate, frmt);
  100.                 ld = ld.plusDays(1);
  101.                 prefDate = ld.format(frmt);
  102.             }
  103.  
  104.             //if person is luxury
  105.             if(seatType.equals("L")){
  106.                 count = 0;
  107.                 rs = state2.executeQuery("SELECT * FROM Schedules_Table WHERE Seat_Section = 'L' AND Flight_Date ='" + prefDate + "' AND Flight_TUID = " + planePref +";");
  108.  
  109.                 while(rs.next()){
  110.                     count++;
  111.                 }
  112.                 state2.close();
  113.                 //check to see if luxury is full
  114.                 if(count >= findMaxSeats(seatType, planePref)){
  115.                     ++planePref;
  116.                     line[2] = String.valueOf(planePref);
  117.                     insertIntoScheduleTable(buildString(line));
  118.                 } else {
  119.                     String insertS = "INSERT INTO Schedules_Table (Passenger_TUID, Flight_TUID, Flight_Date, Seat_Section, Seat_Number, Price_Paid)" +
  120.                             " VALUES(" + passengerID + "," + planePref + ",'"
  121.                             + prefDate + "','" + seatType + "'," + (count + 1) + "," + 2500 + ");";
  122.                     state3.executeUpdate(insertS);
  123.                 }
  124.  
  125.  
  126.             } else {
  127.                 //LOGIC FOR VIP
  128.                 rs = state2.executeQuery("SELECT * FROM Schedules_Table WHERE Seat_Section = 'V' AND Flight_Date ='" + prefDate + "' AND Flight_TUID = " + planePref +";");
  129.  
  130.                 while(rs.next()){
  131.                     count++;
  132.                 }
  133.  
  134.                 state2.close();
  135.  
  136.                 if(count >= findMaxSeats(seatType, planePref)){
  137.                     //kick out a luxury
  138.                     rs = state2.executeQuery("SELECT * FROM Schedules_Table WHERE Seat_Section = 'L' AND Price_Paid = 2500 AND Flight_Date ='" + prefDate + "' AND Flight_TUID = " + planePref +";");
  139.                     boolean seatFound = false;
  140.                     while(rs.next()){
  141.                         if(rs.getString("Seat_Section").equals("L")){
  142.                             String[] temp = {"S", rs.getString("Passenger_TUID"), rs.getString("Flight_TUID"), "L", rs.getString("Flight_Date")};
  143.                             state4.executeUpdate("UPDATE Schedules_Table SET Passenger_TUID =" + passengerID +", Price_Paid = 4000 WHERE Passenger_TUID = " + rs.getString("Passenger_TUID") + ";");
  144.                             insertIntoScheduleTable(buildString(temp));
  145.                             seatFound = true;
  146.                             break;
  147.                         }
  148.                     }
  149.  
  150.                     if(!seatFound){
  151.                         ++planePref;
  152.                         line[2] = String.valueOf(planePref);
  153.                         insertIntoScheduleTable(buildString(line));
  154.                     }
  155.  
  156.                 } else {
  157.                     String insertS = "INSERT INTO Schedules_Table (Passenger_TUID, Flight_TUID, Flight_Date, Seat_Section, Seat_Number, Price_Paid)" +
  158.                             " VALUES(" + passengerID + "," + planePref + ",'"
  159.                             + prefDate + "','" + seatType + "'," + (count + 1) + "," + 4000 + ");";
  160.                     state3.executeUpdate(insertS);
  161.                 }
  162.             }
  163.         } else {
  164.             line[2] = String.valueOf(1);
  165.             insertIntoScheduleTable(buildString(line));
  166.         }
  167.  
  168.  
  169.     }
  170.  
  171.     public static String buildString(String[] str){
  172.         String newLine = "";
  173.         for(int i = 0; i < str.length; i++){
  174.             newLine += str[i] + " ";
  175.         }
  176.  
  177.  
  178.         return newLine;
  179.     }
  180.  
  181.     public static int findMaxSeats(String seatType, int flightID){
  182.         if(flightID == 1){
  183.             if (seatType.equals("V")) {
  184.                 return 4;
  185.             } else {
  186.                 return 6;
  187.             }
  188.         }else if(flightID == 2){
  189.             if (seatType.equals("V")) {
  190.                 return 3;
  191.             } else {
  192.                 return 5;
  193.             }
  194.         } else {
  195.             if (seatType.equals("V")) {
  196.                 return 6;
  197.  
  198.             } else {
  199.                 return 8;
  200.             }
  201.         }
  202.  
  203.     }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement