-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignUpScreen.java
More file actions
184 lines (157 loc) · 7.03 KB
/
SignUpScreen.java
File metadata and controls
184 lines (157 loc) · 7.03 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
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class SignUpScreen extends JFrame {
private JTextField usernameField;
private JPasswordField passwordField;
private JPasswordField confirmPasswordField;
private JTextField emailField;
private JTextField fullNameField;
private JComboBox<String> roleComboBox;
public SignUpScreen() {
setTitle("Library Management System - Sign Up");
setSize(400, 500);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel titleLabel = new JLabel("Create New Account");
titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
gbc.anchor = GridBagConstraints.CENTER;
mainPanel.add(titleLabel, gbc);
gbc.gridwidth = 1;
gbc.gridy = 1;
mainPanel.add(new JLabel("Full Name:"), gbc);
gbc.gridx = 1;
fullNameField = new JTextField(20);
mainPanel.add(fullNameField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
mainPanel.add(new JLabel("Username:"), gbc);
gbc.gridx = 1;
usernameField = new JTextField(20);
mainPanel.add(usernameField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
mainPanel.add(new JLabel("Email:"), gbc);
gbc.gridx = 1;
emailField = new JTextField(20);
mainPanel.add(emailField, gbc);
gbc.gridx = 0;
gbc.gridy = 4;
mainPanel.add(new JLabel("Password:"), gbc);
gbc.gridx = 1;
passwordField = new JPasswordField(20);
mainPanel.add(passwordField, gbc);
gbc.gridx = 0;
gbc.gridy = 5;
mainPanel.add(new JLabel("Confirm Password:"), gbc);
gbc.gridx = 1;
confirmPasswordField = new JPasswordField(20);
mainPanel.add(confirmPasswordField, gbc);
gbc.gridx = 0;
gbc.gridy = 6;
mainPanel.add(new JLabel("Role:"), gbc);
gbc.gridx = 1;
String[] roles = {"Student", "Librarian"};
roleComboBox = new JComboBox<>(roles);
mainPanel.add(roleComboBox, gbc);
gbc.gridx = 0;
gbc.gridy = 7;
gbc.gridwidth = 2;
JButton signUpButton = new JButton("Sign Up");
signUpButton.addActionListener(e -> handleSignUp());
mainPanel.add(signUpButton, gbc);
gbc.gridy = 8;
JButton backButton = new JButton("Back to Login");
backButton.addActionListener(e -> {
new LoginScreen().setVisible(true);
this.dispose();
});
mainPanel.add(backButton, gbc);
add(mainPanel);
}
private void handleSignUp() {
String username = usernameField.getText().trim();
String password = new String(passwordField.getPassword());
String confirmPassword = new String(confirmPasswordField.getPassword());
String email = emailField.getText().trim();
String fullName = fullNameField.getText().trim();
String role = (String) roleComboBox.getSelectedItem();
if (username.isEmpty() || password.isEmpty() || email.isEmpty() || fullName.isEmpty()) {
JOptionPane.showMessageDialog(this, "All fields are required!");
return;
}
if (!password.equals(confirmPassword)) {
JOptionPane.showMessageDialog(this, "Passwords do not match!");
return;
}
if (!isValidEmail(email)) {
JOptionPane.showMessageDialog(this, "Please enter a valid email address!");
return;
}
try {
Connection conn = DatabaseConnection.getConnection();
String checkQuery = "SELECT COUNT(*) FROM users WHERE username = ?";
PreparedStatement checkStmt = conn.prepareStatement(checkQuery);
checkStmt.setString(1, username);
ResultSet rs = checkStmt.executeQuery();
rs.next();
if (rs.getInt(1) > 0) {
JOptionPane.showMessageDialog(this, "Username already exists!");
return;
}
checkQuery = "SELECT COUNT(*) FROM users WHERE email = ?";
checkStmt = conn.prepareStatement(checkQuery);
checkStmt.setString(1, email);
rs = checkStmt.executeQuery();
rs.next();
if (rs.getInt(1) > 0) {
JOptionPane.showMessageDialog(this, "Email already registered!");
return;
}
// INSERT with all 7 columns matching your DB: full_name, username, password, role, type, email, is_approved
String sql = "INSERT INTO users (full_name, username, password, role, type, email, is_approved) " +
"VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, fullName); // full_name
pstmt.setString(2, username); // username
pstmt.setString(3, password); // password
pstmt.setString(4, role.toUpperCase()); // role
pstmt.setString(5, role.toUpperCase()); // type (same as role)
pstmt.setString(6, email); // email
pstmt.setBoolean(7, false); // is_approved
pstmt.executeUpdate();
// Get the new user's ID to create a notification
String getIdQuery = "SELECT id FROM users WHERE username = ?";
PreparedStatement getIdStmt = conn.prepareStatement(getIdQuery);
getIdStmt.setString(1, username);
ResultSet idRs = getIdStmt.executeQuery();
if (idRs.next()) {
int newUserId = idRs.getInt("id");
String notifyQuery = "INSERT INTO notifications (user_id, message, type) VALUES (?, ?, ?)";
PreparedStatement notifyStmt = conn.prepareStatement(notifyQuery);
notifyStmt.setInt(1, newUserId);
notifyStmt.setString(2, "New " + role + " account registration: " + username);
notifyStmt.setString(3, "APPROVAL");
notifyStmt.executeUpdate();
}
JOptionPane.showMessageDialog(this,
"Account created successfully!\nPlease wait for admin approval to login.");
new LoginScreen().setVisible(true);
this.dispose();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Error creating account: " + e.getMessage());
}
}
private boolean isValidEmail(String email) {
String emailRegex = "^[A-Za-z0-9+_.-]+@(.+)$";
return email.matches(emailRegex);
}
}