Introduction of the Project
Are you tired of waiting in long lines or struggling to find a taxi on the go? A Cab Booking System is the solution to your transportation woes. By building a Cab Booking System in Java, you can streamline the process of booking a cab and simplify the entire transportation experience.
Java is a powerful programming language that offers an extensive set of tools for building robust and scalable applications. With its wide range of libraries and frameworks, Java can help you create a Cab Booking System that is efficient, secure, and easy to maintain.
In this Java Project tutorial, we will guide you through the process of building a Cab Booking System in Java. We’ll cover everything from designing the user interface to implementing the booking logic and integrating payment systems. By the end of this tutorial, you’ll have a fully functional Cab Booking System that can be customized to suit your specific needs. So buckle up, and let’s get started!
Objectives
1. To use SQL database and provide a way to save information about available taxis, book a taxi for the selected taxi, and get the driver and vehicle number details.
2. To create a graphic user interface with the Swing module that makes it easier for the user to interact with the cab booking system
Requirements
Before diving into building a Cab Booking System in Java, there are a few prerequisites that you should have knowledge of. Here are some key requirements to get started:
1. Java programming knowledge: To build a Cab Booking System in Java, you need to have a good understanding of the Java programming language. This includes knowledge of object-oriented programming (OOPS) concepts, data structures, and algorithms.
2. Integrated Development Environment (IDE): You need an IDE to develop Java applications. IDEs like Eclipse, NetBeans, and IntelliJ IDEA are some of the most popular choices. You should be familiar with using an IDE to create and manage Java projects.
3. Database management: A Cab Booking System needs to store and retrieve data about users, bookings, and other related information. You should have knowledge of database management systems (DBMS) like MySQL or PostgreSQL and be familiar with SQL queries for creating, reading, updating, and deleting records.
4. Swing Module: To make this Java project, you’ll need to use Swing to create the program’s graphical user interface (GUI). Swing is a part of Java that allows developers to make attractive interfaces for Windows-based programs. The great thing about using Swing is that it works on different types of computers. Swing has many tools that let you make tables, scrollbars, and buttons for your program.
5. Server management: Finally, you should have knowledge of server management and deployment. You need to know how to deploy your application to a web server and manage it effectively.
Source Code
package com.company; public class Main { public static void main(String[] args) { new Cab(); } } package com.company; import com.toedter.calendar.JDateChooser; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.sql.*; import java.text.DateFormat; import java.util.Vector; public class Cab { 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 JTextArea boardDetails; private JLabel fare; private JButton RESETButton; JFrame planeF = new JFrame(); JDateChooser bookingDate = new JDateChooser(); public Cab(){ planeF.setContentPane(airPanel); planeF.pack(); planeF.setLocationRelativeTo(null); planeF.setVisible(true); doj.add(bookingDate); 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 cab 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 queryResult = statement.executeQuery(sql); if(!queryResult.next()) JOptionPane.showMessageDialog(null,"NO CAB AVAILABLE"); else{ ResultSet queryResult1 = statement.executeQuery(sql); table1.setModel(buildTableModel(queryResult1)); } }catch (Exception ex){ JOptionPane.showMessageDialog(null,ex.getMessage()); } } } }); BOOKButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null,"FIND YOUR CAB DETAILS 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); fare.setText(String.valueOf(a)); } }); RESETButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { planeF.dispose(); new Cab(); } }); } public void printPass(){ DefaultTableModel dms = (DefaultTableModel)table1.getModel(); int selectedRow1 = table1.getSelectedRow(); String date = DateFormat.getDateInstance().format(bookingDate.getDate()); boardDetails.setText(boardDetails.getText() + " CAB DETAILS"+"\n"); boardDetails.setText(boardDetails.getText() + " ------***------"+"\n"); boardDetails.setText(boardDetails.getText() + "\n"); boardDetails.setText(boardDetails.getText() +"PASSENGER NAME: "+nameData.getText()+"\n"); boardDetails.setText(boardDetails.getText() +"DRIVER NAME: "+dms.getValueAt(selectedRow1,0)+"\n"); boardDetails.setText(boardDetails.getText() +"VEHICLE NO: "+dms.getValueAt(selectedRow1,1).toString()+"\n"); boardDetails.setText(boardDetails.getText() +"SOURCE: "+sourceData.getText()+"\n"); boardDetails.setText(boardDetails.getText() +"DESTINATION: "+destination.getText()+"\n"); boardDetails.setText(boardDetails.getText() +"DATE: "+ date+"\n"); boardDetails.setText(boardDetails.getText() +"TOTAL AMOUNT: "+"₹"+fare.getText()+"\n"); boardDetails.setText(boardDetails.getText() +"ARRIVING IN: "+"15 MINUTES"); } 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
Now let us understand the working of the code. Let’s look at the GUI first.
1. Our main screen consists of three buttons: search cabs, book cab, and reset screen.
2. We have four text fields and a JLabel that needs information about the customer who booked the cab or taxi.
3. Once the user has input the passenger information then, we display the information stored in the database through a JTable.
Now moving to the retrieval of information from the SQL database to allow users to book tickets for selected flights and obtain a boarding pass, we have applied:
1. At first, we established a connection to the database using a connection object.
2. Then, we paste a query containing table data into a ResultSet.
3. Next, we send the data to Jtable.
4. After that, we use the Search button to retrieve available cabs from origin to destination and display them in JTable. If not, we throw an error message.
5. Finally, the Book Taxi button is for booking a cab for the cab selected from the table and prints the customer, driver, vehicle, and total price details in the JtextArea below the table.
Output
Main Interface
Recommendation
We have successfully built a cab booking system in Java that helps you book a cab and get cab details. This java project utilizes Swing for the GUI and is a very efficient and easy way to book a taxi, with all driver and taxi details attached to the JtextArea. We have a few recommendations for you that can make your application even more useful and ready for the real world.
- You can make a web-based interface for this cab booking system that users can access from their devices. But for that, you should have knowledge of web development technologies like HTML, CSS, JavaScript, and Servlets/JSPs.
- You can also integrate a payment gateway to this application that is required to process transactions for booking a cab. You should be familiar with payment gateway APIs and how to integrate them into your application.

Cisco Ramon is an American software engineer who has experience in several popular and commercially successful programming languages and development tools. He has been writing content since last 5 years. He is a Senior Manager at Rude Labs Pvt. Ltd.
0 Comments