-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.java
More file actions
227 lines (163 loc) · 5.4 KB
/
Demo.java
File metadata and controls
227 lines (163 loc) · 5.4 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import javax.swing.table.*;
import java.io.*;
import java.util.ArrayList;
import javax.swing.*;
import java.awt.event.*;
public class Demo extends JFrame{
static JTextField txtName,txtUPrice,txtQuantity,txtTotal;
static DefaultTableModel model;
Demo(){
JLabel lblName = new JLabel("Fullname");
add(lblName).setBounds(30,50,100,10);
txtName = new JTextField();
add(txtName).setBounds(30,65,150,30);
JLabel lblUPrice = new JLabel("Unit Price");
add(lblUPrice).setBounds(30,100,100,10);
txtUPrice = new JTextField();
add(txtUPrice).setBounds(30,115,150,30);
JLabel lblQuantity = new JLabel("Quantity");
add(lblQuantity).setBounds(30,150,100,10);
txtQuantity = new JTextField();
add(txtQuantity).setBounds(30,165,150,30);
JLabel lblTotal = new JLabel("Total");
add(lblTotal).setBounds(30,200,100,10);
txtTotal = new JTextField();
add(txtTotal).setBounds(30,215,150,30);
txtTotal.setEditable(false);
JButton btnAdd = new JButton("Add");
add(btnAdd).setBounds(30,250,70,30);
JButton btnUpdate = new JButton("Update");
add(btnUpdate).setBounds(30,285,70,30);
JButton btnDelete = new JButton("Delete");
add(btnDelete).setBounds(30,320,70,30);
String columns[] = {"Fullname","UnitPrice", "Quantity", "Total"};
model = new DefaultTableModel(columns, 0);
JTable table = new JTable(model);
JScrollPane scrollpane = new JScrollPane(table);
add(scrollpane).setBounds(200,50,460,300);
table.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
int row = table.getSelectedRow();
if(row != -1) {
txtName.setText(model.getValueAt(row,0).toString());
txtUPrice.setText(model.getValueAt(row, 1).toString());
txtQuantity.setText(model.getValueAt(row, 2).toString());
txtTotal.setText(model.getValueAt(row, 3).toString());
}
}
});
read();
btnAdd.addActionListener(e->{
try {
FileWriter fw = new FileWriter("Billing.txt",true);
String name = txtName.getText();
double price = Double.parseDouble(txtUPrice.getText());
int quantity = Integer.parseInt(txtQuantity.getText());
double total = price*quantity;
txtTotal.setText(String.valueOf(total));
fw.write(name+"#"+price+"#"+quantity+"#"+total+"\n");
fw.close();
JOptionPane.showMessageDialog(null,"Record saved successfully!");
read();
} catch (IOException x) {
System.err.println("System Error" + x);
}
clear();
});
btnDelete.addActionListener(e->{
int selectedRow = table.getSelectedRow();
if(selectedRow == -1) {
JOptionPane.showMessageDialog(null, "Select a record to delete");
return;
}
int confirm = JOptionPane.showConfirmDialog(null, "Are you sure to delete this record?","Confirm Delete", JOptionPane.YES_NO_OPTION);
if(confirm != JOptionPane.YES_OPTION)return;
ArrayList<String> lines = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader ("Billing.txt"))){
String line;
int rowIndex=0;
while((line=br.readLine()) !=null) {
if(rowIndex !=selectedRow) lines.add(line);
rowIndex++;
}
}catch(IOException z) {
System.err.println(z);
}
//ArrayList -> File
try(BufferedWriter bw = new BufferedWriter (new FileWriter("Billing.txt"))){
for(String record:lines) bw.write(record+"\n");
}catch(IOException z) {
System.err.println(z);
}
read();
JOptionPane.showMessageDialog(null,"Deleted Successfully");
clear();
});
btnUpdate.addActionListener(e->{
int selectedRow = table.getSelectedRow();
if(selectedRow == -1) {
JOptionPane.showMessageDialog(null,"Select a record to Update");
return;
}
//File -> ArrayList
ArrayList<String> lines = new ArrayList<>();
try(BufferedReader br = new BufferedReader(new FileReader("Billing.txt"))){
String line;
int rowIndex=0;
while((line=br.readLine())!=null) {
if(rowIndex == selectedRow) {
double price = Double.parseDouble(txtUPrice.getText());
int quantity = Integer.parseInt(txtQuantity.getText());
double total = price*quantity;
txtTotal.setText(String.valueOf(total));
String updatedRecord = txtName.getText() + "#" + txtUPrice.getText() + "#" + txtQuantity.getText() + "#" + txtTotal.getText();
lines.add(updatedRecord);
}else {
lines.add(line);
}
rowIndex++;
}
}catch(IOException x) {
System.out.println(x);
}
//ArrayList -> File
try(BufferedWriter bw = new BufferedWriter (new FileWriter("Billing.txt"))){
for(String record:lines) bw.write(record+"\n");
}catch(IOException z) {
System.err.println(z);
}
read();
JOptionPane.showMessageDialog(null,"Updated Successfully");
clear();
});
//setUndecorated(true);
//setResizable(false);
setTitle("Billing System");
//setLocationRelativeTo(null);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(700,500);
setVisible(true);
}
public static void clear() {
txtName.setText("");
txtUPrice.setText("");
txtQuantity.setText("");
txtTotal.setText("");
}
public static void read() {
model.setRowCount(0);
try(BufferedReader br = new BufferedReader(new FileReader("Billing.txt"))){
String line;
while((line=br.readLine())!=null) {
String row[] = line.split("#");
model.addRow(row);
}
}catch(IOException e) {
System.out.println(e);
}
}
public static void main(String[]args) {
new Demo();
}
}