In: Computer Science
Im in a java class and having trouble with assigning the seat to a specific number from the array.. Below are the instructions I was given and then the code that I have already.
ITSE 2321 – OBJECT-ORIENTED PROGRAMMING JAVA Program 8 – Arrays and ArrayList
A small airline has just purchased a computer for its new automated reservations system. You have been asked to develop the new system. You are to write an application to assign seats on flight of the airline’s only plane (capacity: 10 seats). Your application should display the following alternatives: "Please type 1 for First Class" and "Please type 2 for Economy." If the user types 1, your application should assign a seat in the first-class section (seats 1 – 5). If the user types 2, your application should assign a seat in the economy section (seats 6 – 10). Your application should then display a boarding pass indicating the person’s seat number and whether it’s in the first-class or economy section of the plane. Use a one-dimensional array of primitive type boolean to represent the seating chart of the plane. Initialize all the elements of the array to false to indicate that all the seats are empty. As each is assigned, set the corresponding element of the array to true to indicate that the seat is on longer available. Your application should never assign a seat that has already been assigned. When the economy section is full, your application should ask the person if it’s acceptable to be placed in the first-class section (and vice versa). If yes, make the appropriate seat assignment. If no, display the message "Next flight leaves in 3 hours." End the program when all the ten seats on the plane have been assigned.
No input, processing or output should happen in the main method. All work should be delegated to other non-static methods. Include the recommended minimum documentation for each method. See the program one template for more details.
Run your program, with your own data, and copy and paste the output to a file. Create a folder named, fullname_program8. Copy your source code and the output file to the folder. Zip the folder and upload it to Blackboard.
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package airlineapp;
import java.util.Scanner;
/**
*
* @author user1
*/
public class AirlineApp {
int num;
int i;
int counter = 0;
int [] seatNum = new int[11];
public static void main(String[] args) {
AirlineApp application = new AirlineApp();
application.userInput();
application.seatChart();
//create the array
// TODO code application logic here
}
public void userInput(){
Scanner input = new Scanner(System.in);
System.out.print("Please type 1 for first class and press 2 for
economy: ");
//gets the input from the user and saves it as num to perform the
rest
//of the aplication
num = input.nextInt();
}
public void seatChart(){
for(int counter = 0; counter < seatNum.length; counter++){
if(num != 2 && counter < 6 )
counter = counter + 1;
}
}
}
// Java program to simulate an automated reservations system for
Airline
import java.util.Scanner;
public class AirlineApp {
private boolean [] seatChart ; // array of seatChart
which determines if seat is available or not
// constructor that allocates 10 seats for the
airline
//initially all are available
public AirlineApp()
{
seatChart = new boolean[10];
for(int
i=0;i<seatChart.length;i++)
seatChart[i] =
false;
}
// method to simulate user input and processing
public void userInput(){
Scanner input = new
Scanner(System.in);
String choice;
int index;
// loop that continues till all the
seats are reserved
while(economyEmptySeat() != -1 ||
firstClassEmptySeat() != -1)
{
// input the
type of seat
System.out.print("\nPlease type 1 for first class and press 2 for
economy: ");
//gets the input
from the user and saves it as num to perform the rest
//of the
application
int num =
input.nextInt();
if(num == 2) //
if economy class
{
index = economyEmptySeat(); // get the index of
the next seat free , -1 is economy is full
if(index == -1) // check if economy is
full
{
// inform the user and ask if
they would like to be placed in first class
System.out.print("Economy
class is full. Should I place you in First class (yes/no) ?
");
choice = input.next();
// if user agrees, allocate a
seat in first class and display the boarding pass
if(choice.equalsIgnoreCase("yes"))
{
index =
firstClassEmptySeat();
seatChart[index] = true;
displayBoardPass(index);
}else // if user doesn't
agree, display the message
System.out.println("Next flight leaves in 3 hours");
}else // if seat is available in economy,
allocate a seat and display the boarding pass
{
seatChart[index] =
true;
displayBoardPass(index);
}
}else if(num ==
1) // first class
{
index = firstClassEmptySeat(); // get the index
of the next seat free , -1 is economy is full
if(index == -1) // check if first class is
full
{
// inform the user and ask if
they would like to be placed in economy class
System.out.print("First class
is full. Should I place you in Economy class (yes/no)");
choice = input.next();
// if user agrees, allocate a
seat in economy class and display the boarding pass
if(choice.equalsIgnoreCase("yes"))
{
index =
economyEmptySeat();
seatChart[index] = true;
displayBoardPass(index);
}else // if user doesn't
agree, display the message
System.out.println("Next flight leaves in 3 hours");
}else // if seat is available in first class,
allocate a seat and display the boarding pass
{
seatChart[index] =
true;
displayBoardPass(index);
}
}else // invalid
choice
System.out.println("Invalid choice");
}
}
// method that returns the next available seat in
economy class, -1 if full
public int economyEmptySeat()
{
for(int i=0;i<5;i++)
if(!seatChart[i])
return i;
return -1;
}
// method that returns the next available seat in
first class, -1 if full
public int firstClassEmptySeat()
{
for(int
i=5;i<seatChart.length;i++)
if(!seatChart[i])
return i;
return -1;
}
// method to display the boarding pass
public void displayBoardPass(int index)
{
System.out.println("BOARDING PASS:
");
System.out.println("Seat Number :
"+(index+1));
if(index >=0 && index
< 5)
System.out.println("Economy");
else
System.out.println("First class");
}
public static void main(String[] args) {
// create an object of
AirlineApp
AirlineApp application = new
AirlineApp();
application.userInput(); //simulate
the operation
}
}
//end of program
Output: