SlideShare a Scribd company logo
1 of 6
Download to read offline
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.
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);
});
}}

More Related Content

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

Quest 1 define a class batsman with the following specifications
Quest  1 define a class batsman with the following specificationsQuest  1 define a class batsman with the following specifications
Quest 1 define a class batsman with the following specifications
rajkumari873
 
import java.util.ArrayList; import java.util.List; import java.u.pdf
import java.util.ArrayList; import java.util.List; import java.u.pdfimport java.util.ArrayList; import java.util.List; import java.u.pdf
import java.util.ArrayList; import java.util.List; import java.u.pdf
anupamagarud8
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
udit652068
 
In Java Write a GUI application to simulate writing out a check. The.pdf
In Java Write a GUI application to simulate writing out a check. The.pdfIn Java Write a GUI application to simulate writing out a check. The.pdf
In Java Write a GUI application to simulate writing out a check. The.pdf
flashfashioncasualwe
 
Java!!!!!Create a program that authenticates username and password.pdf
Java!!!!!Create a program that authenticates username and password.pdfJava!!!!!Create a program that authenticates username and password.pdf
Java!!!!!Create a program that authenticates username and password.pdf
arvindarora20042013
 
Ejemplos Interfaces Usuario 3
Ejemplos Interfaces Usuario 3Ejemplos Interfaces Usuario 3
Ejemplos Interfaces Usuario 3
martha leon
 

Similar to I want to write this program in java.Write a simple airline ticket.pdf (20)

Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 
Eclipse Tricks
Eclipse TricksEclipse Tricks
Eclipse Tricks
 
20150319 testotipsio
20150319 testotipsio20150319 testotipsio
20150319 testotipsio
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 
Quest 1 define a class batsman with the following specifications
Quest  1 define a class batsman with the following specificationsQuest  1 define a class batsman with the following specifications
Quest 1 define a class batsman with the following specifications
 
Java Airline Reservation System – Travel Smarter, Not Harder.pdf
Java Airline Reservation System – Travel Smarter, Not Harder.pdfJava Airline Reservation System – Travel Smarter, Not Harder.pdf
Java Airline Reservation System – Travel Smarter, Not Harder.pdf
 
import java.util.ArrayList; import java.util.List; import java.u.pdf
import java.util.ArrayList; import java.util.List; import java.u.pdfimport java.util.ArrayList; import java.util.List; import java.u.pdf
import java.util.ArrayList; import java.util.List; import java.u.pdf
 
Spin Up Desktop Apps with Electron.js
Spin Up Desktop Apps with Electron.jsSpin Up Desktop Apps with Electron.js
Spin Up Desktop Apps with Electron.js
 
Java swing
Java swingJava swing
Java swing
 
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdfWorking with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
Working with Layout Managers. Notes 1. In part 2, note that the Gam.pdf
 
Write a GUI application to simulate writing out a check. The value o.pdf
Write a GUI application to simulate writing out a check. The value o.pdfWrite a GUI application to simulate writing out a check. The value o.pdf
Write a GUI application to simulate writing out a check. The value o.pdf
 
In Java Write a GUI application to simulate writing out a check. The.pdf
In Java Write a GUI application to simulate writing out a check. The.pdfIn Java Write a GUI application to simulate writing out a check. The.pdf
In Java Write a GUI application to simulate writing out a check. The.pdf
 
Airline reservation system
Airline reservation systemAirline reservation system
Airline reservation system
 
How to develop automated tests
How to develop automated testsHow to develop automated tests
How to develop automated tests
 
Java!!!!!Create a program that authenticates username and password.pdf
Java!!!!!Create a program that authenticates username and password.pdfJava!!!!!Create a program that authenticates username and password.pdf
Java!!!!!Create a program that authenticates username and password.pdf
 
Ejemplos Interfaces Usuario 3
Ejemplos Interfaces Usuario 3Ejemplos Interfaces Usuario 3
Ejemplos Interfaces Usuario 3
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt07-Basic-Input-Output.ppt
07-Basic-Input-Output.ppt
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
The next step, part 2
The next step, part 2The next step, part 2
The next step, part 2
 

More from feelinggift

A) Which of the following element is seen in all organic molecules Si.pdf
A) Which of the following element is seen in all organic molecules Si.pdfA) Which of the following element is seen in all organic molecules Si.pdf
A) Which of the following element is seen in all organic molecules Si.pdf
feelinggift
 
What motives do corporate executives have that force them to embrace.pdf
What motives do corporate executives have that force them to embrace.pdfWhat motives do corporate executives have that force them to embrace.pdf
What motives do corporate executives have that force them to embrace.pdf
feelinggift
 
Using the space provided compose an ESSAY concerning the following qu.pdf
Using the space provided compose an ESSAY concerning the following qu.pdfUsing the space provided compose an ESSAY concerning the following qu.pdf
Using the space provided compose an ESSAY concerning the following qu.pdf
feelinggift
 
We continually hear about interest groups in the news. Understanding.pdf
We continually hear about interest groups in the news. Understanding.pdfWe continually hear about interest groups in the news. Understanding.pdf
We continually hear about interest groups in the news. Understanding.pdf
feelinggift
 
Physical security is a fundamental component of any secure infrastru.pdf
Physical security is a fundamental component of any secure infrastru.pdfPhysical security is a fundamental component of any secure infrastru.pdf
Physical security is a fundamental component of any secure infrastru.pdf
feelinggift
 
This is a homework assignment that I have for my Java coding class. .pdf
This is a homework assignment that I have for my Java coding class. .pdfThis is a homework assignment that I have for my Java coding class. .pdf
This is a homework assignment that I have for my Java coding class. .pdf
feelinggift
 
The Jannuschs operated Festival Foods, a busi- ness that served conc.pdf
The Jannuschs operated Festival Foods, a busi- ness that served conc.pdfThe Jannuschs operated Festival Foods, a busi- ness that served conc.pdf
The Jannuschs operated Festival Foods, a busi- ness that served conc.pdf
feelinggift
 
show all of your work to arrive a final result Simple Interest Simpl.pdf
show all of your work to arrive a final result Simple Interest Simpl.pdfshow all of your work to arrive a final result Simple Interest Simpl.pdf
show all of your work to arrive a final result Simple Interest Simpl.pdf
feelinggift
 
Terms from which students can chooseMacrophages; •Only one.pdf
Terms from which students can chooseMacrophages; •Only one.pdfTerms from which students can chooseMacrophages; •Only one.pdf
Terms from which students can chooseMacrophages; •Only one.pdf
feelinggift
 
Research how voting is conducted for the following event. Descri.pdf
Research how voting is conducted for the following event. Descri.pdfResearch how voting is conducted for the following event. Descri.pdf
Research how voting is conducted for the following event. Descri.pdf
feelinggift
 

More from feelinggift (20)

A) Which of the following element is seen in all organic molecules Si.pdf
A) Which of the following element is seen in all organic molecules Si.pdfA) Which of the following element is seen in all organic molecules Si.pdf
A) Which of the following element is seen in all organic molecules Si.pdf
 
Given an ArrayList, write a Java method that returns a new ArrayList.pdf
Given an ArrayList, write a Java method that returns a new ArrayList.pdfGiven an ArrayList, write a Java method that returns a new ArrayList.pdf
Given an ArrayList, write a Java method that returns a new ArrayList.pdf
 
Write an MSP430g2553 C program to drive a continually scrolling mess.pdf
Write an MSP430g2553 C program to drive a continually scrolling mess.pdfWrite an MSP430g2553 C program to drive a continually scrolling mess.pdf
Write an MSP430g2553 C program to drive a continually scrolling mess.pdf
 
Which of the following is NOT a financial measurement needed to see .pdf
Which of the following is NOT a financial measurement needed to see .pdfWhich of the following is NOT a financial measurement needed to see .pdf
Which of the following is NOT a financial measurement needed to see .pdf
 
Which process uses chemiosmosis A. Pyruvate oxidation B. Electron .pdf
Which process uses chemiosmosis  A. Pyruvate oxidation  B. Electron .pdfWhich process uses chemiosmosis  A. Pyruvate oxidation  B. Electron .pdf
Which process uses chemiosmosis A. Pyruvate oxidation B. Electron .pdf
 
What motives do corporate executives have that force them to embrace.pdf
What motives do corporate executives have that force them to embrace.pdfWhat motives do corporate executives have that force them to embrace.pdf
What motives do corporate executives have that force them to embrace.pdf
 
when are business cases or project charters overkillSolutionP.pdf
when are business cases or project charters overkillSolutionP.pdfwhen are business cases or project charters overkillSolutionP.pdf
when are business cases or project charters overkillSolutionP.pdf
 
True or False The Congressional Budget Office projects that approxi.pdf
True or False The Congressional Budget Office projects that approxi.pdfTrue or False The Congressional Budget Office projects that approxi.pdf
True or False The Congressional Budget Office projects that approxi.pdf
 
Using the space provided compose an ESSAY concerning the following qu.pdf
Using the space provided compose an ESSAY concerning the following qu.pdfUsing the space provided compose an ESSAY concerning the following qu.pdf
Using the space provided compose an ESSAY concerning the following qu.pdf
 
We continually hear about interest groups in the news. Understanding.pdf
We continually hear about interest groups in the news. Understanding.pdfWe continually hear about interest groups in the news. Understanding.pdf
We continually hear about interest groups in the news. Understanding.pdf
 
View transaction list Journal entry worksheet 6 9 The company receive.pdf
View transaction list Journal entry worksheet 6 9 The company receive.pdfView transaction list Journal entry worksheet 6 9 The company receive.pdf
View transaction list Journal entry worksheet 6 9 The company receive.pdf
 
Physical security is a fundamental component of any secure infrastru.pdf
Physical security is a fundamental component of any secure infrastru.pdfPhysical security is a fundamental component of any secure infrastru.pdf
Physical security is a fundamental component of any secure infrastru.pdf
 
TrueFalse Verilog is case-insensitive TF Verilog has constructs.pdf
TrueFalse Verilog is case-insensitive TF Verilog has constructs.pdfTrueFalse Verilog is case-insensitive TF Verilog has constructs.pdf
TrueFalse Verilog is case-insensitive TF Verilog has constructs.pdf
 
This is a homework assignment that I have for my Java coding class. .pdf
This is a homework assignment that I have for my Java coding class. .pdfThis is a homework assignment that I have for my Java coding class. .pdf
This is a homework assignment that I have for my Java coding class. .pdf
 
The Jannuschs operated Festival Foods, a busi- ness that served conc.pdf
The Jannuschs operated Festival Foods, a busi- ness that served conc.pdfThe Jannuschs operated Festival Foods, a busi- ness that served conc.pdf
The Jannuschs operated Festival Foods, a busi- ness that served conc.pdf
 
The first thermodynamic law for a system of charged molecules in elec.pdf
The first thermodynamic law for a system of charged molecules in elec.pdfThe first thermodynamic law for a system of charged molecules in elec.pdf
The first thermodynamic law for a system of charged molecules in elec.pdf
 
show all of your work to arrive a final result Simple Interest Simpl.pdf
show all of your work to arrive a final result Simple Interest Simpl.pdfshow all of your work to arrive a final result Simple Interest Simpl.pdf
show all of your work to arrive a final result Simple Interest Simpl.pdf
 
Terms from which students can chooseMacrophages; •Only one.pdf
Terms from which students can chooseMacrophages; •Only one.pdfTerms from which students can chooseMacrophages; •Only one.pdf
Terms from which students can chooseMacrophages; •Only one.pdf
 
Simulate the Stakeholder deliverable for the development of an onlin.pdf
Simulate the Stakeholder deliverable for the development of an onlin.pdfSimulate the Stakeholder deliverable for the development of an onlin.pdf
Simulate the Stakeholder deliverable for the development of an onlin.pdf
 
Research how voting is conducted for the following event. Descri.pdf
Research how voting is conducted for the following event. Descri.pdfResearch how voting is conducted for the following event. Descri.pdf
Research how voting is conducted for the following event. Descri.pdf
 

Recently uploaded

Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

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

  • 1. 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. 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); }
  • 2. 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);
  • 3. } 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] = ""; } }
  • 4. /** * 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 {
  • 5. 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);
  • 6. 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); }); }}