Saturday, May 16, 2015

Exam registration system on Java MVC pattern

I recently played around the mvc pattern in Java 8. I have implemented a small exam registration system having two views : student and teacher views. It detects duplicate registration and check that the student ID is in the form xxxxyyyy where x = letter and y= digit.
Here is a package diagram with calculated instability.

Here are some views:
CONTROL PANEL:

STUDENT:

TEACHER:

Source Code:
Project structure:


Registration


        package registration;

import controller.Controller;
import view.CPanelUI;

public class Registration {
    public static void main(String[] args) {
       
       Registration registration = new Registration(); 
        
    }
    //Main Frame call
    public Registration(){
       new CPanelUI( new Controller().getListOfIds()).setVisible(true);
    }
}

  

Exam


       package model;

import java.util.ArrayList;

public class Exam {
    
    //Students list declaration
   private  ArrayList  students = new ArrayList(); 
  
   // Adding an id to the list
   public void addStudent(String id){
       students.add(id);
   }
   
   //Returning the list of students IDs
   public ArrayList getStudents(){
       return students;
   }
   
   //Setting the list of students IDs
   public void setStudents(ArrayList  students){
       this.students=students;
   }
   
}

  
Controller

      
package controller;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import model.Exam;

public class Controller {
    Exam exam = new Exam();
     
    
    public Controller(){
             
    }
    
    public void setList(ArrayList list){
        exam.setStudents(list);
    }
    public ArrayList getListOfIds(){
        return exam.getStudents();
    }
    
    public void register(String id){
        
       //Checking ID format
           if(checkIdFormat(id)==false){
             JOptionPane.showMessageDialog(null, "INVALID ID FORMAT");
           }
           else{
              
               //Checking ID Duplication
              if(checkIdDuplicate(getListOfIds(),id)==true){
                JOptionPane.showMessageDialog(null, "ID DUPLICATED");
              }
              else{
                  //Adding new id to the list
                   exam.addStudent(id);
                   JOptionPane.showMessageDialog(null, id+ " successfully registered for the exam !");
               }
                
           }         

    }
    
    
    public boolean checkIdFormat(String id){
     //Check if the ID contains 4 letters followed by 4 digits
       if(id.matches("^[a-zA-Z]{4}[0-9]{4}$")) {
           return true;
        } else {
           return false;
        }
    }
    
    
    public boolean checkIdDuplicate(ArrayList list, String id){
        // check whether the id exists in the list or not
        if(list.indexOf(id)==-1){
            return false;
        }else{
            return true;
        }
    }
}

  


CPanelUI

      
   package view;

import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CPanelUI extends JFrame{
    private final JLabel lblTitle = new JLabel("Teacher UI");
    private final JButton btnStudent = new JButton("Student");
    private final JButton btnTeacher = new JButton("Teacher");
       
    public CPanelUI (ArrayList list){
        setSize(300,300);
        setLayout(new FlowLayout());
        setTitle("Control Panel UI");
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(lblTitle);
        panel.add(btnStudent);
        panel.add(btnTeacher);
        add(panel);
 
        btnStudent.addActionListener(
          ev->{
           dispose();
           new StudentUI(list).setVisible(true);
          }
        );
        
        btnTeacher.addActionListener(
         ev -> {
          dispose();
          new TeacherUI(list).setVisible(true);
         
         }
        );
         //uses the default look and feel specified by windows themes
        setDefaultLookAndFeelDecorated(true);
        //centered the window position
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
    }
    
}

  

StudentUI

      
   package view;

import controller.Controller;
import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class StudentUI extends JFrame {
    private final JLabel lblId = new JLabel("Enter your ID (4 letters and 4 digits): ");
    private JTextField txtId = new JTextField(10);
    private final JButton btnId= new JButton("Submit ID");
    Controller controller = new Controller();
    public StudentUI (ArrayList list){
        //setVisible(true);
        setSize(300,300);
        setLayout(new FlowLayout());
        setTitle("Student UI");
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(lblId);
        panel.add(txtId);
        panel.add(btnId);
        add(panel);
        btnId.addActionListener(
         ev -> {
           String id = txtId.getText();
           controller.setList(list);
           controller.register(id);
           dispose();
           new CPanelUI(list).setVisible(true);
           }
        );
        //uses the default look and feel specified by windows themes
        setDefaultLookAndFeelDecorated(true);
        //centered the window position
        setLocationRelativeTo(null);
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
}
  


TeacherUI

   
   package view;

import java.awt.FlowLayout;
import java.util.ArrayList;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class TeacherUI extends JFrame{
    private final JLabel lblTitle = new JLabel("Teacher UI");
    private final JButton btnBack = new JButton("Back");
    private final JList listIds ;
    //passing model to UI
    public TeacherUI(ArrayList list){
        listIds = new JList(list.toArray());
        setSize(300,300);
        setLayout(new FlowLayout());
        setTitle("Student UI");
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.add(lblTitle);
        panel.add(new JScrollPane(listIds));
        panel.add(btnBack);
        add(panel);
        listIds.setFixedCellWidth(300);
         btnBack.addActionListener(
         ev -> {
             setVisible(false);
             new CPanelUI(list).setVisible(true);
           }
        );
         //uses the default look and feel specified by windows themes
        setDefaultLookAndFeelDecorated(true);
        //centered the window position
        setLocationRelativeTo(null);
    } 
}
  


Althought this system is working properly and following all the stable dependencies principles, we to add an observer pattern between views and the model to make it more scalable. And that is the next thing I will add to this system. Stay tuned. Feel free to drop your comments...

Note: This is project built on Java 8

No comments:

Post a Comment