In: Computer Science
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.
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);
});
}}