georgeB96

Untitled

Apr 14th, 2022
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.79 KB | None | 0 0
  1. package cruise_boarding_sys;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5. import java.io.BufferedWriter;
  6. import java.io.FileWriter;
  7. import java.io.BufferedReader;
  8. import java.io.FileReader;
  9.  
  10. public class BoardingSys {
  11.  
  12.     //declaring scanner to take input
  13.     static Scanner kb = new Scanner(System.in);
  14.    
  15.     public static void main(String[] args) throws Exception {
  16.        
  17.        
  18.        
  19.         //declaring cabin array
  20.         String[] cabins = new String[12];
  21.        
  22.         while(true) {
  23.             System.out.println("Enter option: "
  24.                     + "\nA - to add cabin crew, "
  25.                     + "\nV - to view all cabins,"
  26.                     + "\nE - print empty cabins,"
  27.                     + "\nD - delete customer from cabin,"
  28.                     + "\nF - find cabin using Customer name,"
  29.                     + "\nS - store program data into file,"
  30.                     + "\nL - load program data from the file,"
  31.                     + "\nO - view passengers in alphabetical order");
  32.            
  33.             char option = kb.next().charAt(0); // reads String from keyboard and save it into option
  34.             System.out.println("You selected option: " + option);
  35.             kb.nextLine(); //fixes issues with nextLine()
  36.             //code for option A
  37.            
  38.             switch (option) {
  39.                 case 'A':
  40.                     cabins = addCustomer(cabins);
  41.                     break;
  42.                 case 'V':
  43.                     printCabins(cabins);
  44.                     break;
  45.                 case 'E':
  46.                     printEmptyCabins(cabins);
  47.                     break;
  48.                 case 'D':
  49.                     cabins = deleteCustomerFromCabin(cabins);
  50.                     break;
  51.                 case 'F':
  52.                     findCabinWithCusName(cabins);
  53.                     break;
  54.                 case 'S':
  55.                     storeDataInFile(cabins);
  56.                     break;
  57.                 case 'L':
  58.                     cabins = loadProgramData();
  59.                     break;
  60.                 case 'O':
  61.                     viewPassengerInOrder(cabins);
  62.                     break;
  63.                 default:
  64.                     System.out.println("Invalid option");
  65.                     break;
  66.             }  
  67.         }
  68.     }
  69.     private static void viewPassengerInOrder(String[] cabins) {
  70.        
  71.         String[] cabinsSorted = new String[12];
  72.        
  73.         for(int i =0; i<cabins.length;i++) {
  74.             if(!(cabins[i]== null)) {
  75.                 for(int j=i+1; j<cabins.length; j++) {
  76.                     //compares each elements of the array to all the remaining elements
  77.                     if(!(cabins[j]==null)) {
  78.                         if(cabins[i].compareTo(cabins[j])>0) {
  79.                            
  80.                             //swap elements with each other
  81.                             String temp = cabins[i];
  82.                             cabins[i] = cabins[j];
  83.                             cabins[j] = temp;
  84.                         }
  85.                     }
  86.                    
  87.                 }
  88.             }
  89.            
  90.         }
  91.        
  92.         System.out.println("Viewing all cabins: ");
  93.         for(int i=0;i<cabins.length;i++) {
  94.             if(!(cabins[i] == null)) {
  95.                 System.out.println(cabins[i]);
  96.             }
  97.         }
  98.        
  99.        
  100.     }
  101.     private static String[] loadProgramData() throws Exception {
  102.         System.out.println("Loading data from CabinData.txt");
  103.         BufferedReader reader = new BufferedReader(new FileReader("CabinData.txt"));
  104.         String[] cabins = new String[12];
  105.         for (int i=0; i<cabins.length;i++) {
  106.             String cabinCus = reader.readLine();
  107.             //if cabinCus is string null, transform it to NULL type
  108.             if(cabinCus.equals("null")) {
  109.                 cabins[i] = null;
  110.             }else {
  111.                 cabins[i] = cabinCus;
  112.             }
  113.            
  114.         }
  115.         reader.close();
  116.         return cabins;
  117.     }
  118.     private static void storeDataInFile(String[] cabins) throws Exception {
  119.         System.out.println("Saving data in CabinData.txt");
  120.         BufferedWriter writer = new BufferedWriter(new FileWriter("CabinData.txt", false));
  121.         for(int i=0;i<cabins.length;i++) {
  122.             String message = cabins[i];
  123.              writer.write(message);
  124.              writer.newLine();
  125.         }
  126.         writer.flush();
  127.         System.out.println("Data successfully saved");
  128.        
  129.     }
  130.     private static void findCabinWithCusName(String[] cabins) {
  131.         System.out.println("Enter customer name:");
  132.         String cusName = kb.nextLine();
  133.         boolean found = false;
  134.         for(int i=0; i<cabins.length;i++) {
  135.             //check to see if its not null first so we can use .equals to check the customer name
  136.             //Cannot invoke "String.equals(Object)" because "cabins[i]" is null
  137.             if(!(cabins[i] == null)) {
  138.                 if(cabins[i].equals(cusName)) {
  139.                     System.out.println("Customer " + cusName + " is in cabin " + (i+1));
  140.                     found = true;
  141.                     break;
  142.                 }
  143.             }
  144.         }
  145.         if(!found) {
  146.             System.out.println("Customer " + cusName + " is not on the ship.");
  147.         }
  148.        
  149.        
  150.     }
  151.     private static String[] deleteCustomerFromCabin(String[] cabins) {
  152.        
  153.         //delete customer by cabin index/id
  154.         System.out.println("Please enter the index of the cabin you would like to empty: ");
  155.         int cabinIndex = kb.nextInt() - 1;
  156.         System.out.println("Customer "+ cabins[cabinIndex] +" has been removed from the cabin");
  157.         cabins[cabinIndex] = null;
  158.         return cabins;
  159.        
  160.     }
  161.     private static void printEmptyCabins(String[] cabins) {
  162.         System.out.println("Viewing all empty cabins: ");
  163.         for(int i=0;i<cabins.length;i++) {
  164.             if(cabins[i] == null) {
  165.                 System.out.println("Cabin number: " + (i+1) + " is empty.");
  166.             }
  167.         }
  168.     }
  169.     private static void printCabins(String[] cabins) {
  170.         System.out.println("Viewing all cabins: ");
  171.         for(int i=0;i<cabins.length;i++) {
  172.             System.out.println("Cabin number: " + (i+1) + " is ocuppied by: " + cabins[i]);
  173.         }
  174.        
  175.     }
  176.     //add customer method that takes the old cabins array and returns the new cabins array
  177.     private static String[] addCustomer(String[] cabins) {
  178.        
  179.         //check if customer is entered
  180.         boolean entered = false;
  181.         int cabinSpot = 0;
  182.         String cusName = "";
  183.         //iterate through cabin indexes in order to insert new cus
  184.         for(int i=0;i<cabins.length;i++) {
  185.             //check if cabin is empty
  186.             if(cabins[i] == null) {
  187.                 //customer entered cabin
  188.                 entered = true;
  189.                 //ask for cus name
  190.                 System.out.println("Please enter customer name");
  191.                 //read cus name
  192.                 cusName = kb.nextLine();
  193.                 //save cus name into the cabin on the specific index
  194.                 cabins[i] = cusName;
  195.                 cabinSpot = i+1;
  196.                 break;
  197.             }
  198.         }
  199.         if(!entered /* entered == false */) {
  200.             System.out.println("There are no empty cabins");
  201.         }else {
  202.             System.out.println("Customer entered cabin " + cabinSpot + " and it's name is: "  + cusName);
  203.         }
  204.        
  205.         return cabins;
  206.     }
  207. }
  208.  
Advertisement
Add Comment
Please, Sign In to add comment