-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddController.java
More file actions
95 lines (76 loc) · 3.09 KB
/
AddController.java
File metadata and controls
95 lines (76 loc) · 3.09 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
package jobinterviewpreparationsystem;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import jobinterviewpreparationsystem.DB.DatabaseConnection;
import javafx.scene.control.Alert;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import jobinterviewpreparationsystem.DB.DatabaseHandler;
public class AddController {
@FXML
private TextField userIdField; // User ID field
@FXML
private PasswordField passwordField; // Password field
private DatabaseHandler dbHandler; // Database handler instance
public AddController() {
dbHandler = new DatabaseHandler(); // Initialize the DatabaseHandler
}
// Handle Add User action
@FXML
private void handleAddUserAction(ActionEvent event) {
String userId = userIdField.getText().trim();
String password = passwordField.getText().trim();
// Validate inputs
if (userId.isEmpty() || password.isEmpty()) {
showAlert("Error", "Please enter both user ID and password.");
return;
}
// Create default email based on user ID
String email = "user" + userId + "@example.com";
// Try to add the user to the database
boolean success = addUserToDatabase(userId, password, email);
// Show the result of the operation
if (success) {
showAlert("Success", "User " + userId + " added successfully.");
userIdField.clear();
passwordField.clear();
} else {
showAlert("Error", "Failed to add user. Please try again.");
}
}
// Add user to database using DatabaseHandler
private boolean addUserToDatabase(String userId, String password, String email) {
try {
// Call the addJobSeeker method from DatabaseHandler to add the user
int userIdInt = Integer.parseInt(userId); // Ensure userId is an integer
return dbHandler.addJobSeeker(userIdInt, password, email); // Call the method to add the user
} catch (NumberFormatException e) {
// Handle invalid user ID format (must be an integer)
showAlert("Error", "User ID must be a valid integer.");
return false;
}
}
// Show an alert message
private void showAlert(String title, String message) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.showAndWait();
}
// Handle the back button action to go back to the previous screen
@FXML
private void handleGoBackButton(ActionEvent event) {
JobInterviewPreparationSystem.showManageScreen(); // Adjust as per your application structure
}
}