├── .gitignore ├── .vscode └── settings.json ├── src ├── resources │ ├── add.png │ ├── edit.png │ ├── logo.png │ ├── assign.png │ ├── courses.png │ ├── remove.png │ ├── student.png │ ├── teacher.png │ ├── settings.png │ ├── login_image.png │ ├── logo_round.png │ ├── search_icon.png │ ├── signup_image.png │ ├── password_eye_hide.png │ └── password_eye_show.png ├── models │ ├── user │ │ ├── Admin.java │ │ ├── SystemUser.java │ │ ├── Teacher.java │ │ └── Student.java │ └── course │ │ ├── Module.java │ │ └── Course.java ├── exceptions │ ├── InvalidEmailException.java │ └── InvalidPasswordException.java ├── util │ ├── CustomImage.java │ ├── Validator.java │ ├── CellRenderer.java │ ├── DatabaseManager.java │ ├── DataRetriever.java │ └── DataManager.java ├── pages │ ├── SplashScreen.java │ ├── Login.java │ ├── Courses.java │ ├── SignUp.java │ ├── Students.java │ ├── Teachers.java │ └── Dashboard.java ├── App.java └── auth │ └── Auth.java ├── External_JARS └── mysql-connector-j-8.0.32.jar ├── .classpath ├── .project ├── README.md └── UML_Diagram.ucls /.gitignore: -------------------------------------------------------------------------------- 1 | *class 2 | .settings 3 | bin/ 4 | gen/ 5 | out/ 6 | .DS_Store -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "java.debug.settings.onBuildFailureProceed": true 3 | } 4 | -------------------------------------------------------------------------------- /src/resources/add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/add.png -------------------------------------------------------------------------------- /src/resources/edit.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/edit.png -------------------------------------------------------------------------------- /src/resources/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/logo.png -------------------------------------------------------------------------------- /src/resources/assign.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/assign.png -------------------------------------------------------------------------------- /src/resources/courses.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/courses.png -------------------------------------------------------------------------------- /src/resources/remove.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/remove.png -------------------------------------------------------------------------------- /src/resources/student.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/student.png -------------------------------------------------------------------------------- /src/resources/teacher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/teacher.png -------------------------------------------------------------------------------- /src/resources/settings.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/settings.png -------------------------------------------------------------------------------- /src/resources/login_image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/login_image.png -------------------------------------------------------------------------------- /src/resources/logo_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/logo_round.png -------------------------------------------------------------------------------- /src/resources/search_icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/search_icon.png -------------------------------------------------------------------------------- /src/resources/signup_image.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/signup_image.png -------------------------------------------------------------------------------- /src/resources/password_eye_hide.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/password_eye_hide.png -------------------------------------------------------------------------------- /src/resources/password_eye_show.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/src/resources/password_eye_show.png -------------------------------------------------------------------------------- /External_JARS/mysql-connector-j-8.0.32.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/R0shish/Course-Management-System/HEAD/External_JARS/mysql-connector-j-8.0.32.jar -------------------------------------------------------------------------------- /src/models/user/Admin.java: -------------------------------------------------------------------------------- 1 | package models.user; 2 | 3 | public class Admin extends SystemUser { 4 | 5 | public Admin(String name) { 6 | super(name, "Admin"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/exceptions/InvalidEmailException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | public class InvalidEmailException extends Exception { 4 | 5 | private static final long serialVersionUID = -8145316108548232390L; 6 | 7 | public InvalidEmailException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/exceptions/InvalidPasswordException.java: -------------------------------------------------------------------------------- 1 | package exceptions; 2 | 3 | public class InvalidPasswordException extends Exception { 4 | 5 | private static final long serialVersionUID = 3761449227809409477L; 6 | 7 | public InvalidPasswordException(String message) { 8 | super(message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/models/user/SystemUser.java: -------------------------------------------------------------------------------- 1 | package models.user; 2 | 3 | public class SystemUser { 4 | private String name; 5 | private String role; 6 | 7 | public SystemUser(String name, String role) { 8 | this.name = name; 9 | this.role = role; 10 | } 11 | 12 | public String getName() { 13 | return name; 14 | } 15 | 16 | public String getRole() { 17 | return role; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return name; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/util/CustomImage.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import javax.swing.ImageIcon; 4 | import javax.swing.JPanel; 5 | 6 | public class CustomImage extends JPanel { 7 | 8 | private static final long serialVersionUID = 4985367388576734702L; 9 | private ImageIcon image; 10 | 11 | public CustomImage(String path) { 12 | ImageIcon image = new ImageIcon(getClass().getResource(path)); 13 | this.image = image; 14 | } 15 | 16 | public ImageIcon getImage(int width, int height) { 17 | return new ImageIcon(image.getImage().getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH)); 18 | } 19 | } -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/models/course/Module.java: -------------------------------------------------------------------------------- 1 | package models.course; 2 | 3 | public class Module { 4 | private int id; 5 | private String name; 6 | private String type; 7 | 8 | public Module(int id, String name, String type) { 9 | this.id = id; 10 | this.name = name; 11 | this.type = type; 12 | } 13 | 14 | public int getId() { 15 | return id; 16 | } 17 | 18 | public String getName() { 19 | return name; 20 | } 21 | 22 | public String getType() { 23 | return type; 24 | } 25 | 26 | public static Module fromSql(int id, String name, String type) { 27 | return new Module(id, name, type); 28 | } 29 | 30 | @Override 31 | public String toString() { 32 | return name; 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | course_management_system 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | 19 | 1674297827780 20 | 21 | 30 22 | 23 | org.eclipse.core.resources.regexFilterMatcher 24 | node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/models/course/Course.java: -------------------------------------------------------------------------------- 1 | package models.course; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class Course { 6 | private int id; 7 | private String name; 8 | private ArrayList modules; 9 | 10 | public Course(int id, String name) { 11 | this.id = id; 12 | this.name = name; 13 | } 14 | 15 | public int getId() { 16 | return id; 17 | } 18 | 19 | public String getName() { 20 | return name; 21 | } 22 | 23 | public ArrayList getModules() { 24 | return modules; 25 | } 26 | 27 | public String getModulesString() { 28 | String modulesString = ""; 29 | for (Module module : modules) { 30 | modulesString += module.getName() + ", "; 31 | } 32 | return modulesString.substring(0, modulesString.length() - 2); 33 | } 34 | 35 | public void setModules(ArrayList modules) { 36 | this.modules = modules; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return name; 42 | } 43 | 44 | public static Course fromSql(int id, String name) { 45 | return new Course(id, name); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Course Management System 2 | A simple Course Management System implemented using Java Swing and MYSQL database made for the final coursework of 5CS019 - Object-Oriented Design and Programming. 3 | 4 | ## Tools and Utilities 5 | - Java 6 | - Swing (UI) 7 | - MYSQL (Database) 8 | 9 | ## Screenshots 10 | 11 | #### Splash Screen 12 | SplashScreen 13 | 14 | #### Login Page 15 | LoginPage 16 | 17 | #### Sign Up Page 18 | SignUpPage 19 | 20 | 21 | ## Contributing 22 | If you're interested in contributing to this project and making it better with new ideas, your pull request will be greatly appreciated. 23 | 24 | If you come across any problems, please let us know by posting in the issues section of the repository. 25 | 26 | Also, if you like the project, don't forget to give it a star. Thank you for your support. 27 | -------------------------------------------------------------------------------- /src/util/Validator.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import java.util.regex.Matcher; 4 | import java.util.regex.Pattern; 5 | 6 | import javax.swing.JLabel; 7 | 8 | public class Validator { 9 | 10 | public static boolean validate(String text, JLabel label, String field) { 11 | switch (field) { 12 | case "Email": 13 | String regex = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$"; 14 | Pattern pattern = Pattern.compile(regex); 15 | Matcher matcher = pattern.matcher(text); 16 | if (!matcher.matches()) { 17 | label.setText("Invalid " + field.toLowerCase() + "!"); 18 | return false; 19 | } 20 | label.setText(""); 21 | return true; 22 | case "Password": 23 | if (text.length() < 8) { 24 | label.setText(field + " must be atleast 8 characters long!"); 25 | return false; 26 | } 27 | if (!text.matches(".*\\d+.*")) { 28 | label.setText(field + " must have atleast one number!"); 29 | return false; 30 | } 31 | label.setText(""); 32 | return true; 33 | 34 | default: 35 | if (text.length() < 3) { 36 | label.setText("Invalid " + field.toLowerCase() + "!"); 37 | return false; 38 | } 39 | label.setText(""); 40 | return true; 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/models/user/Teacher.java: -------------------------------------------------------------------------------- 1 | package models.user; 2 | 3 | import java.util.ArrayList; 4 | import models.course.Module; 5 | 6 | public class Teacher extends SystemUser { 7 | private int id; 8 | private String name; 9 | private String phone; 10 | private String email; 11 | private ArrayList modules; 12 | 13 | public Teacher(int id, String name, String phone, String email, ArrayList modules) { 14 | super(name, "Teacher"); 15 | this.id = id; 16 | this.name = name; 17 | this.phone = phone; 18 | this.email = email; 19 | this.modules = modules; 20 | } 21 | 22 | public Teacher(String name) { 23 | super(name, "Teacher"); 24 | } 25 | 26 | public static Teacher fromSql(int id, String name, String phone, String email, ArrayList modules) { 27 | return new Teacher(id, name, phone, email, modules); 28 | } 29 | 30 | public ArrayList getModules() { 31 | return modules; 32 | } 33 | 34 | public String getModuleString() { 35 | String moduleString = ""; 36 | for (Module module : modules) { 37 | moduleString += module.getName() + ", "; 38 | } 39 | if (moduleString.length() < 3) { 40 | return "No modules"; 41 | } 42 | return moduleString.substring(0, moduleString.length() - 2); 43 | } 44 | 45 | public int getId() { 46 | return id; 47 | } 48 | 49 | public String getName() { 50 | return name; 51 | } 52 | 53 | public String getPhone() { 54 | return phone; 55 | } 56 | 57 | public String getEmail() { 58 | return email; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/pages/SplashScreen.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import java.awt.Color; 4 | import java.awt.event.ActionEvent; 5 | import java.awt.event.ActionListener; 6 | 7 | import javax.swing.JFrame; 8 | import javax.swing.JLabel; 9 | import javax.swing.JPanel; 10 | import javax.swing.JProgressBar; 11 | import javax.swing.Timer; 12 | 13 | import util.CustomImage; 14 | 15 | public class SplashScreen extends JPanel { 16 | 17 | private static final long serialVersionUID = -3421670490444154816L; 18 | private JProgressBar progressBar; 19 | private int currentProgress; 20 | private Timer timer; 21 | 22 | public SplashScreen(JFrame frame, CustomImage logo) { 23 | JPanel splashFrame = new JPanel(); 24 | splashFrame.setBackground(new Color(255, 255, 255)); 25 | splashFrame.setBounds(0, -7, 1480, 701); 26 | frame.getContentPane().add(splashFrame); 27 | splashFrame.setVisible(true); 28 | splashFrame.setLayout(null); 29 | 30 | JLabel logoImg = new JLabel(logo.getImage(100, 100)); 31 | logoImg.setBounds(690, 259, 100, 100); 32 | splashFrame.add(logoImg); 33 | 34 | progressBar = new JProgressBar(); 35 | progressBar.setBounds(690, 400, 100, 100); 36 | progressBar.setMaximum(100); 37 | splashFrame.add(progressBar); 38 | 39 | currentProgress = 0; 40 | 41 | timer = new Timer(3, new ActionListener() { 42 | public void actionPerformed(ActionEvent e) { 43 | currentProgress++; 44 | progressBar.setValue((int) ((currentProgress / (float) progressBar.getMaximum()) * 100)); 45 | if (currentProgress == 100) { 46 | timer.stop(); 47 | new SignUp(frame, logo); 48 | splashFrame.setVisible(false); 49 | } 50 | } 51 | }); 52 | timer.start(); 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/App.java: -------------------------------------------------------------------------------- 1 | import java.awt.EventQueue; 2 | import java.sql.SQLException; 3 | 4 | import javax.swing.JFrame; 5 | import javax.swing.JOptionPane; 6 | 7 | import auth.Auth; 8 | import pages.SplashScreen; 9 | import util.CustomImage; 10 | import util.DataManager; 11 | import util.DataRetriever; 12 | import util.DatabaseManager; 13 | 14 | public class App { 15 | 16 | private JFrame frmHeraldCourseManagement; 17 | 18 | public static void main(String[] args) { 19 | EventQueue.invokeLater(new Runnable() { 20 | public void run() { 21 | try { 22 | App window = new App(); 23 | window.frmHeraldCourseManagement.setVisible(true); 24 | } catch (Exception e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | }); 29 | } 30 | 31 | public App() { 32 | initialize(); 33 | } 34 | 35 | private void initialize() { 36 | frmHeraldCourseManagement = new JFrame(); 37 | frmHeraldCourseManagement.setTitle("Herald Course Management System"); 38 | frmHeraldCourseManagement.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 39 | frmHeraldCourseManagement.getContentPane().setLayout(null); 40 | frmHeraldCourseManagement.setSize(1480, 720); 41 | frmHeraldCourseManagement.setResizable(false); 42 | 43 | CustomImage logo = new CustomImage("../resources/logo.png"); 44 | 45 | new SplashScreen(frmHeraldCourseManagement, logo); 46 | 47 | try { 48 | DatabaseManager db = DatabaseManager.getInstance(); 49 | new Auth(db); 50 | new DataRetriever(); 51 | new DataManager(); 52 | 53 | } catch (SQLException e) { 54 | System.out.println(e.getMessage()); 55 | JOptionPane.showMessageDialog(null, 56 | "Can not connect to database!\nPlease make sure mySQL is correctly setup and running!", 57 | "Error 500: Server Communication Failed", JOptionPane.ERROR_MESSAGE); 58 | System.exit(500); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/models/user/Student.java: -------------------------------------------------------------------------------- 1 | package models.user; 2 | 3 | import models.course.Course; 4 | 5 | public class Student extends SystemUser { 6 | private int id; 7 | private String name; 8 | private String phone; 9 | private String email; 10 | private int level; 11 | private Course enrolledCourse; 12 | 13 | public Student(int id, String name, String phone, String email, int level, Course enrolledCourse) { 14 | super(name, "Student"); 15 | this.id = id; 16 | this.name = name; 17 | this.phone = phone; 18 | this.email = email; 19 | this.level = level; 20 | this.enrolledCourse = enrolledCourse; 21 | } 22 | 23 | public Student(int id, String name) { 24 | super(name, "Student"); 25 | this.id = id; 26 | this.name = name; 27 | } 28 | 29 | public int getId() { 30 | return id; 31 | } 32 | 33 | public String getName() { 34 | return name; 35 | } 36 | 37 | public String getPhone() { 38 | return phone; 39 | } 40 | 41 | public String getEmail() { 42 | return email; 43 | } 44 | 45 | public int getLevel() { 46 | return level; 47 | } 48 | 49 | public Course getEnrolledCourse() { 50 | return enrolledCourse; 51 | } 52 | 53 | public static Student fromSql(int id, String name, String phone, String email, int level, Course enrolledCourse) { 54 | return new Student(id, name, phone, email, level, enrolledCourse); 55 | } 56 | 57 | public void setId(int id) { 58 | this.id = id; 59 | } 60 | 61 | public void setName(String name) { 62 | this.name = name; 63 | } 64 | 65 | public void setPhone(String phone) { 66 | this.phone = phone; 67 | } 68 | 69 | public void setEmail(String email) { 70 | this.email = email; 71 | } 72 | 73 | public void setLevel(int level) { 74 | this.level = level; 75 | } 76 | 77 | public void setEnrolledCourse(Course enrolledCourse) { 78 | this.enrolledCourse = enrolledCourse; 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/util/CellRenderer.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import java.awt.Component; 4 | import java.awt.Font; 5 | 6 | import javax.swing.BoxLayout; 7 | import javax.swing.JLabel; 8 | import javax.swing.JList; 9 | import javax.swing.JPanel; 10 | import javax.swing.ListCellRenderer; 11 | import javax.swing.border.EmptyBorder; 12 | 13 | import models.course.Course; 14 | import models.course.Module; 15 | import models.user.SystemUser; 16 | import models.user.Teacher; 17 | 18 | public class CellRenderer extends JPanel implements ListCellRenderer { 19 | private static final long serialVersionUID = 6422050714652017814L; 20 | private JLabel titleLabel; 21 | private JLabel subtitleLabel; 22 | 23 | public CellRenderer() { 24 | setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 25 | setBorder(new EmptyBorder(10, 10, 10, 10)); 26 | titleLabel = new JLabel(); 27 | subtitleLabel = new JLabel(); 28 | add(titleLabel); 29 | add(subtitleLabel); 30 | } 31 | 32 | @Override 33 | public Component getListCellRendererComponent(JList list, Object value, int index, 34 | boolean isSelected, boolean cellHasFocus) { 35 | if (value instanceof SystemUser) { 36 | SystemUser user = (SystemUser) value; 37 | titleLabel.setText(user.getName()); 38 | removeAll(); 39 | add(titleLabel); 40 | if (user instanceof Teacher) { 41 | Teacher teacher = (Teacher) user; 42 | for (Module module : teacher.getModules()) { 43 | JLabel moduleLabel = new JLabel(" " + module.getName()); 44 | moduleLabel.setFont(new Font("Futura", Font.ITALIC, 10)); 45 | add(moduleLabel); 46 | } 47 | } 48 | } else if (value instanceof Course) { 49 | Course course = (Course) value; 50 | titleLabel.setText("📌 " + course.getName()); 51 | removeAll(); 52 | add(titleLabel); 53 | for (Module module : course.getModules()) { 54 | JLabel moduleLabel = new JLabel(" " + module.getName()); 55 | moduleLabel.setFont(new Font("Futura", Font.ITALIC, 10)); 56 | add(moduleLabel); 57 | } 58 | } 59 | titleLabel.setFont(new Font("Futura", Font.BOLD, 15)); 60 | subtitleLabel.setFont(new Font("Futura", Font.ITALIC, 10)); 61 | setBackground(list.getBackground()); 62 | setForeground(list.getForeground()); 63 | return this; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/auth/Auth.java: -------------------------------------------------------------------------------- 1 | package auth; 2 | 3 | import java.sql.PreparedStatement; 4 | import java.sql.ResultSet; 5 | import java.sql.SQLException; 6 | 7 | import exceptions.InvalidEmailException; 8 | import exceptions.InvalidPasswordException; 9 | import models.user.Admin; 10 | import models.user.Student; 11 | import models.user.SystemUser; 12 | import models.user.Teacher; 13 | import util.DataRetriever; 14 | 15 | public class Auth { 16 | private static PreparedStatement checkEmailStmt; 17 | private static PreparedStatement retrieveRoleStmt; 18 | private static PreparedStatement checkEmailExistenceStmt; 19 | private static PreparedStatement addCredentialStmt; 20 | 21 | public Auth(util.DatabaseManager db) throws SQLException { 22 | checkEmailStmt = db.getConnection().prepareStatement("SELECT count(*) FROM auth WHERE email=?"); 23 | retrieveRoleStmt = db.getConnection() 24 | .prepareStatement("SELECT id, name,role FROM auth WHERE email=? AND BINARY password=?"); 25 | checkEmailExistenceStmt = db.getConnection().prepareStatement("SELECT email FROM auth WHERE email=?"); 26 | addCredentialStmt = db.getConnection() 27 | .prepareStatement("INSERT INTO auth (name, email, password, role) VALUES (?,?,?, 'Student')"); 28 | } 29 | 30 | public static SystemUser returnSystemUser(String email, String password) throws Exception { 31 | try { 32 | checkEmailStmt.setString(1, email); 33 | ResultSet rs = checkEmailStmt.executeQuery(); 34 | int count = 0; 35 | if (rs.next()) 36 | count = rs.getInt(1); 37 | if (count == 0) 38 | throw new InvalidEmailException("No user with this email found!"); 39 | else { 40 | retrieveRoleStmt.setString(1, email); 41 | retrieveRoleStmt.setString(2, password); 42 | rs = retrieveRoleStmt.executeQuery(); 43 | if (rs.next()) { 44 | switch (rs.getString("role")) { 45 | case "Student": 46 | Student student = DataRetriever.getStudents(rs.getInt("id")); 47 | if (student == null) 48 | return new Student(rs.getInt("id"), rs.getString("name")); 49 | return student; 50 | case "Teacher": 51 | return new Teacher(rs.getString("name")); 52 | case "Admin": 53 | return new Admin(rs.getString("name")); 54 | default: 55 | throw new Exception("An error occurred while retrieving role!"); 56 | } 57 | } else 58 | throw new InvalidPasswordException("Password not valid!"); 59 | } 60 | } catch (SQLException e) { 61 | throw new Exception("An error occurred while checking the email and password!"); 62 | } 63 | } 64 | 65 | public static void addCredential(String name, String email, String password) throws Exception { 66 | try { 67 | checkEmailExistenceStmt.setString(1, email); 68 | ResultSet rs = checkEmailExistenceStmt.executeQuery(); 69 | if (rs.next()) { 70 | throw new InvalidEmailException("Email already in use!"); 71 | } else { 72 | addCredentialStmt.setString(1, name); 73 | addCredentialStmt.setString(2, email); 74 | addCredentialStmt.setString(3, password); 75 | addCredentialStmt.executeUpdate(); 76 | } 77 | } catch (SQLException e) { 78 | e.printStackTrace(); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/util/DatabaseManager.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.SQLException; 7 | 8 | public class DatabaseManager { 9 | public static DatabaseManager instance; 10 | private Connection conn; 11 | 12 | private DatabaseManager(String path, String username, String password) throws SQLException { 13 | this.conn = DriverManager.getConnection(path, username, password); 14 | System.out.println("Connected to database"); 15 | createDatabase("course_management_system"); 16 | String query = "USE course_management_system"; 17 | executeUpdate(query); 18 | createAndPopulateTables(); 19 | } 20 | 21 | private void createDatabase(String dbName) throws SQLException { 22 | try { 23 | executeUpdate("CREATE DATABASE " + dbName); 24 | System.out.println("Database created!"); 25 | } catch (SQLException e) { 26 | if (e.getErrorCode() == 1007) 27 | System.out.println("Database exists!"); 28 | else 29 | throw e; 30 | } 31 | } 32 | 33 | private void createAndPopulateTables() throws SQLException { 34 | try { 35 | String createAuthTableSQL = """ 36 | CREATE TABLE auth ( 37 | id INT AUTO_INCREMENT PRIMARY KEY, 38 | name VARCHAR(100), 39 | email VARCHAR(100), 40 | password VARCHAR(100), 41 | role VARCHAR(20)) 42 | """; 43 | executeUpdate(createAuthTableSQL); 44 | String coursesTableSQL = """ 45 | CREATE TABLE courses ( 46 | course_id INT PRIMARY KEY AUTO_INCREMENT, 47 | course_name VARCHAR(100) NOT NULL) 48 | """; 49 | executeUpdate(coursesTableSQL); 50 | String insertIntoCoursesSQL = """ 51 | INSERT INTO courses(course_name) VALUES 52 | ('Bachelors in Information Technology'), 53 | ('Bachelors in International Business Management'), 54 | ('International Master of Business Administration'); 55 | """; 56 | executeUpdate(insertIntoCoursesSQL); 57 | String modulesTableSQL = """ 58 | CREATE TABLE modules ( 59 | module_id INT PRIMARY KEY AUTO_INCREMENT, 60 | module_name VARCHAR(100) NOT NULL, 61 | module_type VARCHAR(100) NOT NULL, 62 | course_id INT NOT NULL, 63 | FOREIGN KEY (course_id) REFERENCES courses(course_id)) 64 | """; 65 | executeUpdate(modulesTableSQL); 66 | String insertIntoModulesSQL = """ 67 | INSERT INTO modules (module_name,module_type, course_id) VALUES 68 | ('Computational Mathematics','core', 1), 69 | ('Fundamentals of Computing','core', 1), 70 | ('Embedded System Programming','core', 1), 71 | ('Academic Skills and Team-Based Learning','optional', 1), 72 | ('Introductory Programming and Problem Solving','optional', 1), 73 | ('21st Century Management','core', 2), 74 | ('Principles of Business','core', 2), 75 | ('Project Based Learning','core', 2), 76 | ('The Digital Business','optional', 2), 77 | ('The Innovative Business','optional', 2), 78 | ('The Masters Research Project','core', 3), 79 | ('Financial Decision Making','core', 3), 80 | ('The Masters Professional Project','core', 3), 81 | ('Strategic Operations Management','optional', 3), 82 | ('Strategic Global Marketing','optional', 3); 83 | """; 84 | executeUpdate(insertIntoModulesSQL); 85 | String studentsTableSQL = """ 86 | CREATE TABLE students ( 87 | student_id INT PRIMARY KEY AUTO_INCREMENT, 88 | student_name VARCHAR(100) NOT NULL, 89 | course_id INT NOT NULL, 90 | student_phone VARCHAR(100) NOT NULL, 91 | level INT NOT NULL, 92 | auth_id INT, 93 | FOREIGN KEY (course_id) REFERENCES courses(course_id), 94 | FOREIGN KEY (auth_id) REFERENCES auth(id)) 95 | """; 96 | executeUpdate(studentsTableSQL); 97 | String teachersTableSQL = """ 98 | CREATE TABLE teachers ( 99 | teacher_id INT PRIMARY KEY AUTO_INCREMENT, 100 | teacher_name VARCHAR(100) NOT NULL, 101 | teacher_phone VARCHAR(100) NOT NULL, 102 | auth_id INT, 103 | FOREIGN KEY (auth_id) REFERENCES auth(id)) 104 | """; 105 | executeUpdate(teachersTableSQL); 106 | String enrollmentTableSQL = """ 107 | CREATE TABLE enrollments ( 108 | student_id INT NOT NULL, 109 | course_id INT NOT NULL, 110 | FOREIGN KEY (student_id) REFERENCES students(student_id), 111 | FOREIGN KEY (course_id) REFERENCES courses(course_id), 112 | PRIMARY KEY (student_id, course_id)) 113 | """; 114 | executeUpdate(enrollmentTableSQL); 115 | String teacherModulesTableSQL = """ 116 | CREATE TABLE teachers_modules ( 117 | teacher_id INT NOT NULL, 118 | module_id INT NOT NULL, 119 | FOREIGN KEY (teacher_id) REFERENCES teachers(teacher_id), 120 | FOREIGN KEY (module_id) REFERENCES modules(module_id), 121 | PRIMARY KEY (teacher_id, module_id)) 122 | """; 123 | executeUpdate(teacherModulesTableSQL); 124 | String resultTableSQL = """ 125 | CREATE TABLE results ( 126 | student_id INT NOT NULL, 127 | module_id INT NOT NULL, 128 | marks INT NOT NULL, 129 | FOREIGN KEY (student_id) REFERENCES students(student_id), 130 | FOREIGN KEY (module_id) REFERENCES modules(module_id), 131 | PRIMARY KEY (student_id, module_id)) 132 | """; 133 | executeUpdate(resultTableSQL); 134 | System.out.println("Tables Created and Populated!"); 135 | } catch (SQLException e) { 136 | if (e.getErrorCode() == 1050) 137 | System.out.println("Table exists!"); 138 | else 139 | throw e; 140 | } 141 | } 142 | 143 | public static DatabaseManager getInstance() throws SQLException { 144 | if (instance == null) 145 | instance = new DatabaseManager("jdbc:mysql://localhost:3306", "root", ""); 146 | return instance; 147 | } 148 | 149 | public Connection getConnection() { 150 | return conn; 151 | } 152 | 153 | public void executeUpdate(String sql) throws SQLException { 154 | try (PreparedStatement preparedStatement = conn.prepareStatement(sql)) { 155 | preparedStatement.executeUpdate(); 156 | } 157 | } 158 | 159 | public PreparedStatement getPreparedStatement(String sql) throws SQLException { 160 | return conn.prepareStatement(sql); 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /src/pages/Login.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import java.awt.Color; 4 | import java.awt.Font; 5 | import java.awt.Image; 6 | import java.awt.event.ActionEvent; 7 | import java.awt.event.ActionListener; 8 | import java.awt.event.MouseAdapter; 9 | import java.awt.event.MouseEvent; 10 | 11 | import javax.swing.BorderFactory; 12 | import javax.swing.ImageIcon; 13 | import javax.swing.JButton; 14 | import javax.swing.JFrame; 15 | import javax.swing.JLabel; 16 | import javax.swing.JPanel; 17 | import javax.swing.JPasswordField; 18 | import javax.swing.JTextField; 19 | 20 | import auth.Auth; 21 | import exceptions.InvalidEmailException; 22 | import exceptions.InvalidPasswordException; 23 | import models.user.SystemUser; 24 | import util.CustomImage; 25 | import util.Validator; 26 | 27 | public class Login extends JPanel { 28 | 29 | private static final long serialVersionUID = -2934127260269983979L; 30 | 31 | private JTextField emailTxt; 32 | private JPasswordField passwordTxt; 33 | 34 | public Login(JFrame frame, CustomImage logo) { 35 | 36 | ImageIcon loginImg = new ImageIcon(getClass().getResource("/resources/login_image.png")); 37 | Image loginImage = loginImg.getImage().getScaledInstance(1100, 708, java.awt.Image.SCALE_SMOOTH); 38 | loginImg = new ImageIcon(loginImage); 39 | 40 | JPanel login = new JPanel(); 41 | login.setBackground(new Color(255, 255, 255)); 42 | login.setBounds(0, -7, 1480, 701); 43 | frame.getContentPane().add(login); 44 | login.setVisible(true); 45 | login.setLayout(null); 46 | 47 | JLabel password_eye = new JLabel( 48 | new ImageIcon(Login.class.getResource("/resources/password_eye_show.png"))); 49 | password_eye.setBounds(339, 414, 30, 19); 50 | login.add(password_eye); 51 | 52 | JLabel logoImg = new JLabel(logo.getImage(100, 100)); 53 | logoImg.setBounds(52, 43, 100, 100); 54 | login.add(logoImg); 55 | 56 | JLabel loginImgLabel = new JLabel(loginImg); 57 | loginImgLabel.setBounds(437, 0, 1043, 708); 58 | login.add(loginImgLabel); 59 | 60 | JLabel title = new JLabel("Welcome"); 61 | title.setFont(new Font("Futura", Font.PLAIN, 40)); 62 | title.setBounds(52, 155, 352, 54); 63 | login.add(title); 64 | 65 | JLabel subtitle = new JLabel("Please login to get started"); 66 | subtitle.setFont(new Font("Futura", Font.PLAIN, 20)); 67 | subtitle.setBounds(52, 205, 302, 27); 68 | login.add(subtitle); 69 | 70 | emailTxt = new JTextField(); 71 | emailTxt.addActionListener(new ActionListener() { 72 | @Override 73 | public void actionPerformed(ActionEvent e) { 74 | passwordTxt.requestFocusInWindow(); 75 | } 76 | }); 77 | emailTxt.setFont(new Font("Futura", Font.PLAIN, 15)); 78 | emailTxt.setColumns(10); 79 | emailTxt.setBounds(52, 315, 332, 43); 80 | emailTxt.setBorder( 81 | BorderFactory.createCompoundBorder(emailTxt.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 5))); 82 | login.add(emailTxt); 83 | 84 | JLabel emailLbl = new JLabel("Email"); 85 | emailLbl.setFont(new Font("Futura", Font.PLAIN, 15)); 86 | emailLbl.setBounds(55, 293, 42, 16); 87 | login.add(emailLbl); 88 | 89 | JLabel passwordLbl = new JLabel("Password"); 90 | passwordLbl.setFont(new Font("Futura", Font.PLAIN, 15)); 91 | passwordLbl.setBounds(55, 384, 80, 16); 92 | login.add(passwordLbl); 93 | 94 | JLabel bottomLbl = new JLabel("Don't have an account ?"); 95 | bottomLbl.setEnabled(false); 96 | bottomLbl.setFont(new Font("Kohinoor Bangla", Font.PLAIN, 12)); 97 | bottomLbl.setBounds(110, 544, 152, 27); 98 | login.add(bottomLbl); 99 | 100 | JLabel signUpBtn = new JLabel("Sign Up"); 101 | signUpBtn.addMouseListener(new MouseAdapter() { 102 | @Override 103 | public void mouseClicked(MouseEvent e) { 104 | new SignUp(frame, logo); 105 | login.setVisible(false); 106 | } 107 | 108 | @Override 109 | public void mouseEntered(MouseEvent e) { 110 | signUpBtn.setForeground(new Color(242, 147, 179)); 111 | } 112 | 113 | @Override 114 | public void mouseExited(MouseEvent e) { 115 | signUpBtn.setForeground(Color.BLACK); 116 | } 117 | }); 118 | signUpBtn.setFont(new Font("Kohinoor Bangla", Font.BOLD, 12)); 119 | signUpBtn.setBounds(245, 544, 43, 27); 120 | login.add(signUpBtn); 121 | 122 | JLabel errorEmailLbl = new JLabel(""); 123 | errorEmailLbl.setForeground(new Color(255, 0, 7)); 124 | errorEmailLbl.setFont(new Font("Kohinoor Bangla", Font.PLAIN, 12)); 125 | errorEmailLbl.setBounds(55, 356, 322, 27); 126 | login.add(errorEmailLbl); 127 | 128 | JLabel errorPasswordLbl = new JLabel(""); 129 | errorPasswordLbl.setForeground(new Color(255, 0, 7)); 130 | errorPasswordLbl.setFont(new Font("Kohinoor Bangla", Font.PLAIN, 12)); 131 | errorPasswordLbl.setBounds(54, 442, 322, 27); 132 | login.add(errorPasswordLbl); 133 | 134 | ActionListener loginAction = new ActionListener() { 135 | public void actionPerformed(ActionEvent e) { 136 | errorEmailLbl.setText(""); 137 | errorPasswordLbl.setText(""); 138 | 139 | String email = emailTxt.getText().strip().toLowerCase(); 140 | String password = new String(passwordTxt.getPassword()).strip(); 141 | 142 | if (!Validator.validate(email, errorEmailLbl, "Email")) { 143 | return; 144 | } 145 | 146 | try { 147 | SystemUser user = Auth.returnSystemUser(email, password); 148 | 149 | emailTxt.setText(""); 150 | passwordTxt.setText(""); 151 | 152 | new Dashboard(frame, user, login); 153 | 154 | login.setVisible(false); 155 | 156 | } catch (InvalidEmailException errEmail) { 157 | errorEmailLbl.setText(errEmail.getMessage()); 158 | } catch (InvalidPasswordException errPassword) { 159 | errorPasswordLbl.setText(errPassword.getMessage()); 160 | } catch (Exception e1) { 161 | e1.printStackTrace(); 162 | } 163 | 164 | } 165 | }; 166 | 167 | passwordTxt = new JPasswordField(); 168 | passwordTxt.setColumns(10); 169 | passwordTxt.setFont(new Font("Futura", Font.PLAIN, 15)); 170 | passwordTxt.setBounds(52, 401, 332, 43); 171 | passwordTxt.addActionListener(loginAction); 172 | passwordTxt.setBorder(BorderFactory.createCompoundBorder(passwordTxt.getBorder(), 173 | BorderFactory.createEmptyBorder(0, 5, 0, 38))); 174 | login.add(passwordTxt); 175 | 176 | password_eye.addMouseListener(new MouseAdapter() { 177 | @Override 178 | public void mouseClicked(MouseEvent e) { 179 | if (passwordTxt.getEchoChar() == '●') { 180 | passwordTxt.setEchoChar((char) 0); 181 | password_eye 182 | .setIcon(new ImageIcon(Login.class.getResource("/resources/password_eye_hide.png"))); 183 | } else { 184 | passwordTxt.setEchoChar('●'); 185 | password_eye 186 | .setIcon(new ImageIcon(Login.class.getResource("/resources/password_eye_show.png"))); 187 | } 188 | } 189 | }); 190 | 191 | JButton btnLogin = new JButton("Login"); 192 | btnLogin.addActionListener(loginAction); 193 | btnLogin.setFont(new Font("Futura", Font.PLAIN, 15)); 194 | btnLogin.setForeground(new Color(242, 252, 255)); 195 | btnLogin.setOpaque(true); 196 | btnLogin.setBorderPainted(false); 197 | btnLogin.setBackground(new Color(0, 0, 0)); 198 | btnLogin.setBounds(49, 489, 332, 43); 199 | login.add(btnLogin); 200 | } 201 | 202 | } 203 | -------------------------------------------------------------------------------- /src/pages/Courses.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.Color; 5 | import java.awt.Font; 6 | import java.awt.GridLayout; 7 | import java.awt.event.MouseAdapter; 8 | import java.awt.event.MouseEvent; 9 | import java.sql.SQLException; 10 | import java.util.ArrayList; 11 | 12 | import javax.swing.BorderFactory; 13 | import javax.swing.ImageIcon; 14 | import javax.swing.JLabel; 15 | import javax.swing.JOptionPane; 16 | import javax.swing.JPanel; 17 | import javax.swing.JTextField; 18 | import javax.swing.SwingConstants; 19 | 20 | import models.course.Course; 21 | import models.user.Admin; 22 | import models.user.SystemUser; 23 | import util.DataManager; 24 | import util.DataRetriever; 25 | 26 | public class Courses extends JPanel { 27 | 28 | private static final long serialVersionUID = -4551964813659714257L; 29 | private static Courses instance; 30 | JPanel coursesGrid; 31 | 32 | private Courses(JPanel main, SystemUser user) { 33 | main.add(this); 34 | this.setBounds(113, 0, 1341, 701); 35 | setLayout(null); 36 | 37 | JPanel courses = new JPanel(); 38 | courses.setLayout(null); 39 | courses.setBounds(113, 0, 1222, 701); 40 | add(courses); 41 | 42 | String e = user instanceof Admin ? "Manage" : "View"; 43 | JLabel title = new JLabel(e + " Courses"); 44 | title.setFont(new Font("Futura", Font.PLAIN, 25)); 45 | title.setBounds(5, 75, 236, 36); 46 | courses.add(title); 47 | 48 | JLabel addBtnLbl = new JLabel(new ImageIcon(getClass().getResource("/resources/add.png"))); 49 | addBtnLbl.addMouseListener(new MouseAdapter() { 50 | @Override 51 | public void mouseClicked(MouseEvent e) { 52 | JPanel panel = new JPanel(new GridLayout(2, 2)); 53 | JLabel nameLabel = new JLabel("Enter course name: "); 54 | JTextField nameField = new JTextField(); 55 | JLabel codeLabel = new JLabel("Enter course code: "); 56 | JTextField codeField = new JTextField(); 57 | panel.add(codeLabel); 58 | panel.add(codeField); 59 | panel.add(nameLabel); 60 | panel.add(nameField); 61 | 62 | int result = JOptionPane.showConfirmDialog(null, panel, "Add Course", JOptionPane.OK_CANCEL_OPTION); 63 | if (result == JOptionPane.OK_OPTION) { 64 | String courseName = nameField.getText(); 65 | String courseId = codeField.getText(); 66 | String[] column = { "course_id", "course_name" }; 67 | String[] values = { courseId, courseName }; 68 | try { 69 | DataManager.insert("courses", column, values); 70 | JOptionPane.showMessageDialog(null, "Course added successfully"); 71 | courses.remove(coursesGrid); 72 | createCoursesGrid(courses); 73 | courses.revalidate(); 74 | courses.repaint(); 75 | 76 | } catch (SQLException e1) { 77 | JOptionPane.showMessageDialog(null, "Error adding course", e1.getMessage(), 78 | JOptionPane.ERROR_MESSAGE); 79 | } 80 | } 81 | 82 | } 83 | }); 84 | addBtnLbl.setBounds(854, 47, 45, 45); 85 | 86 | JLabel editBtnLbl = new JLabel(new ImageIcon(getClass().getResource("/resources/edit.png"))); 87 | editBtnLbl.addMouseListener(new MouseAdapter() { 88 | @Override 89 | public void mouseClicked(MouseEvent e) { 90 | JPanel panel = new JPanel(new GridLayout(2, 2)); 91 | JLabel nameLabel = new JLabel("Enter course name: "); 92 | JTextField nameField = new JTextField(); 93 | JLabel codeLabel = new JLabel("Enter course code: "); 94 | JTextField codeField = new JTextField(); 95 | panel.add(codeLabel); 96 | panel.add(codeField); 97 | panel.add(nameLabel); 98 | panel.add(nameField); 99 | 100 | int result = JOptionPane.showConfirmDialog(null, panel, "Edit Course", JOptionPane.OK_CANCEL_OPTION); 101 | if (result == JOptionPane.OK_OPTION) { 102 | String courseName = nameField.getText(); 103 | String courseId = codeField.getText(); 104 | try { 105 | DataManager.editCourse(courseId, courseName); 106 | JOptionPane.showMessageDialog(null, "Course updated successfully"); 107 | courses.remove(coursesGrid); 108 | createCoursesGrid(courses); 109 | courses.revalidate(); 110 | courses.repaint(); 111 | 112 | } catch (SQLException e1) { 113 | JOptionPane.showMessageDialog(null, "Error updating course", e1.getMessage(), 114 | JOptionPane.ERROR_MESSAGE); 115 | } 116 | } 117 | 118 | } 119 | }); 120 | editBtnLbl.setBounds(946, 43, 45, 45); 121 | 122 | JLabel removeBtnLbl = new JLabel(new ImageIcon(getClass().getResource("/resources/remove.png"))); 123 | removeBtnLbl.addMouseListener(new MouseAdapter() { 124 | @Override 125 | public void mouseClicked(MouseEvent e) { 126 | JPanel panel = new JPanel(new GridLayout(2, 2)); 127 | JLabel idLabel = new JLabel("Enter course id: "); 128 | JTextField idField = new JTextField(); 129 | 130 | panel.add(idLabel); 131 | panel.add(idField); 132 | 133 | int result = JOptionPane.showConfirmDialog(null, panel, "Delete Course", JOptionPane.OK_CANCEL_OPTION); 134 | if (result == JOptionPane.OK_OPTION) { 135 | String courseId = idField.getText(); 136 | 137 | try { 138 | DataManager.deleteCourse(courseId); 139 | JOptionPane.showMessageDialog(null, "Course deleted successfully"); 140 | courses.remove(coursesGrid); 141 | createCoursesGrid(courses); 142 | courses.revalidate(); 143 | courses.repaint(); 144 | 145 | } catch (SQLException e1) { 146 | System.out.println(e1.getMessage()); 147 | JOptionPane.showMessageDialog(null, "Error deleting course", e1.getMessage(), 148 | JOptionPane.ERROR_MESSAGE); 149 | } 150 | } 151 | 152 | } 153 | }); 154 | removeBtnLbl.setBounds(1036, 47, 45, 45); 155 | 156 | JLabel addLbl = new JLabel("Add"); 157 | addLbl.setHorizontalAlignment(SwingConstants.CENTER); 158 | addLbl.setBounds(846, 103, 61, 16); 159 | 160 | JLabel editLbl = new JLabel("Edit"); 161 | editLbl.setHorizontalAlignment(SwingConstants.CENTER); 162 | editLbl.setBounds(931, 104, 61, 16); 163 | 164 | JLabel removeLbl = new JLabel("Remove"); 165 | removeLbl.setHorizontalAlignment(SwingConstants.CENTER); 166 | removeLbl.setBounds(1029, 103, 61, 16); 167 | 168 | if (user instanceof Admin) { 169 | courses.add(addLbl); 170 | courses.add(editLbl); 171 | courses.add(removeLbl); 172 | courses.add(addBtnLbl); 173 | courses.add(editBtnLbl); 174 | courses.add(removeBtnLbl); 175 | } 176 | 177 | createCoursesGrid(courses); 178 | 179 | } 180 | 181 | private void createCoursesGrid(JPanel courses) { 182 | ArrayList allCourses = DataRetriever.getCourses(); 183 | 184 | coursesGrid = new JPanel(); 185 | coursesGrid.setBounds(4, 207, 1046, 219); 186 | courses.add(coursesGrid); 187 | 188 | coursesGrid.setLayout(new GridLayout(0, 2, 0, 0)); 189 | for (int i = 0; i < allCourses.size(); i++) { 190 | JPanel coursesGridBox = new JPanel(); 191 | coursesGridBox.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 192 | coursesGrid.add(coursesGridBox); 193 | coursesGridBox.setLayout(new BorderLayout(0, 0)); 194 | 195 | JLabel coursesIdLbl = new JLabel( 196 | "
Course ID
" + allCourses.get(i).getId() + "
"); 197 | coursesIdLbl.setHorizontalAlignment(SwingConstants.CENTER); 198 | coursesGridBox.add(coursesIdLbl, BorderLayout.NORTH); 199 | 200 | JLabel coursesNameLbl = new JLabel( 201 | "
Course Name
" + allCourses.get(i).getName() + "
"); 202 | coursesNameLbl.setHorizontalAlignment(SwingConstants.CENTER); 203 | coursesGridBox.add(coursesNameLbl, BorderLayout.CENTER); 204 | } 205 | } 206 | 207 | // Singleton to ensure one and only instance 208 | public static Courses getInstance(JPanel main, SystemUser user) { 209 | if (instance == null) { 210 | instance = new Courses(main, user); 211 | } 212 | return instance; 213 | } 214 | 215 | public static void dispose() { 216 | instance = null; 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /src/util/DataRetriever.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import java.sql.PreparedStatement; 4 | import java.sql.ResultSet; 5 | import java.sql.SQLException; 6 | import java.util.ArrayList; 7 | 8 | import models.course.Course; 9 | import models.course.Module; 10 | import models.user.Student; 11 | import models.user.Teacher; 12 | 13 | public class DataRetriever { 14 | private static PreparedStatement retrieveCourses; 15 | private static DatabaseManager db; 16 | 17 | public DataRetriever() throws SQLException { 18 | db = DatabaseManager.getInstance(); 19 | } 20 | 21 | public static ArrayList getCourses() { 22 | ArrayList courses = new ArrayList(); 23 | try { 24 | retrieveCourses = db.getConnection().prepareStatement("SELECT * FROM courses"); 25 | java.sql.ResultSet rs = retrieveCourses.executeQuery(); 26 | while (rs.next()) { 27 | courses.add(Course.fromSql(rs.getInt("course_id"), rs.getString("course_name"))); 28 | } 29 | } catch (SQLException e) { 30 | e.printStackTrace(); 31 | } 32 | for (Course course : courses) { 33 | course.setModules(getModules(course.getId())); 34 | } 35 | return courses; 36 | } 37 | 38 | public static Course getCourses(int courseId) throws SQLException { 39 | try { 40 | String sql = "SELECT * FROM courses WHERE course_id = ?"; 41 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 42 | ps.setInt(1, courseId); 43 | ResultSet rs = ps.executeQuery(); 44 | if (rs.next()) { 45 | Course course = Course.fromSql(rs.getInt("course_id"), rs.getString("course_name")); 46 | course.setModules(getModules(course.getId())); 47 | return course; 48 | } 49 | } catch (SQLException e) { 50 | throw e; 51 | } 52 | return null; 53 | } 54 | 55 | public static ArrayList getAllModules() { 56 | ArrayList allModules = new ArrayList(); 57 | try { 58 | PreparedStatement retrieveModules = db.getConnection() 59 | .prepareStatement("SELECT * FROM modules"); 60 | java.sql.ResultSet rs = retrieveModules.executeQuery(); 61 | while (rs.next()) { 62 | allModules.add(Module.fromSql(rs.getInt("module_id"), rs.getString("module_name"), 63 | rs.getString("module_type"))); 64 | } 65 | } catch (SQLException e) { 66 | e.printStackTrace(); 67 | } 68 | return allModules; 69 | } 70 | 71 | public static ArrayList getModules(int courseId) { 72 | ArrayList modules = new ArrayList(); 73 | try { 74 | PreparedStatement retrieveModules = db.getConnection() 75 | .prepareStatement("SELECT * FROM modules WHERE course_id = ?"); 76 | retrieveModules.setInt(1, courseId); 77 | java.sql.ResultSet rs = retrieveModules.executeQuery(); 78 | while (rs.next()) { 79 | modules.add(Module.fromSql(rs.getInt("module_id"), rs.getString("module_name"), 80 | rs.getString("module_type"))); 81 | } 82 | } catch (SQLException e) { 83 | e.printStackTrace(); 84 | } 85 | return modules; 86 | } 87 | 88 | public static Module getModuleById(int moduleId) { 89 | ArrayList allModules = getAllModules(); 90 | for (Module module : allModules) { 91 | if (module.getId() == moduleId) { 92 | return module; 93 | } 94 | } 95 | return null; 96 | } 97 | 98 | public static ArrayList getTeachers() { 99 | ArrayList teachers = new ArrayList(); 100 | try { 101 | PreparedStatement retrieveTeachers = db.getConnection() 102 | .prepareStatement( 103 | "SELECT teachers.teacher_id, teachers.teacher_name, teachers.teacher_phone, auth.email FROM teachers LEFT JOIN auth ON teachers.auth_id = auth.id"); 104 | java.sql.ResultSet rs = retrieveTeachers.executeQuery(); 105 | while (rs.next()) { 106 | int teacherId = rs.getInt("teacher_id"); 107 | ArrayList modulesTaught = getModulesTaughtByTeacher(teacherId); 108 | 109 | teachers.add(Teacher.fromSql(teacherId, rs.getString("teacher_name"), 110 | rs.getString("teacher_phone"), rs.getString("email"), modulesTaught)); 111 | } 112 | } catch (SQLException e) { 113 | e.printStackTrace(); 114 | } 115 | return teachers; 116 | } 117 | 118 | private static ArrayList getModulesTaughtByTeacher(int teacherId) { 119 | ArrayList allModules = getAllModules(); 120 | ArrayList modulesTaught = new ArrayList(); 121 | try { 122 | PreparedStatement retrieveModules = db.getConnection() 123 | .prepareStatement("SELECT module_id FROM teachers_modules WHERE teacher_id = ?"); 124 | retrieveModules.setInt(1, teacherId); 125 | java.sql.ResultSet rs = retrieveModules.executeQuery(); 126 | while (rs.next()) { 127 | allModules.forEach(module -> { 128 | try { 129 | if (module.getId() == rs.getInt("module_id")) { 130 | modulesTaught.add(module); 131 | } 132 | } catch (SQLException e) { 133 | e.printStackTrace(); 134 | } 135 | }); 136 | } 137 | } catch (SQLException e) { 138 | e.printStackTrace(); 139 | } 140 | return modulesTaught; 141 | } 142 | 143 | public static ArrayList getStudents() { 144 | ArrayList students = new ArrayList(); 145 | try { 146 | PreparedStatement retrieveStudents = db.getConnection() 147 | .prepareStatement( 148 | "SELECT students.student_id, students.student_name, students.student_phone, students.level, students.course_id, auth.email FROM students LEFT JOIN auth ON students.auth_id = auth.id"); 149 | java.sql.ResultSet rs = retrieveStudents.executeQuery(); 150 | while (rs.next()) { 151 | int enrolledCourseId = rs.getInt("course_id"); 152 | ArrayList allCourses = DataRetriever.getCourses(); 153 | Course enrolledCourse = null; 154 | for (Course course : allCourses) { 155 | if (course.getId() == enrolledCourseId) { 156 | enrolledCourse = course; 157 | } 158 | } 159 | students.add(Student.fromSql(rs.getInt("student_id"), rs.getString("student_name"), 160 | rs.getString("student_phone"), rs.getString("email"), rs.getInt("level"), enrolledCourse)); 161 | } 162 | } catch (SQLException e) { 163 | e.printStackTrace(); 164 | } 165 | return students; 166 | } 167 | 168 | public static Student getStudents(int studentId) { 169 | try { 170 | PreparedStatement retrieveStudents = db.getConnection() 171 | .prepareStatement( 172 | "SELECT students.student_id, students.student_name, students.student_phone, students.level, students.course_id, auth.email FROM students LEFT JOIN auth ON students.auth_id = auth.id WHERE student_id = ?"); 173 | retrieveStudents.setInt(1, studentId); 174 | java.sql.ResultSet rs = retrieveStudents.executeQuery(); 175 | if (rs.next()) { 176 | int enrolledCourseId = rs.getInt("course_id"); 177 | ArrayList allCourses = DataRetriever.getCourses(); 178 | Course enrolledCourse = null; 179 | for (Course course : allCourses) { 180 | if (course.getId() == enrolledCourseId) { 181 | enrolledCourse = course; 182 | } 183 | } 184 | 185 | return Student.fromSql(rs.getInt("student_id"), rs.getString("student_name"), 186 | rs.getString("student_phone"), rs.getString("email"), rs.getInt("level"), enrolledCourse); 187 | } 188 | } catch ( 189 | 190 | SQLException e) { 191 | e.printStackTrace(); 192 | } 193 | return null; 194 | } 195 | 196 | } 197 | -------------------------------------------------------------------------------- /src/pages/SignUp.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import java.awt.Color; 4 | import java.awt.Font; 5 | import java.awt.Image; 6 | import java.awt.event.ActionEvent; 7 | import java.awt.event.ActionListener; 8 | import java.awt.event.MouseAdapter; 9 | import java.awt.event.MouseEvent; 10 | import java.util.ArrayList; 11 | 12 | import javax.swing.BorderFactory; 13 | import javax.swing.ImageIcon; 14 | import javax.swing.JButton; 15 | import javax.swing.JDialog; 16 | import javax.swing.JFrame; 17 | import javax.swing.JLabel; 18 | import javax.swing.JPanel; 19 | import javax.swing.JPasswordField; 20 | import javax.swing.JTextField; 21 | import javax.swing.Timer; 22 | 23 | import auth.Auth; 24 | import util.CustomImage; 25 | import util.Validator; 26 | 27 | public class SignUp extends JPanel { 28 | 29 | private static final long serialVersionUID = 8409831875589206498L; 30 | private JTextField nameTxt; 31 | private JTextField emailTxt; 32 | private JPasswordField passwordTxt; 33 | 34 | public SignUp(JFrame frame, CustomImage logo) { 35 | 36 | ImageIcon signupImg = new ImageIcon(getClass().getResource("/resources/signup_image.png")); 37 | Image signupImage = signupImg.getImage().getScaledInstance(1100, 708, java.awt.Image.SCALE_SMOOTH); 38 | signupImg = new ImageIcon(signupImage); 39 | 40 | JPanel signup = new JPanel(); 41 | signup.setBackground(new Color(255, 255, 255)); 42 | signup.setBounds(0, -7, 1533, 701); 43 | frame.getContentPane().add(signup); 44 | signup.setVisible(true); 45 | signup.setLayout(null); 46 | 47 | JLabel password_eye = new JLabel( 48 | new ImageIcon(getClass().getResource("/resources/password_eye_show.png"))); 49 | password_eye.setBounds(336, 490, 41, 22); 50 | signup.add(password_eye); 51 | 52 | JLabel logoImg = new JLabel(logo.getImage(100, 100)); 53 | logoImg.setBounds(52, 43, 100, 100); 54 | signup.add(logoImg); 55 | 56 | JLabel signupImgLabel = new JLabel(signupImg); 57 | signupImgLabel.setBounds(277, -5, 1290, 708); 58 | signup.add(signupImgLabel); 59 | 60 | JLabel title = new JLabel("Create an account"); 61 | title.setFont(new Font("Futura", Font.PLAIN, 40)); 62 | title.setBounds(52, 155, 352, 54); 63 | signup.add(title); 64 | 65 | JLabel subtitle = new JLabel("Let's get started"); 66 | subtitle.setFont(new Font("Futura", Font.PLAIN, 20)); 67 | subtitle.setBounds(52, 205, 145, 27); 68 | signup.add(subtitle); 69 | 70 | nameTxt = new JTextField(); 71 | nameTxt.addActionListener(new ActionListener() { 72 | @Override 73 | public void actionPerformed(ActionEvent e) { 74 | emailTxt.requestFocusInWindow(); 75 | } 76 | }); 77 | nameTxt.setFont(new Font("Futura", Font.PLAIN, 15)); 78 | nameTxt.setBounds(52, 306, 332, 43); 79 | nameTxt.setBorder( 80 | BorderFactory.createCompoundBorder(nameTxt.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 5))); 81 | signup.add(nameTxt); 82 | nameTxt.setColumns(10); 83 | 84 | emailTxt = new JTextField(); 85 | emailTxt.addActionListener(new ActionListener() { 86 | @Override 87 | public void actionPerformed(ActionEvent e) { 88 | passwordTxt.requestFocusInWindow(); 89 | } 90 | }); 91 | emailTxt.setFont(new Font("Futura", Font.PLAIN, 15)); 92 | emailTxt.setColumns(10); 93 | emailTxt.setBounds(52, 393, 332, 43); 94 | emailTxt.setBorder( 95 | BorderFactory.createCompoundBorder(emailTxt.getBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 5))); 96 | signup.add(emailTxt); 97 | 98 | JLabel nameLbl = new JLabel("Name"); 99 | nameLbl.setFont(new Font("Futura", Font.PLAIN, 15)); 100 | nameLbl.setBounds(55, 290, 42, 16); 101 | signup.add(nameLbl); 102 | 103 | JLabel emailLbl = new JLabel("Email"); 104 | emailLbl.setFont(new Font("Futura", Font.PLAIN, 15)); 105 | emailLbl.setBounds(55, 371, 42, 16); 106 | signup.add(emailLbl); 107 | 108 | JLabel passwordLbl = new JLabel("Password"); 109 | passwordLbl.setFont(new Font("Futura", Font.PLAIN, 15)); 110 | passwordLbl.setBounds(55, 462, 80, 16); 111 | signup.add(passwordLbl); 112 | 113 | JLabel bottomLbl = new JLabel("Already have an account ?"); 114 | bottomLbl.setEnabled(false); 115 | bottomLbl.setFont(new Font("Kohinoor Bangla", Font.PLAIN, 12)); 116 | bottomLbl.setBounds(107, 606, 152, 27); 117 | signup.add(bottomLbl); 118 | 119 | JLabel loginBtn = new JLabel("Login"); 120 | loginBtn.addMouseListener(new MouseAdapter() { 121 | @Override 122 | public void mouseClicked(MouseEvent e) { 123 | new Login(frame, logo); 124 | signup.setVisible(false); 125 | } 126 | 127 | @Override 128 | public void mouseEntered(MouseEvent e) { 129 | loginBtn.setForeground(new Color(242, 147, 179)); 130 | } 131 | 132 | @Override 133 | public void mouseExited(MouseEvent e) { 134 | loginBtn.setForeground(Color.BLACK); 135 | } 136 | }); 137 | loginBtn.setFont(new Font("Kohinoor Bangla", Font.BOLD, 12)); 138 | loginBtn.setBounds(257, 606, 42, 27); 139 | signup.add(loginBtn); 140 | 141 | JLabel errorNameLbl = new JLabel(""); 142 | errorNameLbl.setForeground(new Color(255, 0, 7)); 143 | errorNameLbl.setFont(new Font("Kohinoor Bangla", Font.PLAIN, 12)); 144 | errorNameLbl.setBounds(55, 343, 322, 27); 145 | signup.add(errorNameLbl); 146 | 147 | JLabel errorEmailLbl = new JLabel(""); 148 | errorEmailLbl.setForeground(new Color(255, 0, 7)); 149 | errorEmailLbl.setFont(new Font("Kohinoor Bangla", Font.PLAIN, 12)); 150 | errorEmailLbl.setBounds(55, 434, 322, 27); 151 | signup.add(errorEmailLbl); 152 | 153 | JLabel errorPasswordLbl = new JLabel(""); 154 | errorPasswordLbl.setForeground(new Color(255, 0, 7)); 155 | errorPasswordLbl.setFont(new Font("Kohinoor Bangla", Font.PLAIN, 12)); 156 | errorPasswordLbl.setBounds(55, 515, 322, 27); 157 | signup.add(errorPasswordLbl); 158 | 159 | ActionListener signupAction = new ActionListener() { 160 | public void actionPerformed(ActionEvent e) { 161 | ArrayList isValidSubmission = new ArrayList(); 162 | String name = nameTxt.getText().strip(); 163 | String email = emailTxt.getText().strip().toLowerCase(); 164 | String password = new String(passwordTxt.getPassword()).strip(); 165 | 166 | isValidSubmission.add(Validator.validate(name, errorNameLbl, "Name")); 167 | isValidSubmission.add(Validator.validate(email, errorEmailLbl, "Email")); 168 | isValidSubmission.add(Validator.validate(password, errorPasswordLbl, "Password")); 169 | 170 | if (!isValidSubmission.contains(false)) { 171 | try { 172 | Auth.addCredential(name, email, password); 173 | 174 | JLabel label = new JLabel("Account created successfully"); 175 | label.setHorizontalAlignment(JLabel.CENTER); 176 | 177 | JDialog dialog = new JDialog(); 178 | dialog.setAlwaysOnTop(true); 179 | dialog.setSize(300, 75); 180 | dialog.getContentPane().add(label); 181 | dialog.setUndecorated(true); 182 | dialog.setLocationRelativeTo(null); 183 | 184 | dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 185 | dialog.setVisible(true); 186 | 187 | Timer timer = new Timer(2000, new ActionListener() { 188 | @Override 189 | public void actionPerformed(ActionEvent e) { 190 | dialog.dispose(); 191 | } 192 | }); 193 | timer.setRepeats(false); 194 | timer.start(); 195 | 196 | nameTxt.setText(""); 197 | emailTxt.setText(""); 198 | passwordTxt.setText(""); 199 | 200 | new Login(frame, logo); 201 | signup.setVisible(false); 202 | } catch (Exception err) { 203 | System.out.println(err); 204 | errorEmailLbl.setText(err.getMessage()); 205 | } 206 | } 207 | 208 | } 209 | }; 210 | 211 | passwordTxt = new JPasswordField(); 212 | passwordTxt.setColumns(10); 213 | passwordTxt.setFont(new Font("Futura", Font.PLAIN, 15)); 214 | passwordTxt.setBounds(52, 479, 332, 43); 215 | passwordTxt.setBorder(BorderFactory.createCompoundBorder(passwordTxt.getBorder(), 216 | BorderFactory.createEmptyBorder(0, 5, 0, 38))); 217 | 218 | passwordTxt.addActionListener(signupAction); 219 | signup.add(passwordTxt); 220 | 221 | password_eye.addMouseListener(new MouseAdapter() { 222 | @Override 223 | public void mouseClicked(MouseEvent e) { 224 | if (passwordTxt.getEchoChar() == '●') { 225 | passwordTxt.setEchoChar((char) 0); 226 | password_eye 227 | .setIcon(new ImageIcon(Login.class.getResource("/resources/password_eye_hide.png"))); 228 | } else { 229 | passwordTxt.setEchoChar('●'); 230 | password_eye 231 | .setIcon(new ImageIcon(Login.class.getResource("/resources/password_eye_show.png"))); 232 | } 233 | } 234 | }); 235 | 236 | JButton createBtn = new JButton("Create Account"); 237 | createBtn.addActionListener(signupAction); 238 | createBtn.setFont(new Font("Futura", Font.PLAIN, 15)); 239 | createBtn.setForeground(new Color(242, 252, 255)); 240 | createBtn.setOpaque(true); 241 | createBtn.setBorderPainted(false); 242 | createBtn.setBackground(new Color(0, 0, 0)); 243 | createBtn.setBounds(52, 556, 332, 43); 244 | signup.add(createBtn); 245 | } 246 | } -------------------------------------------------------------------------------- /src/pages/Students.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import java.awt.Font; 4 | import java.sql.SQLException; 5 | import java.util.ArrayList; 6 | 7 | import javax.swing.ImageIcon; 8 | import javax.swing.JLabel; 9 | import javax.swing.JOptionPane; 10 | import javax.swing.JPanel; 11 | import javax.swing.JPasswordField; 12 | import javax.swing.JScrollPane; 13 | import javax.swing.JTable; 14 | import javax.swing.JTextField; 15 | import javax.swing.SwingConstants; 16 | import javax.swing.table.DefaultTableModel; 17 | 18 | import models.user.Admin; 19 | import models.user.Student; 20 | import models.user.SystemUser; 21 | import models.user.Teacher; 22 | 23 | import java.awt.GridLayout; 24 | import java.awt.event.MouseAdapter; 25 | import java.awt.event.MouseEvent; 26 | 27 | import util.DataManager; 28 | import util.DataRetriever; 29 | import javax.swing.JButton; 30 | import java.awt.event.ActionListener; 31 | import java.awt.event.ActionEvent; 32 | 33 | public class Students extends JPanel { 34 | 35 | private static final long serialVersionUID = 7035062181966295884L; 36 | private static Students instance; 37 | JScrollPane scrollPane; 38 | 39 | /** 40 | * Create the panel. 41 | */ 42 | public Students(JPanel main, SystemUser user) { 43 | main.add(this); 44 | this.setBounds(113, 0, 1341, 701); 45 | setLayout(null); 46 | 47 | JPanel students = new JPanel(); 48 | students.setLayout(null); 49 | students.setBounds(113, 0, 1222, 701); 50 | add(students); 51 | 52 | String e = user instanceof Admin ? "Manage" : "View"; 53 | JLabel title = new JLabel(e + " Students"); 54 | title.setFont(new Font("Futura", Font.PLAIN, 25)); 55 | title.setBounds(5, 75, 236, 36); 56 | students.add(title); 57 | 58 | JLabel addBtnLbl = new JLabel(new ImageIcon(getClass().getResource("/resources/add.png"))); 59 | addBtnLbl.addMouseListener(new MouseAdapter() { 60 | @Override 61 | public void mouseClicked(MouseEvent e) { 62 | JPanel panel = new JPanel(new GridLayout(6, 2)); 63 | JLabel nameLabel = new JLabel("Enter student name: "); 64 | JTextField nameField = new JTextField(); 65 | JLabel emailLabel = new JLabel("Enter student email: "); 66 | JTextField emailField = new JTextField(); 67 | JLabel passwordLabel = new JLabel("Enter student password: "); 68 | JPasswordField passwordField = new JPasswordField(); 69 | JLabel numberLabel = new JLabel("Enter student phone number: "); 70 | JTextField numberField = new JTextField(); 71 | JLabel courseLabel = new JLabel("Enter course id: "); 72 | JTextField courseField = new JTextField(); 73 | JLabel levelLabel = new JLabel("Enter student level: "); 74 | JTextField levelField = new JTextField(); 75 | panel.add(emailLabel); 76 | panel.add(emailField); 77 | 78 | panel.add(nameLabel); 79 | panel.add(nameField); 80 | 81 | panel.add(passwordLabel); 82 | panel.add(passwordField); 83 | 84 | panel.add(numberLabel); 85 | panel.add(numberField); 86 | 87 | panel.add(courseLabel); 88 | panel.add(courseField); 89 | 90 | panel.add(levelLabel); 91 | panel.add(levelField); 92 | 93 | int result = JOptionPane.showConfirmDialog(null, panel, "Add Student", JOptionPane.OK_CANCEL_OPTION); 94 | if (result == JOptionPane.OK_OPTION) { 95 | String email = emailField.getText().trim(); 96 | String name = nameField.getText().trim(); 97 | String password = new String(passwordField.getPassword()); 98 | String number = numberField.getText().trim(); 99 | String courseId = courseField.getText().trim(); 100 | String level = levelField.getText().trim(); 101 | try { 102 | DataManager.addStudent(name, number, email, password, Integer.parseInt(courseId), 103 | Integer.parseInt(level)); 104 | JOptionPane.showMessageDialog(null, "Student added successfully"); 105 | refreshTable(students); 106 | } catch (SQLException e1) { 107 | JOptionPane.showMessageDialog(null, "Error adding student", e1.getMessage(), 108 | JOptionPane.ERROR_MESSAGE); 109 | } 110 | } 111 | } 112 | }); 113 | addBtnLbl.setBounds(854, 47, 45, 45); 114 | 115 | JLabel editBtnLbl = new JLabel(new ImageIcon(getClass().getResource("/resources/edit.png"))); 116 | editBtnLbl.addMouseListener(new MouseAdapter() { 117 | @Override 118 | public void mouseClicked(MouseEvent e) { 119 | JPanel panel = new JPanel(new GridLayout(5, 2)); 120 | JLabel idLabel = new JLabel("Enter student id: "); 121 | JTextField idField = new JTextField(); 122 | JLabel nameLabel = new JLabel("Enter student name: "); 123 | JTextField nameField = new JTextField(); 124 | JLabel emailLabel = new JLabel("Enter student email: "); 125 | JTextField emailField = new JTextField(); 126 | JLabel passwordLabel = new JLabel("Enter student password: "); 127 | JPasswordField passwordField = new JPasswordField(); 128 | JLabel numberLabel = new JLabel("Enter student phone number: "); 129 | JTextField numberField = new JTextField(); 130 | 131 | panel.add(idLabel); 132 | panel.add(idField); 133 | 134 | panel.add(emailLabel); 135 | panel.add(emailField); 136 | 137 | panel.add(nameLabel); 138 | panel.add(nameField); 139 | 140 | panel.add(passwordLabel); 141 | panel.add(passwordField); 142 | 143 | panel.add(numberLabel); 144 | panel.add(numberField); 145 | 146 | int result = JOptionPane.showConfirmDialog(null, panel, "Update Student", JOptionPane.OK_CANCEL_OPTION); 147 | if (result == JOptionPane.OK_OPTION) { 148 | String id = idField.getText().trim(); 149 | String email = emailField.getText().trim(); 150 | String name = nameField.getText().trim(); 151 | String password = new String(passwordField.getPassword()); 152 | String number = numberField.getText().trim(); 153 | try { 154 | DataManager.editStudent(id, name, number, email, password); 155 | JOptionPane.showMessageDialog(null, "Student updated successfully"); 156 | refreshTable(students); 157 | 158 | } catch (SQLException e1) { 159 | JOptionPane.showMessageDialog(null, "Error updating student", e1.getMessage(), 160 | JOptionPane.ERROR_MESSAGE); 161 | } 162 | } 163 | 164 | } 165 | }); 166 | editBtnLbl.setBounds(946, 43, 45, 45); 167 | 168 | JLabel removeBtnLbl = new JLabel(new ImageIcon(getClass().getResource("/resources/remove.png"))); 169 | removeBtnLbl.addMouseListener(new MouseAdapter() { 170 | @Override 171 | public void mouseClicked(MouseEvent e) { 172 | JPanel panel = new JPanel(new GridLayout(1, 2)); 173 | JLabel idLabel = new JLabel("Enter student id: "); 174 | JTextField idField = new JTextField(); 175 | 176 | panel.add(idLabel); 177 | panel.add(idField); 178 | 179 | int result = JOptionPane.showConfirmDialog(null, panel, "Delete Student", JOptionPane.OK_CANCEL_OPTION); 180 | if (result == JOptionPane.OK_OPTION) { 181 | String studentId = idField.getText(); 182 | 183 | try { 184 | DataManager.deleteStudent(studentId); 185 | JOptionPane.showMessageDialog(null, "Student deleted successfully"); 186 | refreshTable(students); 187 | 188 | } catch (SQLException e1) { 189 | System.out.println(e1.getMessage()); 190 | JOptionPane.showMessageDialog(null, "Error deleting student", e1.getMessage(), 191 | JOptionPane.ERROR_MESSAGE); 192 | } 193 | } 194 | } 195 | }); 196 | removeBtnLbl.setBounds(1036, 47, 45, 45); 197 | 198 | JLabel addLbl = new JLabel("Add"); 199 | addLbl.setHorizontalAlignment(SwingConstants.CENTER); 200 | addLbl.setBounds(846, 103, 61, 16); 201 | 202 | JLabel editLbl = new JLabel("Edit"); 203 | editLbl.setHorizontalAlignment(SwingConstants.CENTER); 204 | editLbl.setBounds(931, 104, 61, 16); 205 | 206 | JLabel removeLbl = new JLabel("Remove"); 207 | removeLbl.setHorizontalAlignment(SwingConstants.CENTER); 208 | removeLbl.setBounds(1029, 103, 61, 16); 209 | 210 | if (user instanceof Admin) { 211 | students.add(removeBtnLbl); 212 | students.add(editBtnLbl); 213 | students.add(addBtnLbl); 214 | students.add(removeLbl); 215 | students.add(editLbl); 216 | students.add(addLbl); 217 | } 218 | createTable(students); 219 | 220 | if (user instanceof Teacher) { 221 | JButton markStudentBtn = new JButton("Mark Student"); 222 | markStudentBtn.addActionListener(new ActionListener() { 223 | public void actionPerformed(ActionEvent e) { 224 | JPanel panel = new JPanel(new GridLayout(5, 2)); 225 | JLabel idLabel = new JLabel("Enter student id: "); 226 | JTextField idField = new JTextField(); 227 | JLabel moduleLabel = new JLabel("Enter module id: "); 228 | JTextField moduleField = new JTextField(); 229 | JLabel marksLabel = new JLabel("Enter marks obtained: "); 230 | JTextField marksField = new JTextField(); 231 | 232 | panel.add(idLabel); 233 | panel.add(idField); 234 | 235 | panel.add(moduleLabel); 236 | panel.add(moduleField); 237 | 238 | panel.add(marksLabel); 239 | panel.add(marksField); 240 | 241 | int result = JOptionPane.showConfirmDialog(null, panel, "Mark Student", 242 | JOptionPane.OK_CANCEL_OPTION); 243 | if (result == JOptionPane.OK_OPTION) { 244 | String id = idField.getText().trim(); 245 | String moduleId = moduleField.getText(); 246 | String marks = marksField.getText().trim(); 247 | try { 248 | DataManager.markStudent(id, moduleId, marks); 249 | JOptionPane.showMessageDialog(null, "Student marked successfully"); 250 | refreshTable(students); 251 | 252 | } catch (SQLException e1) { 253 | JOptionPane.showMessageDialog(null, "Error marking student", e1.getMessage(), 254 | JOptionPane.ERROR_MESSAGE); 255 | } 256 | } 257 | } 258 | }); 259 | markStudentBtn.setBounds(1021, 121, 117, 36); 260 | students.add(markStudentBtn); 261 | } 262 | } 263 | 264 | private void refreshTable(JPanel students) { 265 | students.remove(scrollPane); 266 | createTable(students); 267 | students.revalidate(); 268 | students.repaint(); 269 | } 270 | 271 | private void createTable(JPanel students) { 272 | scrollPane = new JScrollPane(); 273 | scrollPane.setBounds(0, 185, 1138, 454); 274 | students.add(scrollPane); 275 | 276 | ArrayList studentData = DataRetriever.getStudents(); 277 | 278 | DefaultTableModel model = new DefaultTableModel(); 279 | model.setColumnIdentifiers(new String[] { "ID", "Name", "Phone Number", "Email Address", "Course Enrolled" }); 280 | for (Student student : studentData) { 281 | model.addRow( 282 | new Object[] { student.getId(), student.getName(), student.getPhone(), student.getEmail(), 283 | student.getEnrolledCourse() }); 284 | } 285 | JTable table_1 = new JTable(model); 286 | table_1.setEnabled(false); 287 | scrollPane.setViewportView(table_1); 288 | } 289 | 290 | // Singleton to ensure one and only instance 291 | public static Students getInstance(JPanel main, SystemUser user) { 292 | if (instance == null) { 293 | instance = new Students(main, user); 294 | } 295 | return instance; 296 | } 297 | 298 | public static void dispose() { 299 | instance = null; 300 | } 301 | } 302 | -------------------------------------------------------------------------------- /src/pages/Teachers.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import java.awt.Font; 4 | import java.sql.SQLException; 5 | import java.util.ArrayList; 6 | 7 | import javax.swing.ButtonGroup; 8 | import javax.swing.ImageIcon; 9 | import javax.swing.JLabel; 10 | import javax.swing.JOptionPane; 11 | import javax.swing.JPanel; 12 | import javax.swing.JPasswordField; 13 | import javax.swing.JRadioButton; 14 | import javax.swing.JScrollPane; 15 | import javax.swing.JTable; 16 | import javax.swing.JTextField; 17 | import java.awt.BorderLayout; 18 | import javax.swing.SwingConstants; 19 | import javax.swing.table.DefaultTableModel; 20 | import models.course.Module; 21 | import models.user.Admin; 22 | import models.user.SystemUser; 23 | import models.user.Teacher; 24 | 25 | import java.awt.GridLayout; 26 | import java.awt.event.MouseAdapter; 27 | import java.awt.event.MouseEvent; 28 | 29 | import util.DataManager; 30 | import util.DataRetriever; 31 | 32 | public class Teachers extends JPanel { 33 | 34 | private static final long serialVersionUID = 7035062181966295884L; 35 | private static Teachers instance; 36 | JScrollPane scrollPane; 37 | 38 | /** 39 | * Create the panel. 40 | */ 41 | public Teachers(JPanel main, SystemUser user) { 42 | main.add(this); 43 | this.setBounds(113, 0, 1341, 701); 44 | setLayout(null); 45 | 46 | JPanel students = new JPanel(); 47 | students.setLayout(null); 48 | students.setBounds(113, 0, 1222, 701); 49 | add(students); 50 | 51 | String e = user instanceof Admin ? "Manage" : "View"; 52 | JLabel title = new JLabel(e + " Teachers"); 53 | title.setFont(new Font("Futura", Font.PLAIN, 25)); 54 | title.setBounds(5, 75, 236, 36); 55 | students.add(title); 56 | 57 | JLabel addBtnLbl = new JLabel(new ImageIcon(getClass().getResource("/resources/add.png"))); 58 | addBtnLbl.addMouseListener(new MouseAdapter() { 59 | @Override 60 | public void mouseClicked(MouseEvent e) { 61 | JPanel panel = new JPanel(new GridLayout(4, 2)); 62 | JLabel nameLabel = new JLabel("Enter teacher name: "); 63 | JTextField nameField = new JTextField(); 64 | JLabel emailLabel = new JLabel("Enter teacher email: "); 65 | JTextField emailField = new JTextField(); 66 | JLabel passwordLabel = new JLabel("Enter teacher password: "); 67 | JPasswordField passwordField = new JPasswordField(); 68 | JLabel numberLabel = new JLabel("Enter teacher phone number: "); 69 | JTextField numberField = new JTextField(); 70 | panel.add(emailLabel); 71 | panel.add(emailField); 72 | 73 | panel.add(nameLabel); 74 | panel.add(nameField); 75 | 76 | panel.add(passwordLabel); 77 | panel.add(passwordField); 78 | 79 | panel.add(numberLabel); 80 | panel.add(numberField); 81 | 82 | int result = JOptionPane.showConfirmDialog(null, panel, "Add Teacher", JOptionPane.OK_CANCEL_OPTION); 83 | if (result == JOptionPane.OK_OPTION) { 84 | String email = emailField.getText().trim(); 85 | String name = nameField.getText().trim(); 86 | String password = new String(passwordField.getPassword()); 87 | String number = numberField.getText().trim(); 88 | try { 89 | DataManager.addTeacher(name, number, email, password); 90 | JOptionPane.showMessageDialog(null, "Teacher added successfully"); 91 | refreshTable(students); 92 | } catch (SQLException e1) { 93 | JOptionPane.showMessageDialog(null, "Error adding teacher", e1.getMessage(), 94 | JOptionPane.ERROR_MESSAGE); 95 | } 96 | } 97 | 98 | } 99 | }); 100 | addBtnLbl.setBounds(854, 47, 45, 45); 101 | 102 | JLabel editBtnLbl = new JLabel(new ImageIcon(getClass().getResource("/resources/edit.png"))); 103 | editBtnLbl.setBounds(946, 43, 45, 45); 104 | editBtnLbl.addMouseListener(new MouseAdapter() { 105 | @Override 106 | public void mouseClicked(MouseEvent e) { 107 | JPanel panel = new JPanel(new GridLayout(5, 2)); 108 | JLabel idLabel = new JLabel("Enter teacher id: "); 109 | JTextField idField = new JTextField(); 110 | JLabel nameLabel = new JLabel("Enter teacher name: "); 111 | JTextField nameField = new JTextField(); 112 | JLabel emailLabel = new JLabel("Enter teacher email: "); 113 | JTextField emailField = new JTextField(); 114 | JLabel passwordLabel = new JLabel("Enter teacher password: "); 115 | JPasswordField passwordField = new JPasswordField(); 116 | JLabel numberLabel = new JLabel("Enter teacher phone number: "); 117 | JTextField numberField = new JTextField(); 118 | 119 | panel.add(idLabel); 120 | panel.add(idField); 121 | 122 | panel.add(emailLabel); 123 | panel.add(emailField); 124 | 125 | panel.add(nameLabel); 126 | panel.add(nameField); 127 | 128 | panel.add(passwordLabel); 129 | panel.add(passwordField); 130 | 131 | panel.add(numberLabel); 132 | panel.add(numberField); 133 | 134 | int result = JOptionPane.showConfirmDialog(null, panel, "Update Teacher", JOptionPane.OK_CANCEL_OPTION); 135 | if (result == JOptionPane.OK_OPTION) { 136 | String id = idField.getText().trim(); 137 | String email = emailField.getText().trim(); 138 | String name = nameField.getText().trim(); 139 | String password = new String(passwordField.getPassword()); 140 | String number = numberField.getText().trim(); 141 | try { 142 | DataManager.editTeacher(id, name, number, email, password); 143 | JOptionPane.showMessageDialog(null, "Teacher updated successfully"); 144 | refreshTable(students); 145 | 146 | } catch (SQLException e1) { 147 | JOptionPane.showMessageDialog(null, "Error updating teacher", e1.getMessage(), 148 | JOptionPane.ERROR_MESSAGE); 149 | } 150 | } 151 | 152 | } 153 | }); 154 | 155 | JLabel removeBtnLbl = new JLabel(new ImageIcon(getClass().getResource("/resources/remove.png"))); 156 | removeBtnLbl.addMouseListener(new MouseAdapter() { 157 | @Override 158 | public void mouseClicked(MouseEvent e) { 159 | JPanel panel = new JPanel(new GridLayout(1, 2)); 160 | JLabel idLabel = new JLabel("Enter teacher id: "); 161 | JTextField idField = new JTextField(); 162 | 163 | panel.add(idLabel); 164 | panel.add(idField); 165 | 166 | int result = JOptionPane.showConfirmDialog(null, panel, "Delete Teacher", JOptionPane.OK_CANCEL_OPTION); 167 | if (result == JOptionPane.OK_OPTION) { 168 | String teacherId = idField.getText(); 169 | 170 | try { 171 | DataManager.deleteTeacher(teacherId); 172 | JOptionPane.showMessageDialog(null, "Teacher deleted successfully"); 173 | refreshTable(students); 174 | 175 | } catch (SQLException e1) { 176 | System.out.println(e1.getMessage()); 177 | JOptionPane.showMessageDialog(null, "Error deleting teacher", e1.getMessage(), 178 | JOptionPane.ERROR_MESSAGE); 179 | } 180 | } 181 | 182 | } 183 | }); 184 | removeBtnLbl.setBounds(1036, 47, 45, 45); 185 | 186 | JLabel addLbl = new JLabel("Add"); 187 | addLbl.setHorizontalAlignment(SwingConstants.CENTER); 188 | addLbl.setBounds(846, 103, 61, 16); 189 | 190 | JLabel editLbl = new JLabel("Edit"); 191 | editLbl.setHorizontalAlignment(SwingConstants.CENTER); 192 | editLbl.setBounds(931, 104, 61, 16); 193 | 194 | JLabel removeLbl = new JLabel("Remove"); 195 | removeLbl.setHorizontalAlignment(SwingConstants.CENTER); 196 | removeLbl.setBounds(1029, 103, 61, 16); 197 | 198 | JLabel assignBtnLbl = new JLabel(new ImageIcon(Teachers.class.getResource("/resources/assign.png"))); 199 | assignBtnLbl.addMouseListener(new MouseAdapter() { 200 | @Override 201 | public void mouseClicked(MouseEvent e) { 202 | JLabel idLabel = new JLabel("Enter teacher id: "); 203 | JTextField idField = new JTextField(); 204 | JLabel moduleIdLabel = new JLabel("Enter module id: "); 205 | JTextField moduleIdField = new JTextField(); 206 | 207 | JPanel panel = new JPanel(new BorderLayout()); 208 | JPanel inputPanel = new JPanel(new GridLayout(3, 2)); 209 | 210 | ButtonGroup group = new ButtonGroup(); 211 | 212 | JRadioButton assignRadioButton = new JRadioButton("Assign"); 213 | JRadioButton unassignRadioButton = new JRadioButton("Unassign"); 214 | 215 | group.add(assignRadioButton); 216 | group.add(unassignRadioButton); 217 | 218 | inputPanel.add(assignRadioButton); 219 | inputPanel.add(unassignRadioButton); 220 | 221 | inputPanel.add(idLabel); 222 | inputPanel.add(idField); 223 | inputPanel.add(moduleIdLabel); 224 | inputPanel.add(moduleIdField); 225 | 226 | String[] columnNames = { "Module ID", "Module Name" }; 227 | 228 | // Get the data for the table by calling a method or using a database 229 | ArrayList modules = DataRetriever.getAllModules(); 230 | 231 | int rowCount = modules.size(); 232 | int columnCount = 2; 233 | 234 | Object[][] data = new Object[rowCount][columnCount]; 235 | 236 | for (int i = 0; i < rowCount; i++) { 237 | data[i][0] = modules.get(i).getId(); 238 | data[i][1] = modules.get(i).getName(); 239 | } 240 | 241 | JTable modulesTable = new JTable(data, columnNames); 242 | JScrollPane scrollPane = new JScrollPane(modulesTable); 243 | 244 | panel.add(inputPanel, BorderLayout.NORTH); 245 | panel.add(scrollPane, BorderLayout.CENTER); 246 | 247 | int result = JOptionPane.showConfirmDialog(null, panel, "Assign Teacher", JOptionPane.OK_CANCEL_OPTION); 248 | if (result == JOptionPane.OK_OPTION) { 249 | if (assignRadioButton.isSelected()) { 250 | // Assign button is selected 251 | String teacherId = idField.getText().trim(); 252 | String moduleId = moduleIdField.getText().trim(); 253 | try { 254 | DataManager.assignModulesToTeacher(teacherId, moduleId); 255 | JOptionPane.showMessageDialog(null, "Teacher assigned successfully"); 256 | refreshTable(students); 257 | } catch (SQLException e1) { 258 | JOptionPane.showMessageDialog(null, "Error assigning teacher", e1.getMessage(), 259 | JOptionPane.ERROR_MESSAGE); 260 | } 261 | } else if (unassignRadioButton.isSelected()) { 262 | // Unassign button is selected 263 | String teacherId = idField.getText().trim(); 264 | String moduleId = moduleIdField.getText().trim(); 265 | try { 266 | DataManager.unassignModulesFromTeacher(teacherId, moduleId); 267 | JOptionPane.showMessageDialog(null, "Teacher unassigned successfully"); 268 | refreshTable(students); 269 | } catch (SQLException e1) { 270 | JOptionPane.showMessageDialog(null, "Error unassigning teacher", e1.getMessage(), 271 | JOptionPane.ERROR_MESSAGE); 272 | } 273 | } 274 | } 275 | } 276 | }); 277 | assignBtnLbl.setBounds(771, 53, 45, 45); 278 | 279 | JLabel assignLbl = new JLabel("Assign"); 280 | assignLbl.setHorizontalAlignment(SwingConstants.CENTER); 281 | assignLbl.setBounds(765, 103, 61, 16); 282 | 283 | if (user instanceof Admin) { 284 | students.add(addBtnLbl); 285 | students.add(editBtnLbl); 286 | students.add(removeBtnLbl); 287 | students.add(addLbl); 288 | students.add(editLbl); 289 | students.add(removeLbl); 290 | students.add(assignLbl); 291 | students.add(assignBtnLbl); 292 | } 293 | 294 | createTable(students); 295 | 296 | } 297 | 298 | private void createTable(JPanel students) { 299 | scrollPane = new JScrollPane(); 300 | scrollPane.setBounds(0, 185, 1138, 454); 301 | students.add(scrollPane); 302 | 303 | ArrayList teachersData = DataRetriever.getTeachers(); 304 | 305 | DefaultTableModel model = new DefaultTableModel(); 306 | model.setColumnIdentifiers(new String[] { "ID", "Name", "Phone Number", "Email Address", "Modules Taught" }); 307 | for (Teacher teacherData : teachersData) { 308 | model.addRow(new Object[] { teacherData.getId(), teacherData.getName(), teacherData.getPhone(), 309 | teacherData.getEmail(), 310 | teacherData.getModuleString() }); 311 | } 312 | JTable table_1 = new JTable(model); 313 | table_1.setEnabled(false); 314 | scrollPane.setViewportView(table_1); 315 | 316 | } 317 | 318 | private void refreshTable(JPanel students) { 319 | students.remove(scrollPane); 320 | createTable(students); 321 | students.revalidate(); 322 | students.repaint(); 323 | } 324 | 325 | // Singleton to ensure one and only instance 326 | public static Teachers getInstance(JPanel main, SystemUser user) { 327 | if (instance == null) { 328 | instance = new Teachers(main, user); 329 | } 330 | return instance; 331 | } 332 | 333 | public static void dispose() { 334 | instance = null; 335 | } 336 | } 337 | -------------------------------------------------------------------------------- /src/pages/Dashboard.java: -------------------------------------------------------------------------------- 1 | package pages; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.Color; 5 | import java.awt.Font; 6 | import java.awt.GridLayout; 7 | import java.awt.event.ActionEvent; 8 | import java.awt.event.ActionListener; 9 | import java.awt.event.MouseAdapter; 10 | import java.awt.event.MouseEvent; 11 | import java.sql.SQLException; 12 | import java.text.SimpleDateFormat; 13 | import java.util.ArrayList; 14 | import java.util.Calendar; 15 | 16 | import javax.swing.DefaultListModel; 17 | import javax.swing.ImageIcon; 18 | import javax.swing.JButton; 19 | import javax.swing.JFrame; 20 | import javax.swing.JLabel; 21 | import javax.swing.JList; 22 | import javax.swing.JOptionPane; 23 | import javax.swing.JPanel; 24 | import javax.swing.JScrollPane; 25 | import javax.swing.JSeparator; 26 | import javax.swing.JTable; 27 | import javax.swing.JTextField; 28 | import javax.swing.SwingConstants; 29 | 30 | import models.course.Course; 31 | import models.user.Student; 32 | import models.user.SystemUser; 33 | import models.user.Teacher; 34 | import util.CellRenderer; 35 | import util.CustomImage; 36 | import util.DataManager; 37 | import util.DataRetriever; 38 | 39 | class Dashboard extends JPanel { 40 | 41 | private static final long serialVersionUID = -3421670490444154816L; 42 | private JTextField textField; 43 | 44 | public Dashboard(JFrame frame, SystemUser user, JPanel login) { 45 | System.out.println(user.getRole()); 46 | JPanel main = new JPanel(); 47 | 48 | main.setBackground(new Color(238, 238, 238)); 49 | main.setBounds(0, -7, 1480, 701); 50 | frame.getContentPane().add(main); 51 | main.setVisible(true); 52 | main.setLayout(null); 53 | CustomImage roundLogo = new CustomImage("/resources/logo_round.png"); 54 | 55 | Calendar cal = Calendar.getInstance(); 56 | SimpleDateFormat sdf = new SimpleDateFormat("EEEE, MMM dd"); 57 | 58 | JPanel sidebar = new JPanel(); 59 | sidebar.setBackground(new Color(255, 255, 255)); 60 | sidebar.setBounds(0, 0, 134, 701); 61 | main.add(sidebar); 62 | sidebar.setLayout(null); 63 | JLabel logoImg = new JLabel(roundLogo.getImage(50, 50)); 64 | logoImg.setBounds(38, 41, 50, 50); 65 | sidebar.add(logoImg); 66 | 67 | JButton btnNewButton = new JButton("Logout"); 68 | btnNewButton.addActionListener(new ActionListener() { 69 | public void actionPerformed(ActionEvent e) { 70 | Courses.dispose(); 71 | Students.dispose(); 72 | Teachers.dispose(); 73 | login.setVisible(true); 74 | main.setVisible(false); 75 | } 76 | }); 77 | btnNewButton.setFont(new Font("Futura", Font.PLAIN, 15)); 78 | btnNewButton.setBounds(6, 634, 122, 37); 79 | sidebar.add(btnNewButton); 80 | 81 | JPanel coursesSidebar = new JPanel(); 82 | coursesSidebar.setBackground(new Color(255, 255, 255)); 83 | coursesSidebar.setBounds(15, 160, 109, 37); 84 | sidebar.add(coursesSidebar); 85 | coursesSidebar.setLayout(null); 86 | 87 | JLabel coursesLogo = new JLabel(new ImageIcon(getClass().getResource("/resources/courses.png"))); 88 | coursesLogo.setBounds(0, 0, 35, 35); 89 | coursesSidebar.add(coursesLogo); 90 | 91 | JLabel coursesLbl = new JLabel("Courses"); 92 | coursesLbl.setBounds(34, 0, 75, 37); 93 | coursesSidebar.add(coursesLbl); 94 | coursesLbl.setHorizontalAlignment(SwingConstants.CENTER); 95 | coursesLbl.setFont(new Font("Futura", Font.PLAIN, 15)); 96 | 97 | JPanel studentsSidebar = new JPanel(); 98 | 99 | studentsSidebar.setBackground(new Color(255, 255, 255)); 100 | studentsSidebar.setBounds(15, 240, 109, 37); 101 | sidebar.add(studentsSidebar); 102 | studentsSidebar.setLayout(null); 103 | 104 | JLabel studentsLogo = new JLabel(new ImageIcon(getClass().getResource("/resources/student.png"))); 105 | studentsLogo.setBounds(0, 0, 35, 35); 106 | studentsSidebar.add(studentsLogo); 107 | 108 | JLabel studentsLbl = new JLabel("Students"); 109 | studentsLbl.setBounds(30, 0, 79, 37); 110 | studentsSidebar.add(studentsLbl); 111 | studentsLbl.setHorizontalAlignment(SwingConstants.CENTER); 112 | studentsLbl.setFont(new Font("Futura", Font.PLAIN, 15)); 113 | 114 | JPanel teachersSidebar = new JPanel(); 115 | teachersSidebar.setBackground(new Color(255, 255, 255)); 116 | teachersSidebar.setBounds(15, 320, 109, 37); 117 | sidebar.add(teachersSidebar); 118 | teachersSidebar.setLayout(null); 119 | 120 | JLabel teachersLogo = new JLabel(new ImageIcon(getClass().getResource("/resources/teacher.png"))); 121 | teachersLogo.setBounds(0, 0, 35, 35); 122 | teachersSidebar.add(teachersLogo); 123 | 124 | JLabel teacherLbl = new JLabel("Teachers"); 125 | teacherLbl.setBounds(30, 0, 79, 37); 126 | teachersSidebar.add(teacherLbl); 127 | teacherLbl.setHorizontalAlignment(SwingConstants.CENTER); 128 | teacherLbl.setFont(new Font("Futura", Font.PLAIN, 15)); 129 | 130 | JPanel settingsSidebar = new JPanel(); 131 | settingsSidebar.setBackground(new Color(255, 255, 255)); 132 | settingsSidebar.setBounds(15, 590, 109, 37); 133 | sidebar.add(settingsSidebar); 134 | settingsSidebar.setLayout(null); 135 | 136 | JLabel settingsLogo = new JLabel(new ImageIcon(getClass().getResource("/resources/settings.png"))); 137 | settingsLogo.setBounds(0, 0, 35, 35); 138 | settingsSidebar.add(settingsLogo); 139 | 140 | JLabel settingsLbl = new JLabel("Settings"); 141 | settingsLbl.setBounds(30, 0, 79, 37); 142 | settingsSidebar.add(settingsLbl); 143 | settingsLbl.setHorizontalAlignment(SwingConstants.CENTER); 144 | settingsLbl.setFont(new Font("Futura", Font.PLAIN, 15)); 145 | 146 | JSeparator separator = new JSeparator(); 147 | separator.setBounds(6, 112, 122, 12); 148 | sidebar.add(separator); 149 | 150 | JPanel dashboard = new JPanel(); 151 | dashboard.setBounds(133, 0, 1341, 701); 152 | main.add(dashboard); 153 | dashboard.setLayout(null); 154 | 155 | JLabel title = new JLabel("Welcome back, " + user.getName() + " ! 👋"); 156 | title.setFont(new Font("Futura", Font.PLAIN, 20)); 157 | title.setBounds(58, 39, 449, 37); 158 | dashboard.add(title); 159 | 160 | JLabel subtitle = new JLabel(sdf.format(cal.getTime())); 161 | subtitle.setFont(new Font("Futura", Font.PLAIN, 15)); 162 | subtitle.setEnabled(false); 163 | subtitle.setBounds(58, 71, 449, 21); 164 | dashboard.add(subtitle); 165 | 166 | JPanel searchBar = new JPanel(); 167 | searchBar.setLayout(null); 168 | searchBar.setBackground(Color.WHITE); 169 | searchBar.setBounds(806, 39, 418, 45); 170 | dashboard.add(searchBar); 171 | 172 | JLabel searchIcon = new JLabel(new ImageIcon(getClass().getResource("/resources/search_icon.png"))); 173 | searchIcon.setBounds(382, 0, 30, 45); 174 | searchBar.add(searchIcon); 175 | 176 | textField = new JTextField(30); 177 | textField.setFont(new Font("Futura", Font.PLAIN, 15)); 178 | textField.setBorder(null); 179 | textField.setBackground(Color.WHITE); 180 | textField.setBounds(21, 0, 360, 45); 181 | searchBar.add(textField); 182 | 183 | JLabel title2 = new JLabel("Available Courses"); 184 | title2.setFont(new Font("Futura", Font.PLAIN, 20)); 185 | title2.setBounds(58, 218, 280, 37); 186 | dashboard.add(title2); 187 | 188 | JPanel coursesPanel = new JPanel(); 189 | coursesPanel.setBackground(new Color(252, 255, 255)); 190 | coursesPanel.setBounds(58, 267, 676, 370); 191 | dashboard.add(coursesPanel); 192 | 193 | JLabel lblTeachers = new JLabel("Teachers"); 194 | lblTeachers.setFont(new Font("Futura", Font.PLAIN, 20)); 195 | lblTeachers.setBounds(806, 218, 280, 37); 196 | dashboard.add(lblTeachers); 197 | 198 | JPanel teachersPanel = new JPanel(); 199 | teachersPanel.setBackground(Color.WHITE); 200 | teachersPanel.setBounds(806, 268, 418, 369); 201 | dashboard.add(teachersPanel); 202 | 203 | DefaultListModel model = new DefaultListModel(); 204 | model.addAll(DataRetriever.getCourses()); 205 | coursesPanel.setLayout(new BorderLayout(0, 0)); 206 | JList courses = new JList(model); 207 | courses.setCellRenderer(new CellRenderer()); 208 | courses.setBorder(null); 209 | courses.setBounds(0, 0, 418, 365); 210 | JScrollPane scrollPane = new JScrollPane(courses); 211 | coursesPanel.add(scrollPane); 212 | 213 | DefaultListModel teacherModel = new DefaultListModel(); 214 | teacherModel.addAll(DataRetriever.getTeachers()); 215 | JList teachers = new JList(teacherModel); 216 | teachers.setCellRenderer(new CellRenderer()); 217 | teachers.setBorder(null); 218 | teachers.setBounds(0, 0, 418, 365); 219 | JScrollPane teacherScrollPane = new JScrollPane(teachers); 220 | scrollPane.setBounds(0, 0, 418, 369); 221 | teachersPanel.setLayout(new BorderLayout(0, 0)); 222 | teachersPanel.add(teacherScrollPane); 223 | 224 | if (user instanceof Student) { 225 | Student student = (Student) user; 226 | JButton viewResultBtn = new JButton("View Your Result"); 227 | viewResultBtn.addActionListener(new ActionListener() { 228 | public void actionPerformed(ActionEvent e) { 229 | JPanel panel = new JPanel(new GridLayout(1, 2)); 230 | JLabel idLabel = new JLabel("Enter module id: "); 231 | JTextField idField = new JTextField(); 232 | 233 | JLabel marksLabel = new JLabel(""); 234 | 235 | panel.add(idLabel); 236 | panel.add(idField); 237 | 238 | panel.add(marksLabel); 239 | 240 | int result = JOptionPane.showConfirmDialog(null, panel, "View Results", 241 | JOptionPane.OK_CANCEL_OPTION); 242 | if (result == JOptionPane.OK_OPTION) { 243 | String moduleId = idField.getText(); 244 | 245 | try { 246 | int marks = DataManager.retrieveMarks(student.getId(), moduleId); 247 | String res = "You have scored " + marks + " marks in " 248 | + DataRetriever.getModuleById(Integer.parseInt(moduleId)).getName(); 249 | JOptionPane.showMessageDialog(null, res, "Your Result", JOptionPane.INFORMATION_MESSAGE); 250 | 251 | } catch (SQLException e1) { 252 | 253 | JOptionPane.showMessageDialog(null, "Results not available for the module", 254 | e1.getMessage(), 255 | JOptionPane.ERROR_MESSAGE); 256 | } 257 | } 258 | } 259 | }); 260 | viewResultBtn.setBounds(637, 39, 136, 45); 261 | dashboard.add(viewResultBtn); 262 | } 263 | 264 | if (user instanceof Student) { 265 | Student student = (Student) user; 266 | String enrollmentStatus = student.getEnrolledCourse() == null ? "Not Enrolled" 267 | : student.getEnrolledCourse().getName(); 268 | JLabel EnrollmentStatusLbl = new JLabel("Enrollment Status: " + enrollmentStatus); 269 | EnrollmentStatusLbl.setFont(new Font("Futura", Font.PLAIN, 20)); 270 | EnrollmentStatusLbl.setBounds(58, 150, 1200, 16); 271 | dashboard.add(EnrollmentStatusLbl); 272 | 273 | if (student.getEnrolledCourse() == null) { 274 | JButton enrollBtn = new JButton("Enroll Now"); 275 | enrollBtn.addActionListener(new ActionListener() { 276 | public void actionPerformed(ActionEvent e) { 277 | int studentId = student.getId(); 278 | JLabel courseIdLabel = new JLabel("Enter course id: "); 279 | JTextField courseIdField = new JTextField(); 280 | JLabel phoneLabel = new JLabel("Enter phone number: "); 281 | JTextField phoneField = new JTextField(); 282 | 283 | JPanel panel = new JPanel(new BorderLayout()); 284 | JPanel inputPanel = new JPanel(new GridLayout(3, 2)); 285 | 286 | inputPanel.add(courseIdLabel); 287 | inputPanel.add(courseIdField); 288 | 289 | inputPanel.add(phoneLabel); 290 | inputPanel.add(phoneField); 291 | 292 | String[] columnNames = { "Course ID", "Course Name" }; 293 | 294 | ArrayList courses = DataRetriever.getCourses(); 295 | 296 | int rowCount = courses.size(); 297 | int columnCount = 2; 298 | 299 | Object[][] data = new Object[rowCount][columnCount]; 300 | 301 | for (int i = 0; i < rowCount; i++) { 302 | data[i][0] = courses.get(i).getId(); 303 | data[i][1] = courses.get(i).getName(); 304 | } 305 | 306 | JTable modulesTable = new JTable(data, columnNames); 307 | JScrollPane scrollPane = new JScrollPane(modulesTable); 308 | 309 | panel.add(inputPanel, BorderLayout.NORTH); 310 | panel.add(scrollPane, BorderLayout.CENTER); 311 | 312 | int result = JOptionPane.showConfirmDialog(null, panel, "Assign Teacher", 313 | JOptionPane.OK_CANCEL_OPTION); 314 | if (result == JOptionPane.OK_OPTION) { 315 | int courseId = Integer.parseInt(courseIdField.getText()); 316 | try { 317 | DataManager.enrollStudent(studentId, courseId, phoneField.getText(), student); 318 | JOptionPane.showMessageDialog(null, "Student enrolled successfully"); 319 | student.setEnrolledCourse(DataRetriever.getCourses(courseId)); 320 | EnrollmentStatusLbl 321 | .setText("Enrollment Status: " + student.getEnrolledCourse().getName()); 322 | 323 | } catch (SQLException e1) { 324 | System.out.println(e1.getMessage()); 325 | JOptionPane.showMessageDialog(null, "Error enrolling student", null, 326 | JOptionPane.ERROR_MESSAGE); 327 | } 328 | } 329 | } 330 | }); 331 | enrollBtn.setBounds(48, 181, 117, 29); 332 | dashboard.add(enrollBtn); 333 | 334 | } 335 | } 336 | 337 | logoImg.addMouseListener(new MouseAdapter() { 338 | @Override 339 | public void mouseClicked(MouseEvent e) { 340 | dashboard.setVisible(true); 341 | } 342 | }); 343 | 344 | coursesSidebar.addMouseListener(new MouseAdapter() { 345 | @Override 346 | public void mouseClicked(MouseEvent e) { 347 | Courses.getInstance(main, user).setVisible(true); 348 | Students.getInstance(main, user).setVisible(false); 349 | Teachers.getInstance(main, user).setVisible(false); 350 | dashboard.setVisible(false); 351 | } 352 | }); 353 | 354 | studentsSidebar.addMouseListener(new MouseAdapter() { 355 | @Override 356 | public void mouseClicked(MouseEvent e) { 357 | Courses.getInstance(main, user).setVisible(false); 358 | Teachers.getInstance(main, user).setVisible(false); 359 | Students.getInstance(main, user).setVisible(true); 360 | dashboard.setVisible(false); 361 | } 362 | }); 363 | 364 | teachersSidebar.addMouseListener(new MouseAdapter() { 365 | @Override 366 | public void mouseClicked(MouseEvent e) { 367 | Courses.getInstance(main, user).setVisible(false); 368 | Students.getInstance(main, user).setVisible(false); 369 | Teachers.getInstance(main, user).setVisible(true); 370 | dashboard.setVisible(false); 371 | } 372 | }); 373 | 374 | } 375 | } 376 | -------------------------------------------------------------------------------- /src/util/DataManager.java: -------------------------------------------------------------------------------- 1 | package util; 2 | 3 | import java.sql.PreparedStatement; 4 | import java.sql.ResultSet; 5 | import java.sql.SQLException; 6 | import java.sql.Statement; 7 | import java.util.ArrayList; 8 | 9 | import models.user.Student; 10 | 11 | public class DataManager { 12 | private static DatabaseManager db; 13 | 14 | public DataManager() throws SQLException { 15 | db = DatabaseManager.getInstance(); 16 | } 17 | 18 | public static void insert(String table, String[] columns, String[] values) throws SQLException { 19 | String sql = "INSERT INTO " + table + " ("; 20 | for (int i = 0; i < columns.length; i++) { 21 | sql += columns[i]; 22 | if (i < columns.length - 1) { 23 | sql += ", "; 24 | } 25 | } 26 | sql += ") VALUES ("; 27 | for (int i = 0; i < values.length; i++) { 28 | sql += "?"; 29 | if (i < values.length - 1) { 30 | sql += ", "; 31 | } 32 | } 33 | sql += ")"; 34 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 35 | for (int i = 0; i < values.length; i++) { 36 | ps.setString(i + 1, values[i]); 37 | } 38 | ps.executeUpdate(); 39 | } 40 | 41 | public static void deleteCourse(String id) throws SQLException { 42 | String sql = "DELETE FROM courses WHERE course_id = ?"; 43 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 44 | ps.setInt(1, Integer.parseInt(id)); 45 | ps.executeUpdate(); 46 | } 47 | 48 | public static void editCourse(String id, String name) throws SQLException { 49 | String sql = "UPDATE courses SET course_name = ? WHERE course_id = ?"; 50 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 51 | ps.setString(1, name); 52 | ps.setInt(2, Integer.parseInt(id)); 53 | ps.executeUpdate(); 54 | } 55 | 56 | public static void addTeacher(String name, String phoneNumber, String email, String password) throws SQLException { 57 | String insertAuth = "INSERT INTO auth (name, email, password, role) VALUES (?, ?, ?, 'Teacher')"; 58 | PreparedStatement stmtAuth; 59 | 60 | stmtAuth = db.getConnection().prepareStatement(insertAuth, Statement.RETURN_GENERATED_KEYS); 61 | stmtAuth.setString(1, name); 62 | stmtAuth.setString(2, email); 63 | stmtAuth.setString(3, password); 64 | stmtAuth.executeUpdate(); 65 | 66 | ResultSet generatedKeys = stmtAuth.getGeneratedKeys(); 67 | int authId = -1; 68 | if (generatedKeys.next()) { 69 | authId = generatedKeys.getInt(1); 70 | } 71 | 72 | String insertTeacher = "INSERT INTO teachers (teacher_name, teacher_phone, auth_id) VALUES (?, ?, ?)"; 73 | PreparedStatement stmtTeacher = db.getConnection().prepareStatement(insertTeacher); 74 | stmtTeacher.setString(1, name); 75 | stmtTeacher.setString(2, phoneNumber); 76 | stmtTeacher.setInt(3, authId); 77 | stmtTeacher.executeUpdate(); 78 | } 79 | 80 | public static void editTeacher(String id, String name, String phoneNumber, String email, String password) 81 | throws SQLException { 82 | try { 83 | int teacherId = Integer.parseInt(id); 84 | 85 | // Update information in the teachers table 86 | StringBuilder updateTeacherQuery = new StringBuilder("UPDATE teachers SET "); 87 | ArrayList updates = new ArrayList<>(); 88 | ArrayList updateParams = new ArrayList<>(); 89 | 90 | if (name != null && !name.equals("")) { 91 | updates.add("teacher_name = ?"); 92 | updateParams.add(name); 93 | } 94 | 95 | if (phoneNumber != null && !phoneNumber.equals("")) { 96 | updates.add("teacher_phone = ?"); 97 | updateParams.add(phoneNumber); 98 | } 99 | 100 | if (updates.size() > 0) { 101 | updateTeacherQuery.append(String.join(", ", updates)); 102 | updateTeacherQuery.append(" WHERE teacher_id = ?"); 103 | PreparedStatement updateTeacher = db.getConnection().prepareStatement(updateTeacherQuery.toString()); 104 | 105 | for (int i = 0; i < updateParams.size(); i++) { 106 | updateTeacher.setString(i + 1, updateParams.get(i)); 107 | } 108 | updateTeacher.setInt(updateParams.size() + 1, teacherId); 109 | updateTeacher.executeUpdate(); 110 | } 111 | 112 | // Update information in the auth table 113 | StringBuilder updateAuthQuery = new StringBuilder("UPDATE auth SET "); 114 | updates.clear(); 115 | updateParams.clear(); 116 | 117 | if (email != null && !email.equals("")) { 118 | updates.add("email = ?"); 119 | updateParams.add(email); 120 | } 121 | 122 | if (password != null && !password.equals("")) { 123 | updates.add("password = ?"); 124 | updateParams.add(password); 125 | } 126 | 127 | if (updates.size() > 0) { 128 | updateAuthQuery.append(String.join(", ", updates)); 129 | updateAuthQuery.append(" WHERE id = (SELECT auth_id FROM teachers WHERE teacher_id = ?)"); 130 | PreparedStatement updateAuth = db.getConnection().prepareStatement(updateAuthQuery.toString()); 131 | 132 | for (int i = 0; i < updateParams.size(); i++) { 133 | updateAuth.setString(i + 1, updateParams.get(i)); 134 | } 135 | updateAuth.setInt(updateParams.size() + 1, teacherId); 136 | updateAuth.executeUpdate(); 137 | } 138 | } catch (SQLException e) { 139 | e.printStackTrace(); 140 | throw e; 141 | } 142 | } 143 | 144 | public static void assignModulesToTeacher(String teacherId, String moduleId) throws SQLException { 145 | String sql = "INSERT INTO teachers_modules (teacher_id, module_id) VALUES (?, ?)"; 146 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 147 | ps.setInt(1, Integer.parseInt(teacherId)); 148 | ps.setInt(2, Integer.parseInt(moduleId)); 149 | ps.executeUpdate(); 150 | } 151 | 152 | public static void unassignModulesFromTeacher(String teacherId, String moduleId) throws SQLException { 153 | String sql = "DELETE FROM teachers_modules WHERE teacher_id = ? AND module_id = ?"; 154 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 155 | ps.setInt(1, Integer.parseInt(teacherId)); 156 | ps.setInt(2, Integer.parseInt(moduleId)); 157 | ps.executeUpdate(); 158 | } 159 | 160 | public static void deleteTeacher(String id) throws SQLException { 161 | 162 | String sql = "SELECT auth_id FROM teachers WHERE teacher_id = ?"; 163 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 164 | ps.setInt(1, Integer.parseInt(id)); 165 | ResultSet rs = ps.executeQuery(); 166 | rs.next(); 167 | int authId = rs.getInt("auth_id"); 168 | 169 | sql = "DELETE FROM teachers WHERE teacher_id = ?"; 170 | ps = db.getConnection().prepareStatement(sql); 171 | ps.setInt(1, Integer.parseInt(id)); 172 | ps.executeUpdate(); 173 | 174 | sql = "DELETE FROM auth WHERE id = ?"; 175 | ps = db.getConnection().prepareStatement(sql); 176 | ps.setInt(1, authId); 177 | ps.executeUpdate(); 178 | } 179 | 180 | public static void enrollStudent(int studentId, int courseId, String phoneNumber, Student student) 181 | throws SQLException { 182 | String sql = "INSERT INTO students (student_id, student_name, student_phone, course_id, auth_id, level) VALUES (?, ?, ?, ?, ?, 4)"; 183 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 184 | ps.setInt(1, studentId); 185 | ps.setString(2, student.getName()); 186 | ps.setString(3, phoneNumber); 187 | student.setPhone(phoneNumber); 188 | ps.setInt(4, courseId); 189 | ps.setInt(5, studentId); 190 | ps.executeUpdate(); 191 | 192 | sql = "INSERT INTO enrollments (student_id, course_id) VALUES (?, ?)"; 193 | ps = db.getConnection().prepareStatement(sql); 194 | ps.setInt(1, studentId); 195 | ps.setInt(2, courseId); 196 | ps.executeUpdate(); 197 | } 198 | 199 | public static void addStudent(String name, String phoneNumber, String email, String password, int courseId, 200 | int level) 201 | throws SQLException { 202 | String insertAuth = "INSERT INTO auth (name, email, password, role) VALUES (?, ?, ?, 'Student')"; 203 | PreparedStatement stmtAuth; 204 | 205 | stmtAuth = db.getConnection().prepareStatement(insertAuth, Statement.RETURN_GENERATED_KEYS); 206 | stmtAuth.setString(1, name); 207 | stmtAuth.setString(2, email); 208 | stmtAuth.setString(3, password); 209 | stmtAuth.executeUpdate(); 210 | 211 | ResultSet generatedKeys = stmtAuth.getGeneratedKeys(); 212 | int authId = -1; 213 | if (generatedKeys.next()) { 214 | authId = generatedKeys.getInt(1); 215 | } 216 | 217 | String insertStudent = "INSERT INTO students (student_name, student_phone, course_id, level, auth_id) VALUES (?, ?, ?, ?, ?)"; 218 | PreparedStatement stmtStudent = db.getConnection().prepareStatement(insertStudent); 219 | stmtStudent.setString(1, name); 220 | stmtStudent.setString(2, phoneNumber); 221 | stmtStudent.setInt(3, courseId); 222 | stmtStudent.setInt(4, level); 223 | stmtStudent.setInt(5, authId); 224 | stmtStudent.executeUpdate(); 225 | } 226 | 227 | public static void editStudent(String id, String name, String phoneNumber, String email, String password) 228 | throws SQLException { 229 | try { 230 | // Update information in the students table 231 | StringBuilder updateStudentQuery = new StringBuilder("UPDATE students SET "); 232 | ArrayList updates = new ArrayList<>(); 233 | ArrayList updateParams = new ArrayList<>(); 234 | 235 | if (name != null && !name.equals("")) { 236 | updates.add("student_name = ?"); 237 | updateParams.add(name); 238 | } 239 | 240 | if (phoneNumber != null && !phoneNumber.equals("")) { 241 | updates.add("student_phone = ?"); 242 | updateParams.add(phoneNumber); 243 | } 244 | 245 | if (updates.size() > 0) { 246 | updateStudentQuery.append(String.join(", ", updates)); 247 | updateStudentQuery.append(" WHERE student_id = ?"); 248 | PreparedStatement updateStudent = db.getConnection().prepareStatement(updateStudentQuery.toString()); 249 | 250 | for (int i = 0; i < updateParams.size(); i++) { 251 | updateStudent.setString(i + 1, updateParams.get(i)); 252 | } 253 | updateStudent.setInt(updateParams.size() + 1, Integer.parseInt(id)); 254 | updateStudent.executeUpdate(); 255 | } 256 | 257 | // Update information in the auth table 258 | StringBuilder updateAuthQuery = new StringBuilder("UPDATE auth SET "); 259 | updates.clear(); 260 | updateParams.clear(); 261 | 262 | if (email != null && !email.equals("")) { 263 | updates.add("email = ?"); 264 | updateParams.add(email); 265 | } 266 | 267 | if (password != null && !password.equals("")) { 268 | updates.add("password = ?"); 269 | updateParams.add(password); 270 | } 271 | 272 | if (updates.size() > 0) { 273 | updateAuthQuery.append(String.join(", ", updates)); 274 | updateAuthQuery.append(" WHERE id = (SELECT auth_id FROM students WHERE student_id = ?)"); 275 | PreparedStatement updateAuth = db.getConnection().prepareStatement(updateAuthQuery.toString()); 276 | 277 | for (int i = 0; i < updateParams.size(); i++) { 278 | updateAuth.setString(i + 1, updateParams.get(i)); 279 | } 280 | updateAuth.setInt(updateParams.size() + 1, Integer.parseInt(id)); 281 | updateAuth.executeUpdate(); 282 | } 283 | } catch (SQLException e) { 284 | e.printStackTrace(); 285 | throw e; 286 | } 287 | } 288 | 289 | public static void deleteStudent(String id) throws SQLException { 290 | String sql = "SELECT auth_id FROM students WHERE student_id = ?"; 291 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 292 | ps.setInt(1, Integer.parseInt(id)); 293 | ResultSet rs = ps.executeQuery(); 294 | rs.next(); 295 | int authId = rs.getInt("auth_id"); 296 | 297 | sql = "DELETE FROM students WHERE student_id = ?"; 298 | ps = db.getConnection().prepareStatement(sql); 299 | ps.setInt(1, Integer.parseInt(id)); 300 | ps.executeUpdate(); 301 | 302 | sql = "DELETE FROM auth WHERE id = ?"; 303 | ps = db.getConnection().prepareStatement(sql); 304 | ps.setInt(1, authId); 305 | ps.executeUpdate(); 306 | } 307 | 308 | public static void markStudent(String studentId, String moduleId, String marks) throws SQLException { 309 | String sql = "INSERT INTO results (student_id, module_id, marks) VALUES (?, ?, ?)"; 310 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 311 | ps.setInt(1, Integer.parseInt(studentId)); 312 | ps.setInt(2, Integer.parseInt(moduleId)); 313 | ps.setInt(3, Integer.parseInt(marks)); 314 | ps.executeUpdate(); 315 | } 316 | 317 | public static int retrieveMarks(int studentId, String moduleId) throws SQLException { 318 | String sql = "SELECT marks FROM results WHERE student_id = ? AND module_id = ?"; 319 | PreparedStatement ps = db.getConnection().prepareStatement(sql); 320 | ps.setInt(1, studentId); 321 | ps.setInt(2, Integer.parseInt(moduleId)); 322 | ResultSet rs = ps.executeQuery(); 323 | rs.next(); 324 | int marks = rs.getInt("marks"); 325 | return marks; 326 | } 327 | } 328 | -------------------------------------------------------------------------------- /UML_Diagram.ucls: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 15 | 16 | 18 | 19 | 20 | 21 | 22 | 24 | 25 | 27 | 28 | 29 | 30 | 31 | 33 | 34 | 36 | 37 | 38 | 39 | 40 | 42 | 43 | 45 | 46 | 47 | 48 | 49 | 51 | 52 | 54 | 55 | 56 | 57 | 58 | 60 | 61 | 63 | 64 | 65 | 66 | 67 | 69 | 70 | 72 | 73 | 74 | 75 | 76 | 78 | 79 | 81 | 82 | 83 | 84 | 85 | 87 | 88 | 90 | 91 | 92 | 93 | 94 | 96 | 97 | 99 | 100 | 101 | 102 | 103 | 105 | 106 | 108 | 109 | 110 | 111 | 112 | 114 | 115 | 117 | 118 | 119 | 120 | 121 | 123 | 124 | 126 | 127 | 128 | 129 | 130 | 132 | 133 | 135 | 136 | 137 | 138 | 139 | 141 | 142 | 144 | 145 | 146 | 147 | 148 | 150 | 151 | 153 | 154 | 155 | 156 | 157 | 159 | 160 | 162 | 163 | 164 | 165 | 166 | 168 | 169 | 171 | 172 | 173 | 174 | 175 | 177 | 178 | 180 | 181 | 182 | 183 | 184 | 186 | 187 | 189 | 190 | 191 | 192 | 193 | 195 | 196 | 198 | 199 | 200 | 201 | 202 | 204 | 205 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 298 | 299 | 300 | 301 | 302 | --------------------------------------------------------------------------------