Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.19 KB | None | 0 0
  1.     private static ArrayList<Student> registeredUsers = new ArrayList<Student>(); // ArrayList to store Student objects
  2.     public static StudentManager sm;    
  3.  
  4.     public Register() {
  5.     initComponents();
  6.     Initialization();
  7.     }
  8.  
  9.     static void Initialization(){
  10.    
  11.     sm = new StudentManager();
  12.    
  13.     try {
  14.         // Reads the objects from the .txt file
  15.         registeredUsers = sm.load();
  16.                } catch(Exception ex) {
  17.                ex.printStackTrace();
  18.                }
  19.     }
  20.  
  21. // Registering a new Student to the system
  22.     private void RegisterNewButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  23.     boolean valid_studentID = false;
  24.     boolean valid_firstName = false;
  25.     boolean valid_lastName = false;
  26.     boolean valid_contactNo = false;
  27.     boolean valid_email = false;
  28.     boolean valid_carNo = false;
  29.    
  30.        // Test if 'StudentID' field is empty
  31.        if(StudentIDBox.getText().isEmpty()){
  32.        JFrame frame = new JFrame("");
  33.        JOptionPane.showMessageDialog(frame, "StudentID field is empty.", "TODO", JOptionPane.OK_OPTION);
  34.        
  35.        // Test if studentID already exists
  36.        }else if(!StudentIDBox.getText().isEmpty()){
  37.            String testStudentID = StudentIDBox.getText();
  38.           // Search  existing Students for a possible match
  39.            if(sm.searchStudents(registeredUsers, testStudentID) == true){
  40.            JFrame frame = new JFrame("");
  41.            JOptionPane.showMessageDialog(frame, "StudentID already exists.", "TODO", JOptionPane.OK_OPTION);
  42.            }
  43.            // If StudentID is valid
  44.            if(sm.searchStudents(registeredUsers, testStudentID) == false){
  45.            studentID = StudentIDBox.getText();
  46.            valid_studentID = true;
  47.            }         
  48.            
  49.        // Continue and check if 'First name' is valid
  50.        if(valid_studentID){
  51.            
  52.            // Test if 'First name' field is empty
  53.            if(FirstNameBox.getText().isEmpty()){
  54.            JFrame frame = new JFrame("");
  55.            JOptionPane.showMessageDialog(frame, "First name field is empty.", "TODO", JOptionPane.OK_OPTION);
  56.            // If not empty, assign to a variable
  57.            }else if(!FirstNameBox.getText().isEmpty()){
  58.            firstName = FirstNameBox.getText();
  59.            valid_firstName = true;
  60.            }
  61.        }
  62.        
  63.        // Continue and check if 'Last name' is valid
  64.        if(valid_firstName){
  65.            
  66.            // Test if 'Last name' field is empty
  67.            if(LastNameBox.getText().isEmpty()){
  68.            JFrame frame = new JFrame("");
  69.            JOptionPane.showMessageDialog(frame, "Last name field is empty.", "TODO", JOptionPane.OK_OPTION);
  70.            }else if(!LastNameBox.getText().isEmpty()){
  71.            lastName = LastNameBox.getText();
  72.            valid_lastName = true;
  73.            }
  74.        }
  75.        
  76.        // Continue and check if 'Contact number' is valid
  77.        if(valid_lastName){
  78.            
  79.            if(ContactNoBox.getText().isEmpty()){
  80.            JFrame frame = new JFrame("");
  81.            JOptionPane.showMessageDialog(frame, "Contact number field is empty.", "TODO", JOptionPane.OK_OPTION);
  82.            }else if(!ContactNoBox.getText().isEmpty()){
  83.            if(ContactNoBox.getText().matches("[0-9]+")){
  84.            // Save contact number and convert it to Integer
  85.            contactNumber = ContactNoBox.getText();
  86.            valid_contactNo = true;
  87.            }else{
  88.                JFrame frame = new JFrame("");
  89.                JOptionPane.showMessageDialog(frame, "Contact number field contains invalid characters.", "TODO", JOptionPane.OK_OPTION);
  90.            }
  91.            }
  92.        }
  93.        
  94.        // Continue and check if 'Email address' is valid
  95.        if(valid_contactNo){
  96.            if(EmailBox.getText().isEmpty()){
  97.            JFrame frame = new JFrame("");
  98.            JOptionPane.showMessageDialog(frame, "Email address field is empty.", "TODO", JOptionPane.OK_OPTION);
  99.            }else if(!EmailBox.getText().isEmpty()){
  100.            // Save Email address
  101.            emailAddress = EmailBox.getText();
  102.            valid_email = true;
  103.            }
  104.        }
  105.        
  106.        // Continue and check if 'Car Number' is valid
  107.        if(valid_email){
  108.            if(CarNoBox.getText().isEmpty()){
  109.            JFrame frame = new JFrame("");
  110.            JOptionPane.showMessageDialog(frame, "Car number field is empty.", "TODO", JOptionPane.OK_OPTION);
  111.            }else if(!CarNoBox.getText().isEmpty()){
  112.            // Save Car number
  113.            carNumber = CarNoBox.getText();
  114.            valid_carNo = true;
  115.            }
  116.        }
  117.        
  118.        // If all the information is OK, register user to the system
  119.        if (valid_studentID && valid_firstName && valid_lastName && valid_contactNo && valid_email && valid_carNo){
  120.         currentDate = new SimpleDateFormat("dd-MM-yyyy HH:mm").format(new Date());
  121.         System.out.println(currentDate);
  122.         registeredUsers.add(new Student(studentID, firstName, lastName, contactNumber, emailAddress, carNumber, currentDate));
  123.         JFrame frame = new JFrame("");
  124.         JOptionPane.showMessageDialog(frame, "StudentID: "+studentID+" was registered to the system.", "Registration successful.", JOptionPane.INFORMATION_MESSAGE);
  125.         System.out.println("Debug log: New student registered");
  126.         System.out.println("Debug log: Number of registered students = "+registeredUsers.size());
  127.           try {
  128.               // Save the user to the system
  129.                sm.save(registeredUsers);
  130.                } catch(Exception ex) {
  131.                ex.printStackTrace();
  132.                }
  133.              }
  134.     }
  135.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement