georgeB96

Untitled

Apr 14th, 2022
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. package cruise_boarding_sys;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class BoardingSys {
  6.  
  7.     //declaring scanner to take input
  8.     static Scanner kb = new Scanner(System.in);
  9.    
  10.     public static void main(String[] args) {
  11.        
  12.        
  13.        
  14.         //declaring cabin array
  15.         String[] cabins = new String[12];
  16.        
  17.         while(true) {
  18.             System.out.println("Enter option: A - to add cabin crew, V - to view all cabins");
  19.            
  20.             char option = kb.next().charAt(0); // reads String from keyboard and save it into option
  21.             System.out.println("You selected option: " + option);
  22.             kb.nextLine(); //fixes issues with nextLine()
  23.             //code for option A
  24.            
  25.             switch (option) {
  26.                 case 'A':
  27.                     cabins = addCustomer(cabins);
  28.                     break;
  29.                 case 'V':
  30.                     printCabins(cabins);
  31.                     break;
  32.                 default:
  33.                     System.out.println("Invalid option");      
  34.             }  
  35.         }
  36.     }
  37.     private static void printCabins(String[] cabins) {
  38.         System.out.println("Viewing all cabins: ");
  39.         for(int i=0;i<cabins.length;i++) {
  40.             System.out.println("Cabin number: " + (i+1) + " is ocuppied by: " + cabins[i]);
  41.         }
  42.        
  43.     }
  44.     //add customer method that takes the old cabins array and returns the new cabins array
  45.     private static String[] addCustomer(String[] cabins) {
  46.        
  47.         //check if customer is entered
  48.         boolean entered = false;
  49.         int cabinSpot = 0;
  50.         String cusName = "";
  51.         //iterate through cabin indexes in order to insert new cus
  52.         for(int i=0;i<cabins.length;i++) {
  53.             //check if cabin is empty
  54.             if(cabins[i] == null) {
  55.                 //customer entered cabin
  56.                 entered = true;
  57.                 //ask for cus name
  58.                 System.out.println("Please enter customer name");
  59.                 //read cus name
  60.                 cusName = kb.nextLine();
  61.                 //save cus name into the cabin on the specific index
  62.                 cabins[i] = cusName;
  63.                 cabinSpot = i+1;
  64.                 break;
  65.             }
  66.         }
  67.         if(!entered /* entered == false */) {
  68.             System.out.println("There are no empty cabins");
  69.         }else {
  70.             System.out.println("Customer entered cabin " + cabinSpot + " and it's name is: "  + cusName);
  71.         }
  72.        
  73.         return cabins;
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment