Airline Reservation System In Java | Java Project

by | Nov 15, 2022 | Coding, Java

Home » Coding » Airline Reservation System In Java | Java Project

Introduction of the Project

In this article, you will find the source code and explanation of the Airline Reservation System in Java. For this java project, we have also used the Swing module to implement some GUI along with the SQL database to store information on flights available. This project has a method to book tickets on a selected flight & get a boarding pass too.

 

Objectives

The two major objectives of this java project are: 

  1. To create a user interface for the user to look for available flights and then book his tickets accordingly.
  2. To implement the tables that contain flight information and attach the functionality of booking tickets on the selected flight & get a boarding pass. 

Requirements

  • Swing (for creating the Graphical User Interface (GUI) of the project)

Source Code

package com.company;
public class Main {
public static void main(String[] args) {
new Airplane();
}
}
package com.company;
import com.toedter.calendar.JDateChooser;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;
import java.text.DateFormat;
import java.util.Vector;
public class Airplane {
private JTextField nameData;
private JTextField sourceData;
private JTable table1;
private JButton SEARCHButton;
private JButton BOOKButton;
private JPanel airPanel;
private JTextField fee;
private JTextField exp;
private JTextField destination;
private JPanel doj;
private JSpinner spinner1;
private JTextArea boarding;
private JLabel fare;
private JButton RESETButton;
JFrame planeF = new JFrame();
JDateChooser dateChooser = new JDateChooser();
public Airplane(){
planeF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
planeF.setContentPane(airPanel);
planeF.pack();
planeF.setLocationRelativeTo(null);
doj.add(dateChooser);
planeF.setVisible(true);
fare.setText("0");
SEARCHButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(destination.getText().equals("")|| sourceData.getText().equals("")){
JOptionPane.showMessageDialog(null,"Please Fill SOURCE & DESTINATION TO SEARCH.");
}else{
try {
String sql = "select * from airplane WHERE SOURCE= '"+sourceData.getText()+"' AND DESTINATION= '"+destination.getText()+"'";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(sql);
if(!rs.next())
JOptionPane.showMessageDialog(null,"NO FLIGHTS AVAILABLE");
else{
ResultSet rs1 = statement.executeQuery(sql);
table1.setModel(buildTableModel(rs1));
}
}catch (Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
}
}
});
BOOKButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,"FIND YOUR BOARDING PASS ATTACHED");
printPass();
}
});
table1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
DefaultTableModel dm = (DefaultTableModel)table1.getModel();
int selectedRow = table1.getSelectedRow();
int a =(int)dm.getValueAt(selectedRow,4);
String total = String.valueOf(a*(int)spinner1.getValue());
fare.setText(total);
}
});
RESETButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
planeF.dispose();
new Airplane();
}
});
}
public void printPass(){
DefaultTableModel dms = (DefaultTableModel)table1.getModel();
int selectedRow1 = table1.getSelectedRow();
String date = DateFormat.getDateInstance().format(dateChooser.getDate());
boarding.setText(boarding.getText() + " BOARDING PASS"+"\n");
boarding.setText(boarding.getText() + " ------***------"+"\n");
boarding.setText(boarding.getText() + "\n");
boarding.setText(boarding.getText() +"NAME: "+nameData.getText()+"\n");
boarding.setText(boarding.getText() +"FLIGHT NO: "+dms.getValueAt(selectedRow1,0).toString()+"\n");
boarding.setText(boarding.getText() +"SOURCE: "+sourceData.getText()+"\n");
boarding.setText(boarding.getText() +"DESTINATION: "+destination.getText()+"\n");
boarding.setText(boarding.getText() +"DATE OF JOURNEY: "+date+"\n");
boarding.setText(boarding.getText() +"TIME: "+ dms.getValueAt(selectedRow1,3).toString()+"\n");
boarding.setText(boarding.getText() +"TOTAL AMOUNT: "+"₹"+fare.getText());
}
public void tableData() {
try{
String a= "Select* from doctor";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(a);
table1.setModel(buildTableModel(rs));
}catch (Exception ex1){
JOptionPane.showMessageDialog(null,ex1.getMessage());
}
}
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
}

Explanation of the Code

The code consists of two parts, the first one involves creating the GUI for flight availability and booking, and the other is the retrieval of information from the SQL database so a user can book tickets on the selected flight and get a boarding pass.

Let us look at the GUI first:

1. The main screen consists of 3 buttons for searching for flights, booking tickets & resetting the screen, respectively.

2. It has five text fields & a JLabel that requires information on customer booking tickets.

3. It consists of a Jtable that displays the stored information in the database.

Moving to the retrieval, we have applied:

1. Build a connection first with the database using the Connection object.

2. Inject the query that stores table data in ResultSet.

3. Finally, send data to Jtable.

4. The Search button is for retrieving flights available from source to destination & displays in jtable & if not, then pops an error message.

5. The Book Tickets button is to book tickets for a selected flight from the table & then print details of the customer, flight & total amount in JtextArea beneath the table.

Output

Main Interface

Airline Reservation System In Java | Java Project

Conclusion

We have implemented a Java project that helps to manage customer information & help to book a flight with Swing (used for GUI). This Airline Reservation System in Java program is a very efficient & easy way to reserve tickets & have all the details of the customer & flight in the boarding pass attached to it.

More Java Projects>>>>

You May Also Like To Create…

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *