Question

In: Computer Science

I want to write this program in java. Write a simple airline ticket reservation program in...

I want to write this program in java.

Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list of flights with each node including a pointer to a linked list of passengers.

Solutions

Expert Solution

package elastic5.elastic5;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import elastic5.elastic5.ttt.Console;

public class AirlineRes extends JFrame {
   static final int MAX_SEATS = 8;
   String[] seats = new String[MAX_SEATS];
public AirlineRes() {

initUI();
}

private void initUI() {
  
createMenuBar();

setTitle("airline ticket reservation");
setSize(300, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

private void createMenuBar() {

JMenuBar menubar = new JMenuBar();
ImageIcon icon = new ImageIcon("exit.png");

JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);

JMenuItem eMenuItem = new JMenuItem("Exit", icon);
eMenuItem.setMnemonic(KeyEvent.VK_E);
eMenuItem.setToolTipText("Exit application");
eMenuItem.addActionListener((ActionEvent event) -> {
System.exit(0);
});

file.add(eMenuItem);
  
//reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers
JMenuItem eMenuItem1 = new JMenuItem("Reserve Ticket", icon);
eMenuItem1.setMnemonic(KeyEvent.VK_E);
eMenuItem1.setToolTipText("Book Ticket");
eMenuItem1.addActionListener((ActionEvent event) -> {
   String[] seats = new String[MAX_SEATS]; // the passenger list
initializeSeats(seats);
   makeReservation(seats);
System.out.println("book ticket");
});

file.add(eMenuItem1);


JMenuItem eMenuItem2 = new JMenuItem("cancel a reservation", icon);
eMenuItem2.setMnemonic(KeyEvent.VK_E);
eMenuItem2.setToolTipText("cancel a reservation");
eMenuItem2.addActionListener((ActionEvent event) -> {
  
   try {
       String[] seats = new String[MAX_SEATS]; // the passenger list
initializeSeats(seats);
               cancelReservation(seats);
              
           } catch (Exception e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
System.out.println("cancel a reservation");
});

file.add(eMenuItem2);
  
JMenuItem eMenuItem3 = new JMenuItem("check whether a ticket is reserved for a particular person", icon);
eMenuItem3.setMnemonic(KeyEvent.VK_E);
eMenuItem3.setToolTipText("check whether a ticket is reserved for a particular person");
eMenuItem3.addActionListener((ActionEvent event) -> {
System.out.println("check whether a ticket is reserved for a particular person");
});

file.add(eMenuItem3);
  
JMenuItem eMenuItem4 = new JMenuItem("display the passengers", icon);
eMenuItem4.setMnemonic(KeyEvent.VK_E);
eMenuItem4.setToolTipText("display the passengers");
eMenuItem4.addActionListener((ActionEvent event) -> {
System.out.println("display the passengers");
});

file.add(eMenuItem4);
  
menubar.add(file);

setJMenuBar(menubar);
}

  
  
static void initializeSeats(String[] seats) {
for (int i = 0; i < seats.length; i++) {
seats[i] = "";
}
}
  
/**
* Make a reservation
*/
void makeReservation(String[] seats) {
int seatIndex = findEmptySeat(seats); // index of first empty seat
if (seatIndex == seats.length) {
System.out.println("All seats are full. Sorry.");
} else {
String name = getPassengerName(); // passenger's name
seats[seatIndex] = name;
System.out.println(name + " has been assigned seat #" + (seatIndex+1));
}
}
/**
* Find the index of the first empty seat on the plane.
* If there are no empty seats, return seats.length
*/
int findEmptySeat(String[] seats) {
for (int i = 0; i < seats.length; i++) {
if (isEmpty(seats, i)) {
return i;
}
}

return seats.length;
}
boolean isEmpty(String[] seats, int seatIndex) {
return seats[seatIndex].equals("");
}
  
/**
* Cancel a reservation
* @throws IOException
* @throws NumberFormatException
*/
void cancelReservation(String[] seats) throws NumberFormatException, IOException {
int seatIndex = getSeatToCancel(); // index of seat to cancel reservation for
if (isEmpty(seats, seatIndex)) {
System.out.println("Seat #" + (seatIndex+1) + " has not been reserved for anyone");
} else {
seats[seatIndex] = "";
System.out.println("Seat #" + (seatIndex+1) + " is now available");
}
}
  
  
int readInt() throws NumberFormatException, IOException {
int i = 0;

boolean valid = false;
i=2;
// i = Integer.parseInt(in.readLine());
valid = true;

System.out.print("ttt");

return i;
}

int getSeatToCancel() throws NumberFormatException, IOException {
boolean valid = false; // is the seat number valid?
int seat; // seat number to cancel

do {
System.out.print("Enter the seat to cancel: ");
seat = readInt();
if (1 <= seat && seat <= MAX_SEATS) {
valid = true;
} else {
System.out.println("Invalid seat number");
}
} while (!valid);

return seat-1;
}
  
String getPassengerName() {
System.out.print("Enter the passenger's name: ");
return "Parmesh dayal";
}
  
public static void main(String[] args) {
   boolean done = false;
     
String[] seats = new String[MAX_SEATS]; // the passenger list
initializeSeats(seats);
EventQueue.invokeLater(() -> {
AirlineRes ex = new AirlineRes();
ex.setVisible(true);
});
}}


Related Solutions

Write a simple airline ticket reservation program. The program should display a menu with the following...
Write a simple airline ticket reservation program. The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit on the number of flights. Create a linked list...
Java I'm trying to create a program that replicates a theater ticket reservation system. I have...
Java I'm trying to create a program that replicates a theater ticket reservation system. I have a Seat class with members row(integer), seat(character) and ticketType(character). Where a ticket type can be 'A' adult, 'C' child, 'S' senior, or '.' recorded as empty. I have a Node generic class that points to other nodes(members): up, Down. Left, Right. It also has a generic payload. Lastly, I have an Auditorium class which is also generic. its member is a First Node<T>. As...
Bus ticket reservation project in java using class ,inheritance,interface
Bus ticket reservation project in java using class ,inheritance,interface
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Java Linked Lists I want a simple program that reads two text files that contains an...
Java Linked Lists I want a simple program that reads two text files that contains an integers matrix and store each file into a linked lists matrix so I can later preform operations such as addition and subtraction on the matrices an example of the input text files: sample a 2 6 2 6 2 18 17 11 20 sample b 3 13 5 4 11 20 13 18 20
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
in a gui ' in java write a program that draws equal a simple fence with...
in a gui ' in java write a program that draws equal a simple fence with vertical, spaced slats backed by two boards. Behind the fence show a simple house support Make sure the in the und. house is visible between the slats in the fence.
Suppose that you manage an airline reservation system and want to improve service quality. What are...
Suppose that you manage an airline reservation system and want to improve service quality. What are the important CTQs for this process? What are the KPIVs and KPOVs? How do these relate to the customer CTQs that you have identified?
What we want the program to do: We need to write a program, in Java, that...
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then...
Description: The purpose of the program is to create a Java ticket purchasing program that allows...
Description: The purpose of the program is to create a Java ticket purchasing program that allows for users to log in, purchase tickets, keep records of tickets purchased, and keep information about the user. Program Requirements: The following are the program requirements: Must be fully functioning including registration, log-in, view events, and purchase tickets Tickets should be purchased using “points”, no information should be provided via the user for payment method. Default each user to an allotted number of points...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT