-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddStudent.java
More file actions
171 lines (138 loc) · 6.28 KB
/
Copy pathAddStudent.java
File metadata and controls
171 lines (138 loc) · 6.28 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
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.*;
import java.util.List;
import javax.swing.*;
public class AddStudent extends JPanel {
private JComboBox<String> roomComboBox;
private JTextField studentNameField, fatherNameField, addressField, mobileField, cnicField;
public AddStudent() {
setLayout(null);
setBackground(new Color(230, 240, 255));
JLabel titleLabel = new JLabel("Add New Student");
titleLabel.setFont(new Font("SansSerif", Font.BOLD, 24));
titleLabel.setBounds(20, 20, 300, 30);
add(titleLabel);
JLabel studentNameLabel = new JLabel("Student Name:");
studentNameLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
studentNameLabel.setBounds(20, 70, 120, 25);
add(studentNameLabel);
studentNameField = new JTextField();
studentNameField.setBounds(140, 70, 200, 25);
add(studentNameField);
JLabel fatherNameLabel = new JLabel("Father's Name:");
fatherNameLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
fatherNameLabel.setBounds(20, 110, 120, 25);
add(fatherNameLabel);
fatherNameField = new JTextField();
fatherNameField.setBounds(140, 110, 200, 25);
add(fatherNameField);
JLabel addressLabel = new JLabel("Address:");
addressLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
addressLabel.setBounds(20, 150, 120, 25);
add(addressLabel);
addressField = new JTextField();
addressField.setBounds(140, 150, 200, 25);
add(addressField);
JLabel mobileLabel = new JLabel("Mobile Number:");
mobileLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
mobileLabel.setBounds(20, 190, 120, 25);
add(mobileLabel);
mobileField = new JTextField();
mobileField.setBounds(140, 190, 200, 25);
add(mobileField);
JLabel cnicLabel = new JLabel("CNIC Number:");
cnicLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
cnicLabel.setBounds(20, 230, 120, 25);
add(cnicLabel);
cnicField = new JTextField();
cnicField.setBounds(140, 230, 200, 25);
add(cnicField);
JLabel roomLabel = new JLabel("Select Room:");
roomLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
roomLabel.setBounds(20, 270, 120, 25);
add(roomLabel);
roomComboBox = new JComboBox<>();
roomComboBox.setBounds(140, 270, 200, 25);
add(roomComboBox);
loadAvailableRooms(roomComboBox);
JButton saveButton = new JButton("Save");
saveButton.setBounds(140, 310, 100, 30);
add(saveButton);
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = studentNameField.getText().trim();
String fatherName = fatherNameField.getText().trim();
String address = addressField.getText().trim();
String mobile = mobileField.getText().trim();
String cnic = cnicField.getText().trim();
String roomNo = (String) roomComboBox.getSelectedItem();
if (name.isEmpty() || fatherName.isEmpty() || address.isEmpty() ||
mobile.length() != 11 || cnic.isEmpty() || roomNo == null) {
JOptionPane.showMessageDialog(AddStudent.this, "Please fill all fields correctly.");
return;
}
try {
List<String> lines = Files.readAllLines(Paths.get("rooms.txt"));
boolean roomFound = false;
for (int i = 0; i < lines.size(); i++) {
String[] parts = lines.get(i).split(",");
if (parts[0].equals(roomNo)) {
if (parts[2].equals("Not Booked")) {
parts[2] = "Booked";
lines.set(i, String.join(",", parts));
roomFound = true;
} else {
JOptionPane.showMessageDialog(AddStudent.this, "Room is already booked.");
return;
}
break;
}
}
if (!roomFound) {
JOptionPane.showMessageDialog(AddStudent.this, "Room not found.");
return;
}
Files.write(Paths.get("rooms.txt"), lines);
try (BufferedWriter writer = new BufferedWriter(new FileWriter("students.txt", true))) {
writer.write(name + "," + fatherName + "," + address + "," + mobile + "," + cnic + "," + roomNo);
writer.newLine();
}
JOptionPane.showMessageDialog(AddStudent.this, "Student added successfully.");
resetForm();
loadAvailableRooms(roomComboBox);
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(AddStudent.this, "Error saving data.");
}
}
});
}
private void loadAvailableRooms(JComboBox<String> comboBox) {
comboBox.removeAllItems();
try (BufferedReader reader = new BufferedReader(new FileReader("rooms.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3 && parts[1].equals("Yes") && parts[2].equals("Not Booked")) {
comboBox.addItem(parts[0]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
comboBox.setMaximumRowCount(15);
}
private void resetForm() {
studentNameField.setText("");
fatherNameField.setText("");
addressField.setText("");
mobileField.setText("");
cnicField.setText("");
}
public void reloadAvailableRooms() {
loadAvailableRooms(roomComboBox);
}
}