-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateDeleteEmployee.java
More file actions
259 lines (236 loc) · 10.1 KB
/
Copy pathUpdateDeleteEmployee.java
File metadata and controls
259 lines (236 loc) · 10.1 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
import java.util.List;
import javax.swing.*;
public class UpdateDeleteEmployee extends JPanel {
private JTextField mobileField, nameField, fatherNameField, addressField, cnicField, roomNoField;
private JComboBox<String> livingStatusCombo;
private JButton searchButton, updateButton, deleteButton;
private List<String> employeeLines;
private int employeeIndex;
private String originalRoomNo;
public UpdateDeleteEmployee() {
setLayout(null);
setBackground(new Color(230, 240, 255));
setBounds(350, 80, 700, 500);
JLabel titleLabel = new JLabel("Update & Delete Employee");
titleLabel.setFont(new Font("SansSerif", Font.BOLD, 24));
titleLabel.setBounds(20, 20, 300, 30);
add(titleLabel);
JLabel mobileLabel = new JLabel("Mobile Number:");
mobileLabel.setBounds(20, 70, 120, 25);
add(mobileLabel);
mobileField = new JTextField();
mobileField.setBounds(140, 70, 200, 25);
add(mobileField);
searchButton = new JButton("Search");
searchButton.setBounds(350, 70, 100, 25);
add(searchButton);
JLabel nameLabel = new JLabel("Name:");
nameLabel.setBounds(20, 120, 120, 25);
add(nameLabel);
nameField = new JTextField();
nameField.setBounds(140, 120, 200, 25);
nameField.setEnabled(false);
add(nameField);
JLabel fatherNameLabel = new JLabel("Father's Name:");
fatherNameLabel.setBounds(20, 160, 120, 25);
add(fatherNameLabel);
fatherNameField = new JTextField();
fatherNameField.setBounds(140, 160, 200, 25);
fatherNameField.setEnabled(false);
add(fatherNameField);
JLabel addressLabel = new JLabel("Address:");
addressLabel.setBounds(20, 200, 120, 25);
add(addressLabel);
addressField = new JTextField();
addressField.setBounds(140, 200, 200, 25);
addressField.setEnabled(false);
add(addressField);
JLabel cnicLabel = new JLabel("CNIC:");
cnicLabel.setBounds(20, 240, 120, 25);
add(cnicLabel);
cnicField = new JTextField();
cnicField.setBounds(140, 240, 200, 25);
cnicField.setEnabled(false);
add(cnicField);
JLabel roomNoLabel = new JLabel("Room Number:");
roomNoLabel.setBounds(20, 280, 120, 25);
add(roomNoLabel);
roomNoField = new JTextField();
roomNoField.setBounds(140, 280, 200, 25);
roomNoField.setEnabled(false);
add(roomNoField);
JLabel livingStatusLabel = new JLabel("Living Status:");
livingStatusLabel.setBounds(20, 320, 120, 25);
add(livingStatusLabel);
String[] statusOptions = {"Living", "Leaved"};
livingStatusCombo = new JComboBox<>(statusOptions);
livingStatusCombo.setBounds(140, 320, 200, 25);
livingStatusCombo.setEnabled(false);
add(livingStatusCombo);
updateButton = new JButton("Update");
updateButton.setBounds(140, 360, 100, 30);
updateButton.setEnabled(false);
add(updateButton);
deleteButton = new JButton("Delete");
deleteButton.setBounds(250, 360, 100, 30);
deleteButton.setEnabled(false);
add(deleteButton);
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
searchEmployee();
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updateEmployee();
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteEmployee();
}
});
}
private void searchEmployee() {
String mobile = mobileField.getText().trim();
if (mobile.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please enter a mobile number.");
return;
}
try {
employeeLines = Files.readAllLines(Paths.get("employees.txt"));
for (int i = 0; i < employeeLines.size(); i++) {
String line = employeeLines.get(i);
String[] parts = line.split(",");
if (parts.length >= 6 && parts[3].equals(mobile)) {
nameField.setText(parts[0]);
fatherNameField.setText(parts[1]);
addressField.setText(parts[2]);
cnicField.setText(parts[4]);
roomNoField.setText(parts[5]);
livingStatusCombo.setSelectedItem("Living");
originalRoomNo = parts[5];
employeeIndex = i;
enableFields(true);
return;
}
}
JOptionPane.showMessageDialog(null, "Employee not found.");
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error reading employees file.");
}
}
private void enableFields(boolean enable) {
nameField.setEnabled(enable);
fatherNameField.setEnabled(enable);
addressField.setEnabled(enable);
cnicField.setEnabled(enable);
roomNoField.setEnabled(enable);
livingStatusCombo.setEnabled(enable);
updateButton.setEnabled(enable);
deleteButton.setEnabled(enable);
}
private void updateEmployee() {
String name = nameField.getText().trim();
String fatherName = fatherNameField.getText().trim();
String address = addressField.getText().trim();
String cnic = cnicField.getText().trim();
String mobile = mobileField.getText().trim();
String roomNo = roomNoField.getText().trim();
String livingStatus = (String) livingStatusCombo.getSelectedItem();
if (name.isEmpty() || fatherName.isEmpty() || address.isEmpty() || cnic.isEmpty() ||
mobile.length() != 11 || cnic.length() != 15) {
JOptionPane.showMessageDialog(null, "Please fill all fields correctly.");
return;
}
try {
List<String> roomLines = Files.readAllLines(Paths.get("rooms.txt"));
if (livingStatus.equals("Leaved")) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("leaved_employees.txt", true))) {
writer.write(employeeLines.get(employeeIndex));
writer.newLine();
}
employeeLines.remove(employeeIndex);
updateRoomStatus(roomLines, originalRoomNo, "Not Booked");
Files.write(Paths.get("employees.txt"), employeeLines);
Files.write(Paths.get("rooms.txt"), roomLines);
JOptionPane.showMessageDialog(null, "Employee marked as Leaved and moved to leaved employees.");
} else {
if (!roomNo.equals(originalRoomNo)) {
boolean roomAvailable = false;
for (String line : roomLines) {
String[] parts = line.split(",");
if (parts[0].equals(roomNo) && parts[1].equals("Yes") && parts[2].equals("Not Booked")) {
roomAvailable = true;
break;
}
}
if (!roomAvailable) {
JOptionPane.showMessageDialog(null, "Selected room is not available.");
return;
}
updateRoomStatus(roomLines, originalRoomNo, "Not Booked");
updateRoomStatus(roomLines, roomNo, "Booked");
}
String updatedLine = name + "," + fatherName + "," + address + "," + mobile + "," + cnic + "," + roomNo;
employeeLines.set(employeeIndex, updatedLine);
Files.write(Paths.get("employees.txt"), employeeLines);
Files.write(Paths.get("rooms.txt"), roomLines);
JOptionPane.showMessageDialog(null, "Employee details updated.");
}
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error updating employee data.");
} finally {
clearFields();
}
}
private void deleteEmployee() {
int confirm = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this employee?",
"Confirm Delete", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
try {
employeeLines.remove(employeeIndex);
List<String> roomLines = Files.readAllLines(Paths.get("rooms.txt"));
updateRoomStatus(roomLines, originalRoomNo, "Not Booked");
Files.write(Paths.get("employees.txt"), employeeLines);
Files.write(Paths.get("rooms.txt"), roomLines);
JOptionPane.showMessageDialog(null, "Employee deleted.");
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error deleting employee data.");
} finally {
clearFields();
}
}
}
private void updateRoomStatus(List<String> roomLines, String roomNo, String status) {
for (int i = 0; i < roomLines.size(); i++) {
String line = roomLines.get(i);
String[] parts = line.split(",");
if (parts[0].equals(roomNo)) {
parts[2] = status;
roomLines.set(i, String.join(",", parts));
break;
}
}
}
private void clearFields() {
mobileField.setText("");
nameField.setText("");
fatherNameField.setText("");
addressField.setText("");
cnicField.setText("");
roomNoField.setText("");
livingStatusCombo.setSelectedIndex(0);
enableFields(false);
}
}