-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddEmployee.java
More file actions
165 lines (140 loc) · 6.39 KB
/
Copy pathAddEmployee.java
File metadata and controls
165 lines (140 loc) · 6.39 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class AddEmployee extends JPanel {
private JTextField employeeNameField, fatherNameField, addressField, mobileField, cnicField;
private JComboBox<String> roomComboBox;
public AddEmployee() {
setLayout(null);
setBackground(new Color(230, 240, 255));
setBounds(350, 80, 700, 500);
JLabel titleLabel = new JLabel("Add Employee");
titleLabel.setFont(new Font("SansSerif", Font.BOLD, 24));
titleLabel.setBounds(20, 10, 200, 30);
add(titleLabel);
JLabel employeeNameLabel = new JLabel("Employee Name:");
employeeNameLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
employeeNameLabel.setBounds(20, 50, 120, 25);
add(employeeNameLabel);
employeeNameField = new JTextField();
employeeNameField.setBounds(140, 50, 200, 25);
add(employeeNameField);
JLabel fatherNameLabel = new JLabel("Father Name:");
fatherNameLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
fatherNameLabel.setBounds(20, 90, 120, 25);
add(fatherNameLabel);
fatherNameField = new JTextField();
fatherNameField.setBounds(140, 90, 200, 25);
add(fatherNameField);
JLabel addressLabel = new JLabel("Address:");
addressLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
addressLabel.setBounds(20, 130, 120, 25);
add(addressLabel);
addressField = new JTextField();
addressField.setBounds(140, 130, 200, 25);
add(addressField);
JLabel mobileLabel = new JLabel("Mobile No:");
mobileLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
mobileLabel.setBounds(20, 170, 120, 25);
add(mobileLabel);
mobileField = new JTextField();
mobileField.setBounds(140, 170, 200, 25);
add(mobileField);
JLabel cnicLabel = new JLabel("CNIC:");
cnicLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
cnicLabel.setBounds(20, 210, 120, 25);
add(cnicLabel);
cnicField = new JTextField();
cnicField.setBounds(140, 210, 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, 320, 100, 30);
add(saveButton);
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = employeeNameField.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.matches("\\d{11}") || !cnic.matches("\\d{5}-\\d{7}-\\d") ||
roomNo == null) {
JOptionPane.showMessageDialog(AddEmployee.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 line = lines.get(i);
String[] parts = line.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(AddEmployee.this, "Room is already booked.");
return;
}
break;
}
}
if (!roomFound) {
JOptionPane.showMessageDialog(AddEmployee.this, "Room not found.");
return;
}
Files.write(Paths.get("rooms.txt"), lines);
try (BufferedWriter writer = new BufferedWriter(new FileWriter("employees.txt", true))) {
writer.write(name + "," + fatherName + "," + address + "," + mobile + "," + cnic + "," + roomNo);
writer.newLine();
}
JOptionPane.showMessageDialog(AddEmployee.this, "Employee added successfully.");
employeeNameField.setText("");
fatherNameField.setText("");
addressField.setText("");
mobileField.setText("");
cnicField.setText("");
loadAvailableRooms(roomComboBox);
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(AddEmployee.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);
}
public void reloadAvailableRooms() {
loadAvailableRooms(roomComboBox);
}
}