Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Refactoring #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions _src/OOP_2020-03-23_3-Universityy/TestApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public static void main(String[] args) {
Course dep = polito.createCourse("01TXH", "Data Ethics and Protection");
polito.printCourses();

Student alice = polito.createStudent("123456", "Alice");
Student bob = polito.createStudent("654321", "Bob");
Student carla = polito.createStudent("162534", "Carla");
Student david = polito.createStudent("615243", "David");
Student alice = polito.addStudent("123456", "Alice");
Student bob = polito.addStudent("654321", "Bob");
Student carla = polito.addStudent("162534", "Carla");
Student david = polito.addStudent("615243", "David");
polito.printStudents();

polito.enroll(oop, alice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@

package it.polito.oop.university;

public class Course {
public class Course extends StudentsArray {
private String code;
private String title;
private Student[] enrolledStudents = new Student[300];
private int numStudents = 0;


Course(String code, String title) {
this.code = code;
this.title = title;
Expand All @@ -33,14 +31,4 @@ public String getCode() {
void setTitle(String title) {
this.title = title;
}

void addStudent(Student student) {
enrolledStudents[numStudents++] = student;
}

public void printStudents() {
for (int i = 0; i < numStudents; ++i)
System.out.println(i + 1 + ": " + enrolledStudents[i].getName() +
" (" + enrolledStudents[i].getId() + ")");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*-************************************************************************-*\
* * CLASS SAMPLE FOR "OBJECT ORIENTED PROGRAMMING" (04JEY) *
* ##### * (!) Mar-2020, Giovanni Squillero <[email protected]> *
* ###### * *
* ### \ * Copying and distributing this file, either with or without *
* ##G c\ * modification, are permitted in any medium without royalty, *
* # _\ * provided that this 10-line comment is preserved. *
* | _/ * *
* * ===> THIS FILE IS OFFERED AS-IS, WITHOUT ANY WARRANTY <=== *
\*-************************************************************************-*/

package it.polito.oop.university;

abstract class Person {
private String id;
private String name;

Person(String id, String name) {
this.id = id;
this.name = name;
}

public String getId() {
return id;
}

public String getName() {
return name;
}

void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

package it.polito.oop.university;

public class Professor {
public class Professor extends Person {

Professor(String id, String name) {
super(id, name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,8 @@

package it.polito.oop.university;

public class Student {
private String id;
private String name;

public class Student extends Person {
Student(String id, String name) {
this.id = id;
this.name = name;
super(id, name);
}

public String getId() {
return id;
}

public String getName() {
return name;
}

void setName(String name) {
this.name = name;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*-************************************************************************-*\
* * CLASS SAMPLE FOR "OBJECT ORIENTED PROGRAMMING" (04JEY) *
* ##### * (!) Mar-2020, Giovanni Squillero <[email protected]> *
* ###### * *
* ### \ * Copying and distributing this file, either with or without *
* ##G c\ * modification, are permitted in any medium without royalty, *
* # _\ * provided that this 10-line comment is preserved. *
* | _/ * *
* * ===> THIS FILE IS OFFERED AS-IS, WITHOUT ANY WARRANTY <=== *
\*-************************************************************************-*/

package it.polito.oop.university;

abstract class StudentsArray {
private Student[] enrolledStudents = new Student[300];
private int numStudents = 0;

public void addStudent(Student student) {
this.enrolledStudents[this.numStudents++] = student;
}

public Student addStudent(String id, String name) {
Student student = new Student(id, name);
this.addStudent(student);
return student;
}

public void printStudents() {
for (int i = 0; i < numStudents; ++i)
System.out.println(i + 1 + ": " + enrolledStudents[i].getName() + " (" + enrolledStudents[i].getId() + ")");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,30 @@

package it.polito.oop.university;

public class University {
public class University extends StudentsArray {
private String name;
private Course[] courseArray = new Course[100];
private int numCourses = 0; // technically useless (0 is the default)
private Student[] studentArray = new Student[100];
private int numStudents = 0; // technically useless (0 is the default)

public University(String name) {
this.name = name;
}

public String getName() {
return this.name;
}

public Course createCourse(String code, String title) {
Course course = new Course(code, title);
courseArray[numCourses++] = course;
return course;
}

public Student createStudent(String id, String name) {
Student student = new Student(id, name);
studentArray[numStudents++] = student;
return student;
}

public void printCourses() {
for (int i = 0; i < numCourses; ++i)
System.out.println(i + 1 + ": " + courseArray[i].getTitle() +
" (" + courseArray[i].getCode() + ")");
System.out.println(i + 1 + ": " + courseArray[i].getTitle() + " (" + courseArray[i].getCode() + ")");
}

public void printStudents() {
for (int i = 0; i < numStudents; ++i)
System.out.println(i + 1 + ": " + studentArray[i].getName() +
" (" + studentArray[i].getId() + ")");
}

public void enroll(Course course, Student student) {
course.addStudent(student);
}
Expand Down