0% found this document useful (0 votes)
90 views2 pages

Bill Creation Java Swing Project

The document contains a Java program for a Bill Creation System using Swing for the GUI. It allows users to input item details such as name, quantity, and price, and calculates the total cost of items added to a table. The program dynamically updates the grand total as items are added and displays it in the interface.

Uploaded by

chaitalibonde5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views2 pages

Bill Creation Java Swing Project

The document contains a Java program for a Bill Creation System using Swing for the GUI. It allows users to input item details such as name, quantity, and price, and calculates the total cost of items added to a table. The program dynamically updates the grand total as items are added and displays it in the interface.

Uploaded by

chaitalibonde5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

// BillGUI.

java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

class Item {
String name;
int quantity;
double price;

Item(String name, int quantity, double price) {


this.name = name;
this.quantity = quantity;
this.price = price;
}

double getTotal() {
return quantity * price;
}
}

public class BillGUI extends JFrame {


private JTextField nameField, quantityField, priceField;
private DefaultTableModel tableModel;
private JLabel totalLabel;
private ArrayList<Item> items = new ArrayList<>();

public BillGUI() {
setTitle("Bill Creation System");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

JPanel inputPanel = new JPanel(new GridLayout(4, 2));


inputPanel.add(new JLabel("Item Name:"));
nameField = new JTextField();
inputPanel.add(nameField);

inputPanel.add(new JLabel("Quantity:"));
quantityField = new JTextField();
inputPanel.add(quantityField);

inputPanel.add(new JLabel("Price per unit:"));


priceField = new JTextField();
inputPanel.add(priceField);

JButton addButton = new JButton("Add Item");


inputPanel.add(addButton);

totalLabel = new JLabel("Grand Total: 0.00");


inputPanel.add(totalLabel);

add(inputPanel, BorderLayout.NORTH);

tableModel = new DefaultTableModel(new String[]{"Item", "Qty", "Price", "Total"}, 0);


JTable table = new JTable(tableModel);
add(new JScrollPane(table), BorderLayout.CENTER);

addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
int quantity = Integer.parseInt(quantityField.getText());
double price = Double.parseDouble(priceField.getText());

Item item = new Item(name, quantity, price);


items.add(item);

tableModel.addRow(new Object[]{name, quantity, price, item.getTotal()});

double grandTotal = 0;
for (Item it : items) {
grandTotal += it.getTotal();
}
totalLabel.setText("Grand Total: " + String.format("%.2f", grandTotal));

nameField.setText("");
quantityField.setText("");
priceField.setText("");
}
});
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> new BillGUI().setVisible(true));
}
}

You might also like