OOP - StudentRegistration - MVC
OOP - StudentRegistration - MVC
a complete example
STUDENT REGISTRATION
Problem specifications
• You are required to design and implement a program to be used by a university registrar in the
objective of managing students, courses and registration.
• The program can be accessed by the registrar to register new students , activate/deactivate
already registered students, add instructors, show courses and students and also show the list
of registered students in a given course.
• Instructors can also access the program to create a course and enroll students to that course.
This last information about registration should be also saved in your program.
• Student is identified by his Id, name and active/inactive status.
• Instructor is identified by his Id, name and status (full-timer or part-timer)
• A course has a code and a title.
Design
STUDENT REGISTRATION
Student Registration
Create
Registrar student Create
course
Enroll
student Show
students
Instructor registered in
Activate/de
activate Show all a course
student students
Show
courses
Show all registered by
courses a student
Sequence Diagram
(many use cases together since they are simple)
Instructor Application Application Registrar
Login(ins,ins) Login(reg,reg)
open instructor page open registrar page
Cannot enroll students
before creating courses
Create course(code,title) and students
Create student(id,name)
Enroll student(code,id)
Show all students
Print all students
login[registrar]/
logged in Go to management Student
Logged in
management
Start
Un-logged Logged in
login[instructor]/
logged in
End
login[invalid] close
close
Activity Diagram(login)
Enter username and password
Invalid info
Valid login as instructor
yes
Last record?
Registration
- Code: String That’s it?
- ID:Integer
No, another main class to link the course with the students registered in it. Why a class?
+ constructor
+ setters
+ getters
Because a student can register in many courses and a course can be taken by many students ➔
+ toString where to put the link? In class student or class course?
That’s it?
Controller
+ methods of gui actions + methods of gui actions + methods of gui actions + methods of gui actions
Implementation
MVC AND CODE
MVC Definition
Model-View-Controller: an application design model comprised of three interconnected parts. They
include the model (data), the view (user interface), and the controller (processes that handle input).
A Model object stores the data for the domain you are modeling, along with the logic related to that
raw data. E.g an Employee class with the data: First name, Last name, social security number, Date of
birth, salary, etc.
A View: user interface (GUI) element that lets humans interact with the computer system. View
object can be data-entry forms, reports, graphs, etc.
Controller objects are the "coordinators". They are typically written to control/coordinate a sequence
of steps required to perform some business function.
MVC Allowed/Not allowed actions
We call this
way of
programs MVC
14
Usually capital letter
Data model
Four main model classes to be created: Student, Instructor, Course, Registration
15
package model;
String title;
Don’t forget the getters and setters and public Course(String code, String title) {
constructors with following signatures:
this.code = code;
public Course(String code, String title) this.title = title; }
→You can do this by right click anywhere inside the public String getCode() {
code, → insert code, return code;}
then → choose constructor, getter, setter
public String getTitel() {
return title;}
@Override
public void setCode(String code) {
public String toString(){
this.code = code;}
String s="";
public void setTitel(String title) {
s+="Code: "+ this.code +"\t";
this.title = title;}
s+="title: "+ this.title +"\t";
return s;
}
16
@Override
{
• It should be set to ‘1’ in the constructor.
s+="active student";
• ToString will be a bit different in this case: }
return s;
17
Controller Only one controller is used in our project for simplicity.
public ArrayList<Integer> getallIDs(){
import model.*; }
ArrayList<Student> allstudents; }
}
Controller Only one controller is used in our project for simplicity.
public void addCourse(String code, String title) { public String printRegCourses(int StudentID){
allinstructors.add(I); } out+="\n";
allregistrations.add(r); }
Controller Only one controller is used in our project for simplicity.
public String printRegstudents(String CourseCode){ public String printallstudents (){
if(allregistrations.get(i).getCode()==CourseCode){ str+=allstudents.get(i).toString()+"\n";
{ }
if(allstudents.get(j).getId()==id){
return str;
}
Creating GUIs
Each GUI corresponds to a class that extends JFrame class
21
String user = uname.getText();
{
public static regController controller;
InsForm ins = new InsForm();
public Login() {
ins.setTitle("Instructor");
initComponents();
ins.setVisible(true);
controller = new regController();
uname.setText("");
}
pw.setText("");
reg.setTitle("Registrar");
reg.setVisible(true);
uname.setText("");
pw.setText("");
Two users: ins,ins (instructor) and reg,reg(registrar)
Can we have more users? }
initComponents();
setDefaultCloseOperation(managestudent.DISPOSE_ON_CLOSE);
Ids =Login.controller.getallIDs();
idcombo.addItem(Ids.get(i).toString());
codes =Login.controller.getallCodes();
codecombo.addItem(codes.get(i));
}
Save data to files
PART2
Updates to be applied
➢ Replace the arraylists with files to have permanent storage (not destroyed when closing the program)
➢Three main files are needed, students, courses and registrations.
➢Add the class “fileAccess” to the controller package to handle file access.
◦ Handle writing into the files
◦ Handle reading from the files and return an array list to be used in the regcontroller as before.
➢Instead of using a public static variable (not a good practice), the regcontroller instant will be created
in each of the view frames. Its methods are to be called from each frame to read and write to files.
fileAccess Class
➢ Write into a file by passing the name of that file and a string to write it.
public void writeToFile(String fileName, String str) {
try {
stdinf.write(str);
stdinf.close();
catch(IOException e) { }
return allStudents;}
public class regController {
fileAccess fA;
public regController() {
➢The new RegController class will not have any arraylist. It will have only a file access object to
handle files.
public void addStudent(int id, String name) {
➢Now to add any element, we need to add it to the file
Student s = new Student(id, name);
fA.writeToFile("students", str);}
➢Then, to show, print or change we need to read from the files and add to a local array list (a
private variable inside the function and not a public one)
public ArrayList<String> getallCodes() {
allcourses = fA.readCourses();
out.add(allcourses.get(i).getCode());}
return out; }