-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBillingGUI.java
More file actions
133 lines (98 loc) · 4.04 KB
/
BillingGUI.java
File metadata and controls
133 lines (98 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import javax.swing.*;
import java.awt.event.*;
public class BillingGUI extends JFrame implements ActionListener {
JTextField txtOrder, txtCustomer, txtPhone, txtAddress;
JTextField txtInvoiceNo, txtInvoiceDate, txtCashier;
JTextArea txtItems;
JTextField txtTotal, txtCash, txtChange;
JButton btnCalculate, btnClear, btnExit;
public BillingGUI() {
setTitle("Billing System");
setSize(800, 550);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// Receipt Header
JLabel title = new JLabel("CASH RECEIPT", JLabel.CENTER);
title.setBounds(250, 10, 300, 30);
add(title);
JLabel shop = new JLabel("SUPERMARKET 123", JLabel.CENTER);
shop.setBounds(250, 35, 300, 20);
add(shop);
JLabel addr = new JLabel("12, Milkyway Galaxy / Earth", JLabel.CENTER);
addr.setBounds(250, 55, 300, 20);
add(addr);
JLabel tel = new JLabel("Tel : 123-456-7898", JLabel.CENTER);
tel.setBounds(250, 75, 300, 20);
add(tel);
// Left side
addLabel("Order ID", 40, 120); txtOrder = addField(160, 120);
addLabel("Customer Name", 40, 150); txtCustomer = addField(160, 150);
addLabel("Phone No", 40, 180); txtPhone = addField(160, 180);
addLabel("Address", 40, 210); txtAddress = addField(160, 210);
addLabel("Invoice No", 40, 240); txtInvoiceNo = addField(160, 240);
addLabel("Invoice Date", 40, 270); txtInvoiceDate = addField(160, 270);
addLabel("Cashier Name", 40, 300); txtCashier = addField(160, 300);
// Right side
txtItems = new JTextArea();
txtItems.setBounds(400, 120, 340, 200);
txtItems.setText("Item Qty Price");
add(txtItems);
addLabel("Total Amount", 400, 340); txtTotal = addField(520, 340);
addLabel("Cash Given", 400, 370); txtCash = addField(520, 370);
addLabel("Change Amount", 400, 400); txtChange = addField(520, 400);
// Buttons
btnCalculate = new JButton("CALCULATE");
btnCalculate.setBounds(200, 450, 120, 30);
btnCalculate.addActionListener(this);
add(btnCalculate);
btnClear = new JButton("CLEAR");
btnClear.setBounds(340, 450, 100, 30);
btnClear.addActionListener(this);
add(btnClear);
btnExit = new JButton("EXIT");
btnExit.setBounds(460, 450, 100, 30);
btnExit.addActionListener(this);
add(btnExit);
}
void addLabel(String t, int x, int y) {
JLabel l = new JLabel(t + " :");
l.setBounds(x, y, 120, 25);
add(l);
}
JTextField addField(int x, int y) {
JTextField f = new JTextField();
f.setBounds(x, y, 180, 25);
add(f);
return f;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnCalculate) {
double grandTotal = 0;
String itemsText = txtItems.getText();
String[] lines = itemsText.split("\n");
for (int i = 1; i < lines.length; i++) {
String line = lines[i].trim();
if (line.isEmpty()) continue;
String[] parts = line.split("\\s+");
int qty = Integer.parseInt(parts[1]);
double price = Double.parseDouble(parts[2]);
grandTotal += qty * price;
}
txtTotal.setText(String.valueOf(grandTotal));
// Change calculation
if (!txtCash.getText().isEmpty()) {
double cash = Double.parseDouble(txtCash.getText());
txtChange.setText(String.valueOf(cash - grandTotal));
}
}
if (e.getSource() == btnClear) {
txtItems.setText("Item Qty Price");
txtTotal.setText("");
txtCash.setText("");
txtChange.setText("");
}
if (e.getSource() == btnExit) {
System.exit(0);
}
}
}