Hotel Billing System In Java | Java Project

by | Nov 17, 2022 | Coding, Java

Home » Coding » Hotel Billing System In Java | Java Project

Introduction of the Project

Today we are going to write the source code for a Hotel Billing System in Java. The basic functionality of this Java project will be to store the check-in date, check-out date & room-service charge in the SQL database in order to calculate the total bill. We have implemented a basic GUI also to make the billing system easy to handle.

 

Objectives

  • To implement the tables that contain the customer information like check-in date, check-out date & room-service charge and attach the functionality of calculating bill information.
  • To make an efficient GUI interface for handling the billing system.

Requirements

Source Code

package com.company;
public class Main {
public static void main(String[] args) {
new Hotel();
}
}
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.sql.*;
import java.text.DateFormat;
import java.util.Vector;
import java.util.concurrent.TimeUnit;
public class Hotel {
private JPanel hotelPanel;
private JTextField roomNo;
private JTextField name;
private JTextField roomCharge;
private JTable table1;
private JPanel checkOutDate;
private JPanel checkInDate;
private JButton ADDButton;
JFrame hotel = new JFrame();
JDateChooser checkIn = new JDateChooser();
JDateChooser checkOut = new JDateChooser();
public Hotel(){
hotel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hotel.setContentPane(hotelPanel);
hotel.pack();
hotel.setLocationRelativeTo(null);
hotel.setSize(450,400);
hotel.setVisible(true);
checkInDate.add(checkIn);
checkOutDate.add(checkOut);
tableData();
ADDButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(roomNo.getText().equals("")|| name.getText().equals("")|| roomCharge.getText().equals("")|| checkIn.getDate()==null||checkOut.getDate()==null){
JOptionPane.showMessageDialog(null,"Please Fill All Fields to add Record.");
}else if(checkOut.getDate().before(checkIn.getDate())){
JOptionPane.showMessageDialog(null,"Check Out Date Cannot Be Before Check In Date!");
}else{
try {
String sql = "insert into hotel"+"(Room_No,Name,Check_In,Check_Out,Room_Service,Total_Amount)"+"values (?,?,?,?,?,?)";
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/intern","root","root");
PreparedStatement statement = connection.prepareStatement(sql);
// total calculation
long millies = Math.abs(checkOut.getDate().getTime()-checkIn.getDate().getTime());
long days = TimeUnit.DAYS.convert(millies,TimeUnit.MILLISECONDS);
int total = (int)days*1000 + Integer.parseInt(roomCharge.getText());
// conversion
String outDate = DateFormat.getDateInstance().format(checkOut.getDate());
String inDate = DateFormat.getDateInstance().format(checkIn.getDate());
statement.setInt(1,Integer.parseInt(roomNo.getText()));
statement.setString(2, name.getText());
statement.setString(3, inDate);
statement.setString(4,outDate);
statement.setString(5,roomCharge.getText());
statement.setInt(6,total);
statement.executeUpdate();
JOptionPane.showMessageDialog(null,"ITEM ADDED SUCCESSFULLY");
roomNo.setText("");
name.setText("");
roomCharge.setText("");
checkIn.setCalendar(null);
checkOut.setCalendar(null);
}catch (Exception ex){
JOptionPane.showMessageDialog(null,ex.getMessage());
}
tableData();
}
}
});
}
public void tableData() {
try{
String a= "Select* from hotel";
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(new DefaultTableModel(null, new String[]{"ID", "ITEM NAME", "QUANTITY", "PRICE"}));
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

To explain the working of this code, let us divide it into two parts.

Firstly, Let us look at the GUI first:

1. It consists of 3 text fields and 2 JCalendar that require the customer’s information to be added to the database.

2. Then, we add an “ADD” button, which inserts data into the database from the text fields.

Now in the retrieval part, we will apply the following:

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. Calculate the total bill by getting the number of days of stay from dates entered by the user & multiplying it by 1000, and finally adding the result to the room-service charge.

5. This information also gets stored in the database.

Output

Main Interface

Hotel Billing System In Java | Java Project

Conclusion

We have successfully managed to make a project of a Hotel Billing System In Java to generate bills of hotel rooms with the Swing module used for GUI. This is a very quick way to get bills ready for the customer. It stores all the required information about the customer along with the amount the person needs to pay.

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 *