Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.62 KB | None | 0 0
  1. /*
  2. Class: CSE 3330
  3. Semester: Fall 2018
  4. Student Name: Burnes, Jordan, jlb6776
  5. Student ID: 1001426776
  6. Assignment: project #4
  7. */
  8.  
  9.  
  10. import java.sql.*;
  11. import java.util.Scanner;
  12. import java.util.concurrent.TimeUnit;
  13.  
  14. final class Main {
  15.     //Info needed for connection
  16.     final static String user = "jlb6776";
  17.     final static String password = "Apple123";
  18.     final static String db = "jlb6776";
  19.     final static String jdbc = "jdbc:mysql://localhost:3306/"+db+"?user="+user+"&password="+password;
  20.  
  21.     final static Scanner input = new Scanner(System.in);
  22.  
  23.     public static void main ( String[] args ) throws Exception {
  24.         //Connectiong to driver
  25.         Class.forName("com.mysql.jdbc.Driver").newInstance();
  26.         Connection con = DriverManager.getConnection(jdbc);
  27.  
  28.         //Menu Options and main program loop
  29.         while(true){
  30.             System.out.print("\033[H\033[2J");
  31.             System.out.flush();
  32.             int choice = 0;
  33.             System.out.println("Please Type One of the Following Numbers and Press Enter: ");
  34.             System.out.println("1. Check if Pilot is busy on a certain day and show the pilot assignments for this day");
  35.             System.out.println("2. Assign a Pilot to a flight leg instance");
  36.             System.out.println("3. Add a Pilot");
  37.             System.out.println("4. Quit");
  38.  
  39.             //Error checking for invalid input
  40.             try{
  41.                 choice = input.nextInt();
  42.             }
  43.             catch(Exception e){
  44.                 System.out.println("Please Enter a number");
  45.                 //A time delay to make things look nicer
  46.                 TimeUnit.SECONDS.sleep(1);
  47.                 input.next();
  48.                 continue;
  49.             }
  50.             if(choice < 1 || choice > 4){
  51.                 System.out.println("Please Enter 1-4");
  52.                 TimeUnit.SECONDS.sleep(1);
  53.                 continue;
  54.             }
  55.  
  56.             //First choice to search View3 based on Name and date
  57.             if(choice == 1){
  58.                 System.out.println("Please Enter the Pilots Name");
  59.                 String name = input.next();
  60.                 System.out.println("Please Enter the Date in the Following Format(YYYYMMDD): ");
  61.                 String date = input.next();
  62.  
  63.                 //Creating connection and then creating query and error checking for incorrect data in query
  64.                 Statement flyAssignments = con.createStatement();
  65.                 ResultSet query;
  66.                 try{
  67.                     query = flyAssignments.executeQuery("select * from PilotFlyAssignments where Name='" + name + "' AND FDate=" + date + ";");
  68.                 }catch(Exception e){
  69.                     System.out.println("Error: " + e.getMessage());
  70.                     System.out.println("Please enter a key and press enter to go back to the menu");
  71.                     input.next();
  72.                     continue;
  73.                 }
  74.  
  75.                 //Printing out query, or if null saying nothing was found
  76.                 if(query.next() == false){
  77.                     System.out.println("There are no pilots with that Name and Flight Date. ");
  78.                 }
  79.                 while (query.next())
  80.                     System.out.println(query.getString("ID")+" "+query.getString("Name") + " "+query.getString("FLNO")+" "+query.getString("TOCity")+" "+query.getString("FDate"));
  81.  
  82.                 query.close();
  83.                 flyAssignments.close();
  84.                 System.out.println("If you would like to coninue, type in any key then press enter");
  85.                 input.next();
  86.                 continue;
  87.             }
  88.  
  89.             //Second choice for inputing FlightLegInstance
  90.             else if(choice == 2){
  91.                 System.out.println("ID of Pilot: ");
  92.                 String pilotId = input.next();
  93.                 System.out.println("Flight Number: ");
  94.                 String flno = input.next();
  95.                 System.out.println("Seq: ");
  96.                 String seq = input.next();
  97.                 System.out.println("Flight Date In Format(YYYYMMDD");
  98.                 String fldate = input.next();
  99.                 System.out.println("Departure Time In Format(HHMMSS)");
  100.                 String depart = input.next();
  101.                 System.out.println("Arrival Time In Format (HHMMSS)");
  102.                 String arrive = input.next();
  103.  
  104.                 //Adding connection and creating insert string
  105.                 Statement addPilot = con.createStatement();
  106.                 String thing = "insert into FlightLegInstance values(" + flno + ", " + seq + ", " + fldate + ", " + depart + ", " + arrive + ", " + pilotId + ");";
  107.  
  108.                 //Error checking for things such as foregin key constraints
  109.                 try{
  110.                     addPilot.executeUpdate(thing);
  111.                 }catch (Exception e){
  112.                     System.out.println("Error: " + e.getMessage());
  113.                     System.out.println("Please type a key then press enter to go back to the menu");
  114.                     input.next();
  115.                     continue;
  116.                 }
  117.                 addPilot.close();
  118.                 System.out.println("Table Updated, Please wait to be redirected to menu ");
  119.                 TimeUnit.SECONDS.sleep(2);
  120.                 continue;
  121.             }
  122.  
  123.             //Third choice to add a Pilot
  124.             else if(choice == 3){
  125.                 System.out.println("Enter Id");
  126.                 String id = input.next();
  127.                 System.out.println("Enter Name");
  128.                 String name = input.next();
  129.                 System.out.println("Enter Date hired In Format (YYYYMMDD)");
  130.                 String date = input.next();
  131.  
  132.                 //creating connection and insert string
  133.                 Statement addPilot = con.createStatement();
  134.                 String thing = "insert into Pilot values(" + id + ", '" + name + "', " + date + ");";
  135.  
  136.                 //Error checking for things going wrong when adding such as incorrect format of data entered
  137.                 try{
  138.                     addPilot.executeUpdate(thing);
  139.                 }catch(Exception e){
  140.                     System.out.println("Error: " + e.getMessage());
  141.                     System.out.println("Please type a key then press enter to go back to menu");
  142.                     input.next();
  143.                     continue;
  144.                 }
  145.  
  146.                 System.out.println("Pilot Added, Please wait to be redirected to menu");
  147.                 TimeUnit.SECONDS.sleep(2);
  148.                 continue;
  149.             }
  150.             else{
  151.                 con.close();
  152.                 System.exit(0);
  153.             }
  154.         }
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement