// 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));
}
}