0% found this document useful (0 votes)
20 views

OOP - StudentRegistration - MVC

The document describes an object-oriented system for student registration at a university. It includes use case diagrams, sequence diagrams, state diagrams, activity diagrams, and class diagrams. The system allows registrars to register students, activate/deactivate students, add instructors, and view courses and student registrations. Instructors can create courses and enroll students. Key classes include Student, Instructor, Course, and Registration. A Controller class manages interactions between the classes. The system also includes GUI classes to interact with users.

Uploaded by

areej.saad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

OOP - StudentRegistration - MVC

The document describes an object-oriented system for student registration at a university. It includes use case diagrams, sequence diagrams, state diagrams, activity diagrams, and class diagrams. The system allows registrars to register students, activate/deactivate students, add instructors, and view courses and student registrations. Instructors can create courses and enroll students. Key classes include Student, Instructor, Course, and Registration. A Controller class manages interactions between the classes. The system also includes GUI classes to interact with users.

Uploaded by

areej.saad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

OOP and UML diagrams

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

Use case Diagram login

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

Show all courses


Print all courses

Show students in a course(code)


print students register in curse
with code
Show courses of a student(ID)
print courses registered by
student with id
State Diagram close
close

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

Valid login as registrar

Display instructor page Display registrar page


Activity Diagram(show students registered in a course)
Enter course code C

get a registration record


no
course in this no
record = C?
yes
yes

Add the course to list L

yes
Last record?

Print the list L


Class Diagram
Three main classes: Student, Instructor and Course Student
Course Instructor - id: integer
- id: integer - name: string
- Code: String - active: char
These four classes are the
- Title: string - name: String
- status: String + constructor prototype that we usually
+ constructor
+ constructor + setters are able to re-use in any
+ setters + getters different project since they
+ setters
+ getters + toString
+ getters are generic
+ toString +activate
+ toString
+ deactivate

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

Class Diagram Students: a list of Students


Courses: a list of Courses
Instructors: a list of Instructors
Registrations: a list of Registrations
No, We need to control these classes by more specific classes to our problem.
When the project is large, more than one controller need to be created + constructor
(course controller, instructor controller, registration controller, etc. In our + getallIDs
example, one controller would be enough to instantiate the main classes and + getallCodes
+ addStudent
manipulate the different objects and their relations. + addCourse
+ addinstructor
+ registerStudent
+ printregcourse
+printstudentsInCourse
That’s it?
Finally, we have also the classes of the GUI to communicate with the users of the system directly

login Instructor frame Registrar frame Management frame


+ static 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

• Users interact with View objects


• View objects and Controller objects talk to each other
• Different Controller objects talk to each other
• Controller objects talk to Model objects
• No other forms of communication between objects
are allowed
Creating packages
New Java project without a main function
Right click on Source Packages -> New -> Java Package
◦ Create three packages:
◦ model
◦ view
◦ control

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;

public class Course {

Data model ( course ) String code;

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

public String toString() {

Data model ( student ) String s="";

• Any newly created student is supposed to be s+="id: "+ this.Id +"\t";


active. s+="name: "+ this.name +"\t";

• No need to pass active as a constructor argument. if (active=='1')

{
• It should be set to ‘1’ in the constructor.
s+="active student";
• ToString will be a bit different in this case: }

else {s+="non active student";}

return s;

17
Controller Only one controller is used in our project for simplicity.
public ArrayList<Integer> getallIDs(){

ArrayList<Integer> out = new ArrayList();

package controller; for(int i = 0; i< allstudents.size();i++){

import java.util.ArrayList; out.add(allstudents.get(i).getId());

import model.*; }

public class regController { return out;

ArrayList<Student> allstudents; }

ArrayList<Course> allcourses; public ArrayList<String> getallCodes(){

ArrayList<Instructor> allinstructors; ArrayList<String> out = new ArrayList();

ArrayList<Registration> allregistrations; for(int i = 0; i< allcourses.size();i++){

public regController() { out.add(allcourses.get(i).getCode());

allstudents = new ArrayList(); }

allcourses = new ArrayList(); return out;

allinstructors = new ArrayList(); }

allregistrations = new ArrayList();

}
Controller Only one controller is used in our project for simplicity.
public void addCourse(String code, String title) { public String printRegCourses(int StudentID){

Course c = new Course(code, title); String out = "";

allcourses.add(c);} for(int i = 0; i< allregistrations.size();i++){

public void addStudent(int ID, String name) { if(allregistrations.get(i).getID()==StudentID){

Student s = new Student(ID, name); String code= allregistrations.get(i).getCode();

allstudents.add(s); } for(int j = 0; j< allcourses.size(); j++)

public void addInstructor(int id, String name, String stat){ if(allcourses.get(j).getCode()==code){

Instructor I = new Instructor(id, name, stat); out+=(allcourses.get(j).toString());

allinstructors.add(I); } out+="\n";

public void addregistration(int ID, String code) { } }}}

Registration r = new Registration(ID, code); return out; }

allregistrations.add(r); }
Controller Only one controller is used in our project for simplicity.
public String printRegstudents(String CourseCode){ public String printallstudents (){

String out=""; String str="";

for(int i = 0; i< allregistrations.size();i++){ for(int i = 0; i< allstudents.size();i++){

if(allregistrations.get(i).getCode()==CourseCode){ str+=allstudents.get(i).toString()+"\n";

int id= allregistrations.get(i).getID(); }

for(int j = 0; j< allstudents.size(); j++) return str;

{ }

if(allstudents.get(j).getId()==id){

out+=allstudents.get(j).toString(); public String printCourses(){

out+="\n"; String str="";

} }} } for(int i = 0; i< allcourses.size();i++){

return out; } str+=allcourses.get(i).toString()+"\n";

return str;

}
Creating GUIs
Each GUI corresponds to a class that extends JFrame class

This class belongs to package view


◦ Right click on package view -> new JFrame Form

21
String user = uname.getText();

String pass = pw.getText();

View- GUI Login if (user.equals("ins") && pass.equals("ins"))

{
public static regController controller;
InsForm ins = new InsForm();
public Login() {
ins.setTitle("Instructor");
initComponents();
ins.setVisible(true);
controller = new regController();
uname.setText("");
}
pw.setText("");

else if (user.equals("reg") && pass.equals("reg")){

RegForm reg = new RegForm();

reg.setTitle("Registrar");

reg.setVisible(true);

uname.setText("");

pw.setText("");
Two users: ins,ins (instructor) and reg,reg(registrar)
Can we have more users? }

We will need a file or a database so let us delay it for database course


View-GUI- Instructor

To close without closing the program:


setDefaultCloseOperation(managestudent.DISPOSE_ON_CLOSE);
View-GUI- Registrar
public managestudent() {

initComponents();

setDefaultCloseOperation(managestudent.DISPOSE_ON_CLOSE);

ArrayList<Integer> Ids= new ArrayList();

Ids =Login.controller.getallIDs();

for(int i = 0; i < Ids.size();i++)

idcombo.addItem(Ids.get(i).toString());

ArrayList<String> codes= new ArrayList();

codes =Login.controller.getallCodes();

for(int i = 0; i < codes.size();i++)

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 {

FileWriter stdinf = new FileWriter(fileName+".txt", true);

stdinf.write(str);

stdinf.close();

} catch (IOException ioe) {} }


fileAccess Class
➢Read from the files and write to different array lists (a method is needed for each type of files)
➢ Example: Read from the file students:
public ArrayList<Student> readStudents(){ while(in.ready()){

ArrayList<Student> allStudents = new ArrayList(); line = in.readLine();

String line; stdinfo=line.split("\t");

String stdinfo[]; Student s = new


Student(Integer.parseInt(stdinfo[0]),stdinfo[1]);
try {
if(stdinfo[2].equals("inactive")){
FileInputStream fs = new FileInputStream("students.txt");
s.deactivate();}
InputStreamReader ir=new InputStreamReader(fs);
allStudents.add(s);}
BufferedReader in = new BufferedReader(ir);
in.close();}

catch(IOException e) { }

return allStudents;}
public class regController {

fileAccess fA;

public regController() {

RegController class fA = new fileAccess();}

➢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);

String str = s.toString() + "\n";

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() {

ArrayList<String> out = new ArrayList();

ArrayList<Course> allcourses = new ArrayList();

allcourses = fA.readCourses();

for (int i = 0; i < allcourses.size(); i++) {

out.add(allcourses.get(i).getCode());}

return out; }

You might also like