Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static ArrayList<Student> registeredUsers = new ArrayList<Student>(); // ArrayList to store Student objects
- public static StudentManager sm;
- public Register() {
- initComponents();
- Initialization();
- }
- static void Initialization(){
- sm = new StudentManager();
- try {
- // Reads the objects from the .txt file
- registeredUsers = sm.load();
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
- // Registering a new Student to the system
- private void RegisterNewButtonActionPerformed(java.awt.event.ActionEvent evt) {
- boolean valid_studentID = false;
- boolean valid_firstName = false;
- boolean valid_lastName = false;
- boolean valid_contactNo = false;
- boolean valid_email = false;
- boolean valid_carNo = false;
- // Test if 'StudentID' field is empty
- if(StudentIDBox.getText().isEmpty()){
- JFrame frame = new JFrame("");
- JOptionPane.showMessageDialog(frame, "StudentID field is empty.", "TODO", JOptionPane.OK_OPTION);
- // Test if studentID already exists
- }else if(!StudentIDBox.getText().isEmpty()){
- String testStudentID = StudentIDBox.getText();
- // Search existing Students for a possible match
- if(sm.searchStudents(registeredUsers, testStudentID) == true){
- JFrame frame = new JFrame("");
- JOptionPane.showMessageDialog(frame, "StudentID already exists.", "TODO", JOptionPane.OK_OPTION);
- }
- // If StudentID is valid
- if(sm.searchStudents(registeredUsers, testStudentID) == false){
- studentID = StudentIDBox.getText();
- valid_studentID = true;
- }
- // Continue and check if 'First name' is valid
- if(valid_studentID){
- // Test if 'First name' field is empty
- if(FirstNameBox.getText().isEmpty()){
- JFrame frame = new JFrame("");
- JOptionPane.showMessageDialog(frame, "First name field is empty.", "TODO", JOptionPane.OK_OPTION);
- // If not empty, assign to a variable
- }else if(!FirstNameBox.getText().isEmpty()){
- firstName = FirstNameBox.getText();
- valid_firstName = true;
- }
- }
- // Continue and check if 'Last name' is valid
- if(valid_firstName){
- // Test if 'Last name' field is empty
- if(LastNameBox.getText().isEmpty()){
- JFrame frame = new JFrame("");
- JOptionPane.showMessageDialog(frame, "Last name field is empty.", "TODO", JOptionPane.OK_OPTION);
- }else if(!LastNameBox.getText().isEmpty()){
- lastName = LastNameBox.getText();
- valid_lastName = true;
- }
- }
- // Continue and check if 'Contact number' is valid
- if(valid_lastName){
- if(ContactNoBox.getText().isEmpty()){
- JFrame frame = new JFrame("");
- JOptionPane.showMessageDialog(frame, "Contact number field is empty.", "TODO", JOptionPane.OK_OPTION);
- }else if(!ContactNoBox.getText().isEmpty()){
- if(ContactNoBox.getText().matches("[0-9]+")){
- // Save contact number and convert it to Integer
- contactNumber = ContactNoBox.getText();
- valid_contactNo = true;
- }else{
- JFrame frame = new JFrame("");
- JOptionPane.showMessageDialog(frame, "Contact number field contains invalid characters.", "TODO", JOptionPane.OK_OPTION);
- }
- }
- }
- // Continue and check if 'Email address' is valid
- if(valid_contactNo){
- if(EmailBox.getText().isEmpty()){
- JFrame frame = new JFrame("");
- JOptionPane.showMessageDialog(frame, "Email address field is empty.", "TODO", JOptionPane.OK_OPTION);
- }else if(!EmailBox.getText().isEmpty()){
- // Save Email address
- emailAddress = EmailBox.getText();
- valid_email = true;
- }
- }
- // Continue and check if 'Car Number' is valid
- if(valid_email){
- if(CarNoBox.getText().isEmpty()){
- JFrame frame = new JFrame("");
- JOptionPane.showMessageDialog(frame, "Car number field is empty.", "TODO", JOptionPane.OK_OPTION);
- }else if(!CarNoBox.getText().isEmpty()){
- // Save Car number
- carNumber = CarNoBox.getText();
- valid_carNo = true;
- }
- }
- // If all the information is OK, register user to the system
- if (valid_studentID && valid_firstName && valid_lastName && valid_contactNo && valid_email && valid_carNo){
- currentDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(new Date());
- System.out.println(currentDate);
- registeredUsers.add(new Student(studentID, firstName, lastName, contactNumber, emailAddress, carNumber, currentDate));
- JFrame frame = new JFrame("");
- JOptionPane.showMessageDialog(frame, "StudentID: "+studentID+" was registered to the system.", "Registration successful.", JOptionPane.INFORMATION_MESSAGE);
- System.out.println("Debug log: New student registered");
- System.out.println("Debug log: Number of registered students = "+registeredUsers.size());
- try {
- // Save the user to the system
- sm.save(registeredUsers);
- } catch(Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement