Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cruise_boarding_sys;
- import java.util.Scanner;
- public class BoardingSys {
- //declaring scanner to take input
- static Scanner kb = new Scanner(System.in);
- public static void main(String[] args) {
- //declaring cabin array
- String[] cabins = new String[12];
- while(true) {
- System.out.println("Enter option: A - to add cabin crew, V - to view all cabins");
- char option = kb.next().charAt(0); // reads String from keyboard and save it into option
- System.out.println("You selected option: " + option);
- kb.nextLine(); //fixes issues with nextLine()
- //code for option A
- switch (option) {
- case 'A':
- cabins = addCustomer(cabins);
- break;
- case 'V':
- printCabins(cabins);
- break;
- default:
- System.out.println("Invalid option");
- }
- }
- }
- private static void printCabins(String[] cabins) {
- System.out.println("Viewing all cabins: ");
- for(int i=0;i<cabins.length;i++) {
- System.out.println("Cabin number: " + (i+1) + " is ocuppied by: " + cabins[i]);
- }
- }
- //add customer method that takes the old cabins array and returns the new cabins array
- private static String[] addCustomer(String[] cabins) {
- //check if customer is entered
- boolean entered = false;
- int cabinSpot = 0;
- String cusName = "";
- //iterate through cabin indexes in order to insert new cus
- for(int i=0;i<cabins.length;i++) {
- //check if cabin is empty
- if(cabins[i] == null) {
- //customer entered cabin
- entered = true;
- //ask for cus name
- System.out.println("Please enter customer name");
- //read cus name
- cusName = kb.nextLine();
- //save cus name into the cabin on the specific index
- cabins[i] = cusName;
- cabinSpot = i+1;
- break;
- }
- }
- if(!entered /* entered == false */) {
- System.out.println("There are no empty cabins");
- }else {
- System.out.println("Customer entered cabin " + cabinSpot + " and it's name is: " + cusName);
- }
- return cabins;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment