-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateCustomer.java
More file actions
216 lines (176 loc) · 6.27 KB
/
Copy pathUpdateCustomer.java
File metadata and controls
216 lines (176 loc) · 6.27 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.io.*;
import java.lang.*;
class UpdateCustomer extends JFrame implements ActionListener{
private JLabel lblTitle,lblBack;
private JButton btnSearch,btnCancel,btnUpdate;
private JLabel lblCusId,lblName,lblQty,lblStatus;
private JTextField txtCusId,txtName,txtQty,txtStatus,txtSearch;
private CustomerList customerList;
private Customer cus;
public int index;
UpdateCustomer(CustomerList customerListLocal){
this.customerList = customerListLocal;
setSize(600,350);
setVisible(true);
setTitle("Update Customer Form");
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setBackground(Color.LIGHT_GRAY);
JPanel northPanel=new JPanel(new GridLayout(1,2));
northPanel.setBackground(new Color(51,1,0));
lblTitle=new JLabel("Update Customer");
lblTitle.setFont(new Font("",1,25));
lblTitle.setForeground(new Color(255,255, 0));
lblTitle.setHorizontalAlignment(0);
northPanel.add(lblTitle);
add(northPanel,BorderLayout.NORTH);
JPanel southRowPanel = new JPanel(new GridLayout(2,1,10,10));
JPanel buttonPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));
btnUpdate=new JButton("Update Customer");
btnUpdate.setFont(new Font("",1,18));
btnUpdate.setBackground(new Color(51,1,0));
btnUpdate.setForeground(Color.WHITE);
buttonPanel.add(btnUpdate);
btnCancel=new JButton("Cancel");
btnCancel.setFont(new Font("",1,18));
btnCancel.setBackground(Color.RED);
btnCancel.setForeground(Color.WHITE);
buttonPanel.add(btnCancel);
southRowPanel.add(buttonPanel);
JPanel southPanel = new JPanel(new FlowLayout());
lblBack = new JLabel("AllRights * Reserved by Rishindu Yohan(CE25260028)");
lblBack.setForeground(Color.WHITE);
lblBack.setFont(new Font("", Font.ITALIC, 12));
southPanel.add(lblBack);
southPanel.setBackground(new Color(51,2,0));
southRowPanel.add(southPanel);
add(southRowPanel,BorderLayout.SOUTH);
JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
txtSearch=new JTextField();
txtSearch.setColumns(10);
txtSearch.setFont(new Font("",1,18));
searchPanel.add(txtSearch);
btnSearch=new JButton("Search");
btnSearch.setFont(new Font("",1,20));
btnSearch.setBackground(Color.BLUE);
btnSearch.setForeground(Color.WHITE);
btnSearch.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
File customerFile=new File("CustomerFile.txt");
customerList.clear();
try{
Scanner input = new Scanner(customerFile);
while(input.hasNext()){
String line=input.nextLine();
if (line != null && !line.trim().isEmpty()) {
String[] rowData=line.split(",");
String id=rowData[0];
String name=rowData[1];
int qty=Integer.parseInt(rowData[2]);
int status=Integer.parseInt(rowData[3]);
Customer cus=new Customer(id,name,qty,status);
customerList.add(cus);
}
}
input.close();
customerFile.delete();
String cusId = txtSearch.getText();
index = customerList.indexOfStr(cusId);
cus=customerList.get(index);
if(cus==null){
JOptionPane.showMessageDialog(null, "Customer Not Found.","Not Found",JOptionPane.ERROR_MESSAGE);
clearTextField();
}
int qty = cus.getQty();
int order = cus.getStatus();
txtCusId.setText(cus.getCusId());
txtName.setText(cus.getName());
txtQty.setText(""+qty);
txtStatus.setText(""+order);
}catch(IOException ex){
System.out.println(ex.getMessage());
}
}
});
searchPanel.add(btnSearch);
add(searchPanel,BorderLayout.EAST);
JPanel labelPanel=new JPanel(new GridLayout(4,1,4,4));
lblCusId=new JLabel("Customer ID : ");
lblCusId.setFont(new Font("",1,18));
labelPanel.add(lblCusId);
lblName=new JLabel("Name : ");
lblName.setFont(new Font("",1,18));
labelPanel.add(lblName);
lblQty=new JLabel("Qty : ");
lblQty.setFont(new Font("",1,18));
labelPanel.add(lblQty);
lblQty=new JLabel("Order Status : ");
lblQty.setFont(new Font("",1,18));
labelPanel.add(lblQty);
add("West",labelPanel);
JPanel textPanel=new JPanel(new GridLayout(4,1,4,4));
JPanel idPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
txtCusId=new JTextField(10);
txtCusId.setFont(new Font("",1,18));
idPanel.add(txtCusId);
textPanel.add(idPanel);
JPanel namePanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
txtName=new JTextField(10);
txtName.setFont(new Font("",1,18));
namePanel.add(txtName);
textPanel.add(namePanel);
JPanel qtyPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
txtQty=new JTextField(10);
txtQty.setFont(new Font("",1,18));
qtyPanel.add(txtQty);
textPanel.add(qtyPanel);
JPanel StatusPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
txtStatus=new JTextField(10);
txtStatus.setFont(new Font("",1,18));
StatusPanel.add(txtStatus);
textPanel.add(StatusPanel);
add("Center",textPanel);
btnUpdate.addActionListener(this);
btnCancel.addActionListener(this);
}
public void clearTextField(){
txtCusId.setText("");
txtName.setText("");
txtQty.setText("");
txtStatus.setText("");
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnUpdate){
String id = txtSearch.getText();
String name = txtName.getText();
String qty = txtQty.getText();
String order = txtStatus.getText();
cus = new Customer(id,name,Integer.parseInt(qty),Integer.parseInt(order));
boolean isUpdated = customerList.set(index,cus);
if(isUpdated){
try{
FileWriter fw = new FileWriter("CustomerFile.txt",true);
for (int i = 0; i < customerList.size(); i++){
Customer customer = customerList.get(i);
fw.write(customer+"\n");
}
fw.close();
}catch(IOException ex){
}
JOptionPane.showConfirmDialog(null, "Customer Data Updated Sucessfully..","Customer Updated",JOptionPane.OK_CANCEL_OPTION,JOptionPane.INFORMATION_MESSAGE);
clearTextField();
}else{
JOptionPane.showMessageDialog(null, "Customer Update Failed..","Not Updated",JOptionPane.ERROR_MESSAGE);
clearTextField();
}
}
if(e.getSource()==btnCancel){
dispose();
}
}
}