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...
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 operations: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a 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...
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...
Write a Java program that implements a queue in a hospital. I want your program to...
Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S, to store...
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
I want to write java program to implement the indexing concept so as an example we...
I want to write java program to implement the indexing concept so as an example we have file named Students this file contains information like this 112233445 ahmed 222442211 saeed 112453345 john this program should search for the student name by its number so as an output example: enter the student number 112233445 name found : ahmed also i want to print the index number of where does the student name exists
Lab Assignment Write a Java program that implements a queue in a hospital. I want your...
Lab Assignment Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S,...
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?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT