Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -----------------------------------------------------//
- // Name:
- // Week 2 Discussion 1
- //
- // Author:
- // James J. Boyer
- //
- // Purpose: Books airline seats and outputs a seating
- // chart when full or when user quits
- //
- // Date Last Modified:
- // 2015-06-08 @ 10:29 PM
- //
- // Class:
- // CSC 262 June 2015
- // -----------------------------------------------------//
- import java.util.Scanner;
- import java.util.*;
- public class Plane
- {
- // checks customers in and assigns them a boarding pass
- // To the human user, Seats 1 to 2 are for First Class passengers and Seats 3 to 5 are for Economy Class passengers
- //
- public void reserveSeats()
- {
- int counter = 0;
- int section = 0;
- int choice = 0;
- String eatRest = ""; //to hold junk in input buffer
- String inName = "";
- String name = "";
- boolean goOn = true ;
- List<String> firstClassPassengers = new ArrayList<String>();
- List<String> economyClassPassengers = new ArrayList<String>();
- Scanner input = new Scanner( System.in );
- String[] passenger = new String[ 5 ]; // array of seats
- int firstClass = 0; // next available first class seat
- int economy = 2; // next available economy seat
- while ( (goOn == true) && ( ( firstClass < 2 ) || ( economy < 5 ) ))
- {
- System.out.println( "Please type 1 for First Class" );
- System.out.println( "Please type 2 for Economy" );
- System.out.println( "Please type 3 to quit");
- System.out.print( "choice: " );
- section = input.nextInt();
- eatRest = input.nextLine(); //get any end of line character remaining in the buffer to prepare for the next nextLine()
- System.out.println(" ");
- switch (section)
- {
- case 1: //user chose first class
- if ( firstClass < 2 ) //if first class available
- {
- System.out.print("Please enter your name: ");
- inName = input.nextLine();
- name = inName.trim();
- passenger[firstClass] = name;
- firstClass = firstClass + 1;
- System.out.printf( "First Class. Seat #%d for %s\n", firstClass, name);
- // Add to the economy class list
- firstClassPassengers.add("Seat "+firstClass+"\t\t"+name);
- } // end if
- else //first class is full
- {
- if ( economy < 5 ) // but if economy class is available
- {
- System.out.println("First Class is full, Economy Class?" );
- System.out.print( "1. Yes, 2. No. Your choice: " );
- choice = input.nextInt();
- eatRest = input.nextLine();
- if ( choice == 1 )
- {
- System.out.print("Please enter your name: ");
- inName = input.nextLine();
- name = inName.trim();
- passenger[economy] = name;
- economy = economy + 1;
- System.out.printf( "Economy Class. Seat #%d for %s\n", economy, name );
- // Add to the economy class list
- economyClassPassengers.add("Seat "+economy+"\t\t"+name);
- }
- else
- System.out.println( "Sorry. The next flight leaves in 3 hours." );
- } // end if (economy < 5)
- } // end else
- break;
- case 2: //user chose economy
- if ( economy < 5 ) //if economy class is available
- {
- System.out.print("Please enter your name: ");
- inName = input.nextLine();
- name = inName.trim();
- passenger[economy] = name;
- economy = economy + 1;
- System.out.printf( "Economy Class. Seat #%d for %s\n", economy, name );
- // Add to the economy class list
- economyClassPassengers.add("Seat "+economy+"\t\t"+name);
- } // end if (economy < 5)
- else //economy class is full
- {
- if ( firstClass < 2 ) // but if first class available
- {
- System.out.println("Economy Class is full, First Class?" );
- System.out.print( "1. Yes, 2. No. Your choice: " );
- choice = input.nextInt();
- eatRest = input.nextLine();
- if ( choice == 1 )
- {
- System.out.print("Please enter your name: ");
- inName = input.nextLine();
- name = inName.trim();
- passenger[firstClass] = name;
- firstClass = firstClass + 1;
- System.out.printf( "First Class. Seat #%d for %s\n", firstClass, name );
- //Add to the economy class list
- economyClassPassengers.add("Seat "+economy+"\t\t"+name);
- } // end if
- else
- System.out.println( "Next flight leaves in 3 hours." );
- } // if (firstClass < 2)
- } // end else
- break;
- case 3: //user chose to quit
- System.out.println("OK");
- goOn = false;
- break;
- default:
- System.out.println("1 or 2 only. Please try again.");
- break;
- }// end switch
- System.out.println();
- } // end while
- if ((firstClass > 1) && (economy > 4))
- System.out.println( "The plane is now full." );
- // Make sure the Passenger list has a value before printing it
- if(!firstClassPassengers.isEmpty())
- {
- for(int z =0; z <= firstClassPassengers.size()-1; z++)
- {
- System.out.print(firstClassPassengers.get(z) + "\n");
- }
- }
- // Make sure the Passenger list has a value before printing it
- if(!economyClassPassengers.isEmpty())
- {
- for(int z =0; z <= economyClassPassengers.size()-1; z++)
- {
- System.out.print(economyClassPassengers.get(z) + "\n");
- }
- }
- System.out.println();
- System.out.println("Bye");
- } // end method reserveSeats()
- } // end class Plane
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement