Java Project – Restaurant Billing System

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

SQL Queries

CREATE DATABASE restaurant_db;
USE restaurant_db;

CREATE TABLE customers (
    customer_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    contact VARCHAR(15)
);

CREATE TABLE menu_items (
    item_id INT AUTO_INCREMENT PRIMARY KEY,
    item_name VARCHAR(100),
    price DECIMAL(10,2)
);

CREATE TABLE orders (
    order_id INT AUTO_INCREMENT PRIMARY KEY,
    customer_id INT,
    order_date DATE,
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

CREATE TABLE order_items (
    order_item_id INT AUTO_INCREMENT PRIMARY KEY,
    order_id INT,
    item_id INT,
    quantity INT,
    FOREIGN KEY (order_id) REFERENCES orders(order_id),
    FOREIGN KEY (item_id) REFERENCES menu_items(item_id)
);

CREATE TABLE payments (
    payment_id INT AUTO_INCREMENT PRIMARY KEY,
    order_id INT,
    payment_date DATE,
    amount DECIMAL(10,2),
    method VARCHAR(50),
    FOREIGN KEY (order_id) REFERENCES orders(order_id)
);

Program 1

package db;
import java.sql.*;

public class DBConnection {
    private static final String URL = "jdbc:mysql://localhost:3306/restaurant_db";
    private static final String USER = "root";
    private static final String PASSWORD = "";

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }
}

Program 2

package gui;
import db.DBConnection;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

public class BillingSystem extends JFrame {
    private JComboBox<String> itemCombo;
    private JTextField quantityField;
    private JTextArea billArea;
    private JButton addButton, payButton, deleteButton;
    private Map<String, Integer> billMap = new HashMap<>();

    public BillingSystem() {
        setTitle("Restaurant Billing System");
        setLayout(new FlowLayout());
        setSize(500, 500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        itemCombo = new JComboBox<>();
        loadItems();
        quantityField = new JTextField(5);
        billArea = new JTextArea(15, 40);
        billArea.setEditable(false);
        addButton = new JButton("Add/Update Item");
        deleteButton = new JButton("Delete Item");
        payButton = new JButton("Make Payment");

        add(new JLabel("Item:"));
        add(itemCombo);
        add(new JLabel("Qty:"));
        add(quantityField);
        add(addButton);
        add(deleteButton);
        add(payButton);
        add(new JScrollPane(billArea));

        addButton.addActionListener(e -> addOrUpdateItem());
        deleteButton.addActionListener(e -> deleteItem());
        payButton.addActionListener(e -> processPayment());

        setVisible(true);
    }

    private void loadItems() {
        try (Connection con = DBConnection.getConnection();
             Statement stmt = con.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT item_name FROM menu_items")) {
            while (rs.next()) {
                itemCombo.addItem(rs.getString("item_name"));
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error loading items: " + ex.getMessage());
        }
    }

    private void addOrUpdateItem() {
        String item = (String) itemCombo.getSelectedItem();
        int qty = Integer.parseInt(quantityField.getText());
        billMap.put(item, billMap.getOrDefault(item, 0) + qty);
        showBill();
    }

    private void deleteItem() {
        String item = (String) itemCombo.getSelectedItem();
        billMap.remove(item);
        showBill();
    }

    private void showBill() {
        billArea.setText("");
        double grandTotal = 0.0;

        try (Connection con = DBConnection.getConnection();
             PreparedStatement pst = con.prepareStatement("SELECT price FROM menu_items WHERE item_name = ?")) {

            for (String item : billMap.keySet()) {
                int qty = billMap.get(item);
                pst.setString(1, item);
                ResultSet rs = pst.executeQuery();
                if (rs.next()) {
                    double price = rs.getDouble("price");
                    double total = qty * price;
                    grandTotal += total;
                    billArea.append(item + " x " + qty + " = ₹" + total + "\n");
                }
            }
            billArea.append("----------------------------\n");
            billArea.append("Total: ₹" + grandTotal + "\n");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error displaying bill: " + ex.getMessage());
        }
    }

    private void processPayment() {
        if (billMap.isEmpty()) {
            JOptionPane.showMessageDialog(this, "No items in bill to pay.");
            return;
        }

        String[] methods = {"Cash", "Card", "UPI"};
        String method = (String) JOptionPane.showInputDialog(this, "Select payment method:", "Payment",
                JOptionPane.QUESTION_MESSAGE, null, methods, methods[0]);

        if (method == null) return;

        try (Connection con = DBConnection.getConnection()) {
            con.setAutoCommit(false);

            // Insert dummy customer
            PreparedStatement customerStmt = con.prepareStatement("INSERT INTO customers (name, contact) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);
            customerStmt.setString(1, "Walk-in");
            customerStmt.setString(2, "N/A");
            customerStmt.executeUpdate();
            ResultSet custKeys = customerStmt.getGeneratedKeys();
            custKeys.next();
            int customerId = custKeys.getInt(1);

            // Insert order
            PreparedStatement orderStmt = con.prepareStatement("INSERT INTO orders (customer_id, order_date) VALUES (?, CURDATE())", Statement.RETURN_GENERATED_KEYS);
            orderStmt.setInt(1, customerId);
            orderStmt.executeUpdate();
            ResultSet orderKeys = orderStmt.getGeneratedKeys();
            orderKeys.next();
            int orderId = orderKeys.getInt(1);

            // Insert order items
            PreparedStatement itemStmt = con.prepareStatement("SELECT item_id, price FROM menu_items WHERE item_name = ?");
            PreparedStatement insertOrderItem = con.prepareStatement("INSERT INTO order_items (order_id, item_id, quantity) VALUES (?, ?, ?)");
            double totalAmount = 0.0;

            for (String item : billMap.keySet()) {
                int qty = billMap.get(item);
                itemStmt.setString(1, item);
                ResultSet rs = itemStmt.executeQuery();
                if (rs.next()) {
                    int itemId = rs.getInt("item_id");
                    double price = rs.getDouble("price");
                    totalAmount += qty * price;

                    insertOrderItem.setInt(1, orderId);
                    insertOrderItem.setInt(2, itemId);
                    insertOrderItem.setInt(3, qty);
                    insertOrderItem.executeUpdate();
                }
            }

            // Insert payment
            PreparedStatement payStmt = con.prepareStatement("INSERT INTO payments (order_id, payment_date, amount, method) VALUES (?, CURDATE(), ?, ?)");
            payStmt.setInt(1, orderId);
            payStmt.setDouble(2, totalAmount);
            payStmt.setString(3, method);
            payStmt.executeUpdate();

            con.commit();
            JOptionPane.showMessageDialog(this, "Payment Successful! Receipt Generated.");
            billMap.clear();
            showBill();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error processing payment: " + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(BillingSystem::new);
    }
}

 

 

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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