georgeB96

Untitled

Apr 15th, 2022
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.62 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.io.BufferedWriter;
  4. import java.io.FileWriter;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7.  
  8. public class BoardingSys {
  9.  
  10.     //declaring scanner to take input
  11.     static Scanner kb = new Scanner(System.in);
  12.    
  13.     public static void main(String[] args) throws Exception {
  14.        
  15.        
  16.        
  17.         //declaring the cabins array
  18.         Cabin[] cabins = new Cabin[12];
  19.         for(int i=0;i<cabins.length;i++) {
  20.             cabins[i] = new Cabin();
  21.         }
  22.        
  23.        
  24.        
  25.        
  26.         while(true) {
  27.             System.out.println("Enter option: "
  28.                     + "\nA - to add cabin crew, "
  29.                     + "\nV - to view all cabins,"
  30.                     + "\nE - print empty cabins,"
  31.                     + "\nD - delete customer from cabin,"
  32.                     + "\nF - find cabin using Customer name,"
  33.                     + "\nS - store program data into file,"
  34.                     + "\nL - load program data from the file,"
  35.                     + "\nO - view passengers in alphabetical order");
  36.            
  37.             char option = kb.next().charAt(0); // reads String from keyboard and save it into option
  38.             System.out.println("You selected option: " + option);
  39.             kb.nextLine(); //fixes issues with nextLine()
  40.             //code for option A
  41.            
  42.             switch (option) {
  43.                 case 'A':
  44.                     cabins = addCustomer(cabins);
  45.                     break;
  46.                 case 'V':
  47.                     printCabins(cabins);
  48.                     break;
  49.                 case 'E':
  50.                     printEmptyCabins(cabins);
  51.                     break;
  52. //                case 'D':
  53. //                    cabins = deleteCustomerFromCabin(cabins);
  54. //                    break;
  55. //                case 'F':
  56. //                    findCabinWithCusName(cabins);
  57. //                    break;
  58. //                case 'S':
  59. //                    storeDataInFile(cabins);
  60. //                    break;
  61. //                case 'L':
  62. //                    cabins = loadProgramData();
  63. //                    break;
  64. //                case 'O':
  65. //                    viewPassengerInOrder(cabins);
  66. //                    break;
  67.                 default:
  68.                     System.out.println("Invalid option");
  69.                     break;
  70.             }  
  71.         }
  72.     }
  73.     private static void viewPassengerInOrder(String[] cabins) {
  74.        
  75.         String[] cabinsSorted = new String[12];
  76.        
  77.         for(int i =0; i<cabins.length;i++) {
  78.             if(!(cabins[i]== null)) {
  79.                 for(int j=i+1; j<cabins.length; j++) {
  80.                     //compares each elements of the array to all the remaining elements
  81.                     if(!(cabins[j]==null)) {
  82.                         if(cabins[i].compareTo(cabins[j])>0) {
  83.                            
  84.                             //swap elements with each other
  85.                             String temp = cabins[i];
  86.                             cabins[i] = cabins[j];
  87.                             cabins[j] = temp;
  88.                         }
  89.                     }
  90.                    
  91.                 }
  92.             }
  93.            
  94.         }
  95.        
  96.         System.out.println("Viewing all cabins: ");
  97.         for(int i=0;i<cabins.length;i++) {
  98.             if(!(cabins[i] == null)) {
  99.                 System.out.println(cabins[i]);
  100.             }
  101.         }
  102.        
  103.        
  104.     }
  105.     private static String[] loadProgramData() throws Exception {
  106.         System.out.println("Loading data from CabinData.txt");
  107.         BufferedReader reader = new BufferedReader(new FileReader("CabinData.txt"));
  108.         String[] cabins = new String[12];
  109.         for (int i=0; i<cabins.length;i++) {
  110.             String cabinCus = reader.readLine();
  111.             //if cabinCus is string null, transform it to NULL type
  112.             if(cabinCus.equals("null")) {
  113.                 cabins[i] = null;
  114.             }else {
  115.                 cabins[i] = cabinCus;
  116.             }
  117.            
  118.         }
  119.         reader.close();
  120.         return cabins;
  121.     }
  122.     private static void storeDataInFile(String[] cabins) throws Exception {
  123.         System.out.println("Saving data in CabinData.txt");
  124.         BufferedWriter writer = new BufferedWriter(new FileWriter("CabinData.txt", false));
  125.         for(int i=0;i<cabins.length;i++) {
  126.             String message = cabins[i];
  127.              writer.write(message);
  128.              writer.newLine();
  129.         }
  130.         writer.flush();
  131.         System.out.println("Data successfully saved");
  132.        
  133.     }
  134.     private static void findCabinWithCusName(String[] cabins) {
  135.         System.out.println("Enter customer name:");
  136.         String cusName = kb.nextLine();
  137.         boolean found = false;
  138.         for(int i=0; i<cabins.length;i++) {
  139.             //check to see if its not null first so we can use .equals to check the customer name
  140.             //Cannot invoke "String.equals(Object)" because "cabins[i]" is null
  141.             if(!(cabins[i] == null)) {
  142.                 if(cabins[i].equals(cusName)) {
  143.                     System.out.println("Customer " + cusName + " is in cabin " + (i+1));
  144.                     found = true;
  145.                     break;
  146.                 }
  147.             }
  148.         }
  149.         if(!found) {
  150.             System.out.println("Customer " + cusName + " is not on the ship.");
  151.         }
  152.        
  153.        
  154.     }
  155.     private static String[] deleteCustomerFromCabin(String[] cabins) {
  156.        
  157.         //delete customer by cabin index/id
  158.         System.out.println("Please enter the index of the cabin you would like to empty: ");
  159.         int cabinIndex = kb.nextInt() - 1;
  160.         System.out.println("Customer "+ cabins[cabinIndex] +" has been removed from the cabin");
  161.         cabins[cabinIndex] = null;
  162.         return cabins;
  163.        
  164.     }
  165.     private static void printEmptyCabins(Cabin[] cabins) {
  166.         System.out.println("Viewing all empty cabins: ");
  167.         for(int i=0;i<cabins.length;i++) {
  168.             Passenger[] passengers = cabins[i].getPassengers();
  169.             for(int j=0;j<passengers.length;j++) {
  170.                 if(passengers[j] != null) {
  171.                     break;
  172.                 }
  173.                 if(j == passengers.length-1) {
  174.                     System.out.println("Cabin number: " + (i+1) + " is empty.");
  175.                 }
  176.                 //magic numbers; works if there are few passengers as it doesn't require much code to write
  177. ////            if(passengers[0] == null && passengers[1] == null && passengers[2] == null) {
  178. //               System.out.println("Cabin number: " + (i+1) + " is empty.");
  179.             }
  180.         }
  181.     }
  182.     private static void printCabins(Cabin[] cabins) {
  183.         System.out.println("Viewing all cabins: ");
  184.         for(int i=0;i<cabins.length;i++) {
  185.             Passenger[] passengers = cabins[i].getPassengers();
  186.             System.out.println("Passengers in cabin: " + (i+1));
  187.             for(int j=0; j<passengers.length;j++) {
  188.                 if(!(passengers[j] == null)) {
  189.                     System.out.print(passengers[j].toString());
  190.                     System.out.println();
  191.                 }
  192.             }
  193.         }
  194.        
  195.     }
  196.     //add customer method that takes the old cabins array and returns the new cabins array
  197.     private static Cabin[] addCustomer(Cabin[] cabins) {
  198.        
  199.         boolean entered = false;
  200.        
  201.         for(int i=0;i<cabins.length;i++) {
  202.             Passenger[] passengers = cabins[i].getPassengers();
  203.             for(int j=0;j<passengers.length;j++) {
  204.                 if(passengers[j] == null) {
  205.                     Passenger p = new Passenger();
  206.                    
  207.                     //read from keyboard passenger details
  208.                     System.out.println("Insert First Name: ");
  209.                     p.setFirstName(kb.nextLine());
  210.                     System.out.println("Insert Sur Name: ");
  211.                     p.setSurName(kb.nextLine());
  212.                     System.out.println("Inser Expenses: ");
  213.                     //thorws error in case the value is not Double/Long
  214.                     p.setExpenses(kb.nextDouble());
  215.                    
  216.                     //add passenger to array of first cabin
  217.                     passengers[j] = p;
  218.                     entered = true;
  219.                     System.out.println("Passenger "+ p.getFirstName() +" is added to the cabin: " + (i+1));
  220.                     break;
  221.                 }
  222.             }
  223.             if(entered) {
  224.                 break;
  225.             }
  226.           }
  227.         if(!entered) {
  228.             System.out.println("No available places");
  229.         }
  230.         return cabins;
  231.     }
  232. }
  233.  
  234.  
  235. public class Cabin {
  236.  
  237.     private Passenger[] passengers = new Passenger[3];
  238.    
  239.     public Cabin (Passenger[] passengers) {
  240.        
  241.         this.passengers = passengers;
  242.     }
  243.    
  244.     public Cabin() {
  245. //      this.passengers = null;
  246.        
  247.     }
  248.  
  249.     public Passenger[] getPassengers() {
  250.         return passengers;
  251.     }
  252.  
  253.     public void setPassengers(Passenger[] passengers) {
  254.         this.passengers = passengers;
  255.     }
  256.    
  257.    
  258.    
  259.    
  260. }
  261.  
  262.  
  263.  
  264. public class Passenger {
  265.  
  266.    
  267.     private String firstName, surName;
  268.     private double expenses;
  269.    
  270.     public Passenger(String firstName, String surName, double expenses) {
  271.         this.firstName = firstName;
  272.         this.surName = surName;
  273.         this.expenses = expenses;
  274.     }
  275.    
  276.     public Passenger() {
  277.        
  278.     }
  279.    
  280.     public String getFirstName() {
  281.         return firstName;
  282.     }
  283.     public void setFirstName(String firstName) {
  284.         this.firstName = firstName;
  285.     }
  286.     public String getSurName() {
  287.         return surName;
  288.     }
  289.     public void setSurName(String surName) {
  290.         this.surName = surName;
  291.     }
  292.     public double getExpenses() {
  293.         return expenses;
  294.     }
  295.     public void setExpenses(double expenses) {
  296.         this.expenses = expenses;
  297.     }
  298.  
  299.     @Override
  300.     public String toString() {
  301.         return "Passenger " + firstName + " " + surName + ",  with expenses of: " + expenses;
  302.     }
  303.    
  304.    
  305.    
  306.    
  307. }
  308.  
Advertisement
Add Comment
Please, Sign In to add comment