- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuideRegistrationPage.java
More file actions
Latest commit
308 lines (259 loc) · 12.9 KB
/
Copy pathGuideRegistrationPage.java
File metadata and controls
308 lines (259 loc) · 12.9 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
importjavax.swing.*;
importjavax.swing.text.JTextComponent;
importjava.awt.*;
importjava.awt.event.FocusAdapter;
importjava.awt.event.FocusEvent;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.io.*;
importjava.util.Scanner;
publicclassGuideRegistrationPageextendsJPanel {
privateJTextFieldnameField, numberField, emailField, usernameField, addressField, experienceField, costField, detailsField, areaField, languageField, bankField, bankNameField;
privateJPasswordFieldpasswordField;
privateMainFramemainFrame;
publicGuideRegistrationPage(MainFramemainFrame) {
this.mainFrame = mainFrame;
setLayout(newBorderLayout());
// Set the background image
ImageIconbackgroundImageIcon = newImageIcon("reg-guide.jpg"); // Replace with your image file
ImagebackgroundImage = backgroundImageIcon.getImage();
BackgroundPanelbackgroundPanel = newBackgroundPanel(backgroundImage);
backgroundPanel.setLayout(newGridBagLayout()); // To center the form panel
// Create form panel
JPanelformPanel = newJPanel(newGridBagLayout());
formPanel.setPreferredSize(newDimension(600, 800)); // Adjust size as needed
formPanel.setBackground(newColor(255, 255, 224, 200)); // Light yellow with some transparency
formPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); // Padding around the form
GridBagConstraintsgbc = newGridBagConstraints();
gbc.insets = newInsets(5, 15, 5, 15); // Spacing between components
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
// Title Label
JLabeltitleLabel = newJLabel("Guide Registration", SwingConstants.CENTER);
titleLabel.setFont(newFont("Arial", Font.BOLD, 32));
titleLabel.setForeground(newColor(64, 64, 64)); // Dark gray color
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
formPanel.add(titleLabel, gbc);
gbc.gridwidth = 1; // Reset gridwidth after title
// Name Label and Field
gbc.gridy = 1;
addLabelAndField(formPanel, gbc, "Full Name:", nameField = createStyledTextField());
// Phone Number Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Phone Number:", numberField = createStyledTextField());
// Email Address Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Email Address:", emailField = createStyledTextField());
// Username Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Username:", usernameField = createStyledTextField());
// Password Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Password:", passwordField = createStyledPasswordField());
// Address Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Address:", addressField = createStyledTextField());
// Experience Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Experience in Year:", experienceField = createStyledTextField());
// Per Hour Cost Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Per Day Cost in BDT:", costField = createStyledTextField());
// Details Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Details:", detailsField = createStyledTextField());
// Covered Area Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Covered Area:", areaField = createStyledTextField());
// Preferred Language Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Preferred Language:", languageField = createStyledTextField());
// Bank A/C No. Label and Field
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Bank A/C No.:", bankField = createStyledTextField());
// Your Bank Name Label and Field (Extra Field)
gbc.gridy++;
addLabelAndField(formPanel, gbc, "Your Bank Name:", bankNameField = createStyledTextField());
// Register and Back Buttons
JButtonregisterButton = newJButton("Register");
styleButton(registerButton);
registerButton.addActionListener(e -> registerGuide());
JButtonbackButton = newJButton("Back");
styleButton(backButton);
backButton.addActionListener(e -> mainFrame.showPage("SelectionPage"));
// Button Panel to hold the buttons
JPanelbuttonPanel = newJPanel(newFlowLayout(FlowLayout.CENTER, 20, 0));
buttonPanel.setOpaque(false); // Transparent background
buttonPanel.add(registerButton);
buttonPanel.add(backButton);
gbc.gridy++;
gbc.gridx = 0;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.NONE;
formPanel.add(buttonPanel, gbc);
// Add formPanel to the backgroundPanel
backgroundPanel.add(formPanel);
// Add backgroundPanel to the main panel
add(backgroundPanel, BorderLayout.CENTER);
}
privatevoidaddLabelAndField(JPanelpanel, GridBagConstraintsgbc, StringlabelText, JComponentfield) {
JLabellabel = newJLabel(labelText);
label.setFont(newFont("Arial", Font.PLAIN, 16));
label.setForeground(newColor(64, 64, 64)); // Dark gray color
gbc.gridx = 0;
panel.add(label, gbc);
gbc.gridx = 1;
panel.add(field, gbc);
}
privateJTextFieldcreateStyledTextField() {
JTextFieldtextField = newJTextField();
styleTextField(textField);
// Increase input field height
textField.setPreferredSize(newDimension(250, 35)); // Adjust height as needed
returntextField;
}
privateJPasswordFieldcreateStyledPasswordField() {
JPasswordFieldpasswordField = newJPasswordField();
styleTextField(passwordField);
// Increase input field height
passwordField.setPreferredSize(newDimension(250, 35)); // Adjust height as needed
returnpasswordField;
}
privatevoidstyleTextField(JTextComponenttextField) {
textField.setFont(newFont("Arial", Font.PLAIN, 16));
textField.setForeground(Color.BLACK);
textField.setBorder(BorderFactory.createLineBorder(newColor(200, 200, 200), 1));
textField.setBackground(newColor(255, 255, 240)); // Light yellow background
textField.addFocusListener(newFocusAdapter() {
publicvoidfocusGained(FocusEvente) {
textField.setBorder(BorderFactory.createLineBorder(newColor(64, 64, 64), 1));
}
publicvoidfocusLost(FocusEvente) {
textField.setBorder(BorderFactory.createLineBorder(newColor(200, 200, 200), 1));
}
});
// Ensure the text field is empty
textField.setText("");
}
privatevoidstyleButton(JButtonbutton) {
button.setFont(newFont("Arial", Font.BOLD, 18));
button.setForeground(Color.WHITE);
button.setBackground(newColor(64, 64, 64)); // Dark gray color
button.setFocusPainted(false);
button.setBorderPainted(false);
button.setOpaque(true);
button.setCursor(newCursor(Cursor.HAND_CURSOR));
button.setMargin(newInsets(10, 20, 10, 20));
// Add mouse hover effect
button.addMouseListener(newMouseAdapter() {
publicvoidmouseEntered(MouseEvente) {
button.setBackground(newColor(255, 204, 0)); // Yellow color on hover
}
publicvoidmouseExited(MouseEvente) {
button.setBackground(newColor(64, 64, 64)); // Original color
}
});
}
// Method to handle guide registration logic
privatevoidregisterGuide() {
Stringname = nameField.getText().trim();
Stringnumber = numberField.getText().trim();
Stringemail = emailField.getText().trim();
Stringusername = usernameField.getText().trim();
Stringpassword = newString(passwordField.getPassword()).trim();
Stringaddress = addressField.getText().trim();
Stringexperience = experienceField.getText().trim();
Stringcost = costField.getText().trim();
Stringdetails = detailsField.getText().trim();
Stringarea = areaField.getText().trim();
Stringlanguage = languageField.getText().trim();
StringbankAccount = bankField.getText().trim();
StringbankName = bankNameField.getText().trim(); // New field
// Check if any of the fields are empty
StringBuilderemptyFields = newStringBuilder();
if (name.isEmpty()) emptyFields.append(" Full Name\n");
if (number.isEmpty()) emptyFields.append(" Phone Number\n");
if (email.isEmpty()) emptyFields.append(" Email Address\n");
if (username.isEmpty()) emptyFields.append(" Username\n");
if (password.isEmpty()) emptyFields.append(" Password\n");
if (address.isEmpty()) emptyFields.append(" Address\n");
if (experience.isEmpty()) emptyFields.append(" Experience\n");
if (cost.isEmpty()) emptyFields.append(" Per Hour Cost\n");
if (details.isEmpty()) emptyFields.append(" Details\n");
if (area.isEmpty()) emptyFields.append(" Covered Area\n");
if (language.isEmpty()) emptyFields.append(" Preferred Language\n");
if (bankAccount.isEmpty()) emptyFields.append(" Bank A/C No.\n");
if (bankName.isEmpty()) emptyFields.append(" Your Bank Name\n");
if (emptyFields.length() > 0) {
JOptionPane.showMessageDialog(this, "Please fill the following fields:\n" + emptyFields.toString());
return;
}
// Check for duplicate username in TravelersData.txt and GuidesData.txt
if (isUsernameTaken(username, "TravelersData.txt", "GuidesData.txt")) {
JOptionPane.showMessageDialog(this, "Username already exists. Please choose another username.");
return;
}
// Save guide information to GuidesData.txt
try (BufferedWriterwriter = newBufferedWriter(newFileWriter("GuidesData.txt", true))) {
writer.write(String.join(",", name, number, email, username, password, address, experience, cost, details, area, language, bankAccount, bankName));
writer.newLine();
JOptionPane.showMessageDialog(this, "Registration Successful!");
// Clear all fields after successful registration
clearFields();
// Optionally, navigate to the login page
mainFrame.showPage("LoginPage");
} catch (IOExceptione) {
e.printStackTrace();
}
}
// Method to clear all input fields
privatevoidclearFields() {
nameField.setText("");
numberField.setText("");
emailField.setText("");
usernameField.setText("");
passwordField.setText("");
addressField.setText("");
experienceField.setText("");
costField.setText("");
detailsField.setText("");
areaField.setText("");
languageField.setText("");
bankField.setText("");
bankNameField.setText("");
}
// Method to check if a username already exists in the files
privatebooleanisUsernameTaken(Stringusername, StringfilePath1, StringfilePath2) {
returnisUsernameInFile(username, filePath1) || isUsernameInFile(username, filePath2);
}
// Helper method to check if username exists in a specific file
privatebooleanisUsernameInFile(Stringusername, StringfilePath) {
try (Scannerscanner = newScanner(newFile(filePath))) {
while (scanner.hasNextLine()) {
String[] userInfo = scanner.nextLine().split(",");
if (userInfo.length > 3 && userInfo[3].equals(username)) { // Assuming username is the 4th field (index 3)
returntrue;
}
}
} catch (IOExceptione) {
e.printStackTrace();
}
returnfalse;
}
// Custom JPanel to draw the background image
classBackgroundPanelextendsJPanel {
privateImagebackgroundImage;
publicBackgroundPanel(ImagebackgroundImage) {
this.backgroundImage = backgroundImage;
setPreferredSize(newDimension(backgroundImage.getWidth(null), backgroundImage.getHeight(null)));
}
@Override
protectedvoidpaintComponent(Graphicsg) {
super.paintComponent(g);
// Draw the background image scaled to fit the panel
g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
}
}
}