-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateDeleteStudent.java
More file actions
243 lines (213 loc) · 10.2 KB
/
Copy pathUpdateDeleteStudent.java
File metadata and controls
243 lines (213 loc) · 10.2 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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
import java.util.List;
import javax.swing.*;
public class UpdateDeleteStudent extends JPanel {
private JTextField mobileField, nameField, fatherNameField, cnicField, roomNoField;
private JComboBox<String> livingStatusCombo;
private JButton updateButton, deleteButton;
private List<String> studentLines;
private int studentIndex;
private String originalRoomNo;
public UpdateDeleteStudent() {
setLayout(null);
setBackground(new Color(230, 240, 255));
setBounds(350, 80, 700, 500);
JLabel titleLabel = new JLabel("Update & Delete Student");
titleLabel.setFont(new Font("SansSerif", Font.BOLD, 24));
titleLabel.setBounds(20, 20, 300, 30);
add(titleLabel);
JLabel mobileLabel = new JLabel("Mobile Number:");
mobileLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
mobileLabel.setBounds(20, 70, 120, 25);
add(mobileLabel);
mobileField = new JTextField();
mobileField.setBounds(140, 70, 200, 25);
add(mobileField);
JButton 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 cnicLabel = new JLabel("CNIC:");
cnicLabel.setBounds(20, 200, 120, 25);
add(cnicLabel);
cnicField = new JTextField();
cnicField.setBounds(140, 200, 200, 25);
cnicField.setEnabled(false);
add(cnicField);
JLabel roomNoLabel = new JLabel("Room Number:");
roomNoLabel.setBounds(20, 240, 120, 25);
add(roomNoLabel);
roomNoField = new JTextField();
roomNoField.setBounds(140, 240, 200, 25);
roomNoField.setEnabled(false);
add(roomNoField);
JLabel livingStatusLabel = new JLabel("Living Status:");
livingStatusLabel.setBounds(20, 280, 120, 25);
add(livingStatusLabel);
String[] statusOptions = {"Living", "Non-Living"};
livingStatusCombo = new JComboBox<>(statusOptions);
livingStatusCombo.setBounds(140, 280, 200, 25);
livingStatusCombo.setEnabled(false);
add(livingStatusCombo);
updateButton = new JButton("Update");
updateButton.setBounds(140, 320, 100, 30);
updateButton.setEnabled(false);
add(updateButton);
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String mobile = mobileField.getText().trim();
if (mobile.isEmpty()) {
JOptionPane.showMessageDialog(null, "Please enter a mobile number.");
return;
}
try {
studentLines = Files.readAllLines(Paths.get("students.txt"));
for (int i = 0; i < studentLines.size(); i++) {
String line = studentLines.get(i);
String[] parts = line.split(",");
if (parts.length >= 6 && parts[3].equals(mobile)) {
nameField.setText(parts[0]);
fatherNameField.setText(parts[1]);
cnicField.setText(parts[4]);
roomNoField.setText(parts[5]);
roomNoField.setEditable(false);
livingStatusCombo.setSelectedItem("Living");
originalRoomNo = parts[5];
studentIndex = i;
nameField.setEnabled(true);
fatherNameField.setEnabled(true);
cnicField.setEnabled(true);
roomNoField.setEnabled(true);
livingStatusCombo.setEnabled(true);
updateButton.setEnabled(true);
deleteButton.setEnabled(true);
return;
}
}
JOptionPane.showMessageDialog(null, "Student not found.");
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error reading students file.");
}
}
});
updateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText().trim();
String fatherName = fatherNameField.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() || cnic.isEmpty() || mobile.length() != 11 || cnic.length() != 15) {
JOptionPane.showMessageDialog(null, "Please fill all fields correctly.");
return;
}
try {
String originalLine = studentLines.get(studentIndex);
String[] originalParts = originalLine.split(",");
String address = originalParts[2];
if (livingStatus.equals("Non-Living")) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("leaved_students.txt", true))) {
writer.write(originalLine);
writer.newLine();
}
studentLines.remove(studentIndex);
List<String> roomLines = Files.readAllLines(Paths.get("rooms.txt"));
for (int i = 0; i < roomLines.size(); i++) {
String line = roomLines.get(i);
String[] parts = line.split(",");
if (parts[0].equals(originalRoomNo)) {
parts[2] = "Not Booked";
roomLines.set(i, String.join(",", parts));
break;
}
}
Files.write(Paths.get("rooms.txt"), roomLines);
Files.write(Paths.get("students.txt"), studentLines);
JOptionPane.showMessageDialog(null, "Student marked as Non-Living and moved to leaved students.");
} else {
String updatedLine = name + "," + fatherName + "," + address + "," + mobile + "," + cnic + "," + roomNo;
studentLines.set(studentIndex, updatedLine);
Files.write(Paths.get("students.txt"), studentLines);
JOptionPane.showMessageDialog(null, "Student details updated.");
}
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error updating student data: " + ex.getMessage());
} finally {
clearFields();
}
}
});
deleteButton = new JButton("Delete");
deleteButton.setBounds(250, 320, 100, 30);
deleteButton.setEnabled(false);
add(deleteButton);
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int confirm = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this student?", "Confirm Delete", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
try {
String originalLine = studentLines.get(studentIndex);
studentLines.remove(studentIndex);
Files.write(Paths.get("students.txt"), studentLines);
List<String> roomLines = Files.readAllLines(Paths.get("rooms.txt"));
for (int i = 0; i < roomLines.size(); i++) {
String line = roomLines.get(i);
String[] parts = line.split(",");
if (parts[0].equals(originalRoomNo)) {
parts[2] = "Not Booked";
roomLines.set(i, String.join(",", parts));
break;
}
}
Files.write(Paths.get("rooms.txt"), roomLines);
JOptionPane.showMessageDialog(null, "Student deleted.");
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Error deleting student data: " + ex.getMessage());
} finally {
clearFields();
}
}
}
});
}
private void clearFields() {
nameField.setText("");
fatherNameField.setText("");
cnicField.setText("");
roomNoField.setText("");
livingStatusCombo.setSelectedIndex(0);
nameField.setEnabled(false);
fatherNameField.setEnabled(false);
cnicField.setEnabled(false);
roomNoField.setEnabled(false);
livingStatusCombo.setEnabled(false);
updateButton.setEnabled(false);
deleteButton.setEnabled(false);
mobileField.setText("");
mobileField.setEnabled(true);
mobileField.requestFocus();
}
}