SlideShare a Scribd company logo
Starting with Main.java, where I tested everything:
import College.*;
import College.courses.*;
public class Main
{
public static void main(String[] args)
{
Teacher willson = new Teacher("Willson");
Teacher modi = new Teacher("Modi");
Teacher lil = new Teacher("Lil");
Teacher jorge = new Teacher("Jorge");
Course[] courses = {
new NetworkCourse(15, willson),
new SwingCourse(30, modi),
new APIDesignCourse(50, lil),
new PerformanceCourse(5, jorge)
};
College College = new College(courses);
Student jhon = new Student("Jhon");
Student devid = new Student("Devid");
Student daniel = new Student("Daniel");
jhon.setPreferredCourses(NetworkCourse.class, SwingCourse.class);
devid.setPreferredCourses(APIDesignCourse.class, PerformanceCourse.class,
NetworkCourse.class);
College.register(jhon, devid, daniel);
test(College);
}
static void test(College College) {
System.out.println("Students and their courses:");
for(Student student : College.getStudents()) {
if(student != null) {
String message = student.getName() + " is taking"; //message will reset for each new
student, since we do = and not += here
for(Course course : student.getCourses())
message += " - " + course.getName();
System.out.println(message);
}
}
System.out.println(" Courses and their students:");
for(Course course : College.getCourses()) {
String message = course.getName() + " is taken by";
for(Student student : course.getStudents()) {
if(student != null)
message += " - " + student.getName();
}
System.out.println(message);
}
}
}
College.java
package College;
import java.util.*;
public class College {
private Course[] courses;
private Student[] students;
public College(Course[] courses) {
this.courses = courses;
int numOfStudents = 0;
for(Course course : courses)
numOfStudents += course.getStudents().length;
students = new Student[numOfStudents];
}
public void register(Student...students)
{
if(isFull())
throw new IllegalStateException("Cannot register anymore students at this time");
for(Student student : students) {
if(Arrays.asList(this.students).contains(student))
throw new IllegalArgumentException("You cannot add the same student to a College
twice");
for(Course course : courses) {
if(student.prefersCourse(course) && !course.isFull())
student.assignCourse(course);
}
verifyStudent(student); //make sure the student is ready for College
student.setCollege(this);
for(int i = 0; i < this.students.length; i++) {
if(this.students[i] == null) {
this.students[i] = student;
break;
}
}
}
}
private void verifyStudent(Student student) {
verifyCourses(student);
}
private void verifyCourses(Student student) {
boolean verified = false;
while(!verified) {
for(Course course : student.getCourses()) {
if(course == null) {
int index = (int) (Math.random() * courses.length);
student.assignCourse(courses[index]);
}
}
verified = !Arrays.asList(student.getCourses()).contains(null);
}
}
public Student[] getStudents() {
return Arrays.copyOf(students, students.length);
}
public Course[] getCourses() {
return Arrays.copyOf(courses, courses.length);
}
public boolean isFull() {
boolean full = true;
for(Student student : students)
if(student == null)
return full = false;
return full;
}
}
Course.java
package College;
import java.util.*;
public abstract class Course {
private Teacher teacher;
private Student[] students;
private UUID id;
protected Course(int maxStudents, Teacher teacher) { //might allow multiple teachers later
students = new Student[maxStudents];
this.teacher = teacher;
id = UUID.randomUUID();
}
void addStudent(Student student) {
for(int i = 0; i < students.length; i++) {
if(student == students[i])
continue;
if(students[i] == null) {
students[i] = student;
return;
}
}
}
public Teacher getTeacher() {
return teacher;
}
public Student[] getStudents() {
return Arrays.copyOf(students, students.length);
}
public boolean isFull() {
boolean full = true;
for(Student student : students)
full = student != null;
return full;
}
public abstract String getName();
}
Student.java
package College;
import java.util.*;
public class Student extends Entity {
private College College;
private Course[] courses;
private Set> preferredCourses;
public Student(String name) {
super(name);
courses = new Course[2];
preferredCourses = new HashSet<>();
}
public void setPreferredCourses(Class...courses) {
for(Class course : courses) {
preferredCourses.add(course);
}
}
void assignCourse(Course course) {
for(int i = 0; i < courses.length; i++) {
if(course == courses[i])
continue;
if(courses[i] == null) {
course.addStudent(this);
courses[i] = course;
return;
}
}
}
void setCollege(College College) {
this.College = College;
}
public College getCollege() {
return College;
}
public Course[] getCourses() {
return Arrays.copyOf(courses, courses.length);
}
public boolean prefersCourse(Course course) {
return preferredCourses.contains(course.getClass());
}
public boolean isTakingCourse(Course course) {
boolean contains = false;
for(Course c : courses)
return contains = (c == course);
return contains;
}
}
Teacher.java
package College;
public class Teacher extends Entity {
public Teacher(String name) {
super(name);
}
}
Entity.java
package College;
public abstract class Entity {
private String name;
protected Entity(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
APIDesignCourse.java
package College.courses;
import College.*;
public class APIDesignCourse extends Course {
public APIDesignCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s API Design Course";
}
}
Solution
Starting with Main.java, where I tested everything:
import College.*;
import College.courses.*;
public class Main
{
public static void main(String[] args)
{
Teacher willson = new Teacher("Willson");
Teacher modi = new Teacher("Modi");
Teacher lil = new Teacher("Lil");
Teacher jorge = new Teacher("Jorge");
Course[] courses = {
new NetworkCourse(15, willson),
new SwingCourse(30, modi),
new APIDesignCourse(50, lil),
new PerformanceCourse(5, jorge)
};
College College = new College(courses);
Student jhon = new Student("Jhon");
Student devid = new Student("Devid");
Student daniel = new Student("Daniel");
jhon.setPreferredCourses(NetworkCourse.class, SwingCourse.class);
devid.setPreferredCourses(APIDesignCourse.class, PerformanceCourse.class,
NetworkCourse.class);
College.register(jhon, devid, daniel);
test(College);
}
static void test(College College) {
System.out.println("Students and their courses:");
for(Student student : College.getStudents()) {
if(student != null) {
String message = student.getName() + " is taking"; //message will reset for each new
student, since we do = and not += here
for(Course course : student.getCourses())
message += " - " + course.getName();
System.out.println(message);
}
}
System.out.println(" Courses and their students:");
for(Course course : College.getCourses()) {
String message = course.getName() + " is taken by";
for(Student student : course.getStudents()) {
if(student != null)
message += " - " + student.getName();
}
System.out.println(message);
}
}
}
College.java
package College;
import java.util.*;
public class College {
private Course[] courses;
private Student[] students;
public College(Course[] courses) {
this.courses = courses;
int numOfStudents = 0;
for(Course course : courses)
numOfStudents += course.getStudents().length;
students = new Student[numOfStudents];
}
public void register(Student...students)
{
if(isFull())
throw new IllegalStateException("Cannot register anymore students at this time");
for(Student student : students) {
if(Arrays.asList(this.students).contains(student))
throw new IllegalArgumentException("You cannot add the same student to a College
twice");
for(Course course : courses) {
if(student.prefersCourse(course) && !course.isFull())
student.assignCourse(course);
}
verifyStudent(student); //make sure the student is ready for College
student.setCollege(this);
for(int i = 0; i < this.students.length; i++) {
if(this.students[i] == null) {
this.students[i] = student;
break;
}
}
}
}
private void verifyStudent(Student student) {
verifyCourses(student);
}
private void verifyCourses(Student student) {
boolean verified = false;
while(!verified) {
for(Course course : student.getCourses()) {
if(course == null) {
int index = (int) (Math.random() * courses.length);
student.assignCourse(courses[index]);
}
}
verified = !Arrays.asList(student.getCourses()).contains(null);
}
}
public Student[] getStudents() {
return Arrays.copyOf(students, students.length);
}
public Course[] getCourses() {
return Arrays.copyOf(courses, courses.length);
}
public boolean isFull() {
boolean full = true;
for(Student student : students)
if(student == null)
return full = false;
return full;
}
}
Course.java
package College;
import java.util.*;
public abstract class Course {
private Teacher teacher;
private Student[] students;
private UUID id;
protected Course(int maxStudents, Teacher teacher) { //might allow multiple teachers later
students = new Student[maxStudents];
this.teacher = teacher;
id = UUID.randomUUID();
}
void addStudent(Student student) {
for(int i = 0; i < students.length; i++) {
if(student == students[i])
continue;
if(students[i] == null) {
students[i] = student;
return;
}
}
}
public Teacher getTeacher() {
return teacher;
}
public Student[] getStudents() {
return Arrays.copyOf(students, students.length);
}
public boolean isFull() {
boolean full = true;
for(Student student : students)
full = student != null;
return full;
}
public abstract String getName();
}
Student.java
package College;
import java.util.*;
public class Student extends Entity {
private College College;
private Course[] courses;
private Set> preferredCourses;
public Student(String name) {
super(name);
courses = new Course[2];
preferredCourses = new HashSet<>();
}
public void setPreferredCourses(Class...courses) {
for(Class course : courses) {
preferredCourses.add(course);
}
}
void assignCourse(Course course) {
for(int i = 0; i < courses.length; i++) {
if(course == courses[i])
continue;
if(courses[i] == null) {
course.addStudent(this);
courses[i] = course;
return;
}
}
}
void setCollege(College College) {
this.College = College;
}
public College getCollege() {
return College;
}
public Course[] getCourses() {
return Arrays.copyOf(courses, courses.length);
}
public boolean prefersCourse(Course course) {
return preferredCourses.contains(course.getClass());
}
public boolean isTakingCourse(Course course) {
boolean contains = false;
for(Course c : courses)
return contains = (c == course);
return contains;
}
}
Teacher.java
package College;
public class Teacher extends Entity {
public Teacher(String name) {
super(name);
}
}
Entity.java
package College;
public abstract class Entity {
private String name;
protected Entity(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
APIDesignCourse.java
package College.courses;
import College.*;
public class APIDesignCourse extends Course {
public APIDesignCourse(int numOfStudents, Teacher teacher) {
super(numOfStudents, teacher);
}
public String getName() {
return getTeacher().getName() + "'s API Design Course";
}
}

More Related Content

PDF
import school.; import school.courses.;public class Main { p.pdf
PPTX
DDD Modeling Workshop
PDF
[PL] O klasycznej, programistycznej elegancji
PPT
Mapeamento uml java
PDF
C# Summer course - Lecture 4
PDF
Hello. Im currently working on the last section to my assignment a.pdf
PDF
Java questionI am having issues returning the score sort in numeri.pdf
DOCX
Assignment 7
import school.; import school.courses.;public class Main { p.pdf
DDD Modeling Workshop
[PL] O klasycznej, programistycznej elegancji
Mapeamento uml java
C# Summer course - Lecture 4
Hello. Im currently working on the last section to my assignment a.pdf
Java questionI am having issues returning the score sort in numeri.pdf
Assignment 7

Similar to Starting with Main.java, where I tested everythingimport College..pdf (20)

PPTX
Static keyword.pptx
PPTX
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
PDF
Modul Praktek Java OOP
PPT
A457405934_21789_26_2018_Inheritance.ppt
PPTX
Lecture_4_Static_variables_and_Methods.pptx
DOCX
Unit-3 Practice Programs-5.docx
PDF
public class Person { private String name; private int age;.pdf
PPTX
Java Programs
PDF
C# Delegates, Events, Lambda
PDF
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
DOCX
OOP Lab Report.docx
PPTX
constructer.pptx
PPT
PDF
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
PPTX
Static keyword ppt
PPTX
Migrating to JUnit 5
PPT
Java tutorial for Beginners and Entry Level
PDF
Header #include -string- #include -vector- #include -iostream- using.pdf
PPTX
Solid principles
PPT
WDD_lec_06.ppt
Static keyword.pptx
Code Smells y Refactoring o haciendo que nuestro codigo huela (y se vea) mejo...
Modul Praktek Java OOP
A457405934_21789_26_2018_Inheritance.ppt
Lecture_4_Static_variables_and_Methods.pptx
Unit-3 Practice Programs-5.docx
public class Person { private String name; private int age;.pdf
Java Programs
C# Delegates, Events, Lambda
Teacher.javaimport java.util.Arrays; import java.util.Scanner;.pdf
OOP Lab Report.docx
constructer.pptx
#ifndef COURSES_H #define COURSES_H class Courses { privat.pdf
Static keyword ppt
Migrating to JUnit 5
Java tutorial for Beginners and Entry Level
Header #include -string- #include -vector- #include -iostream- using.pdf
Solid principles
WDD_lec_06.ppt
Ad

More from aptind (20)

PDF
ssian chemist, Dmitri Mendeleev is often consider.pdf
PDF
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
PDF
               CLOUD COMPUTING -----------------------------------.pdf
PDF
You cannot.SolutionYou cannot..pdf
PDF
ViVi is universally available on Unix systems. It has been around.pdf
PDF
Waterfall methodThe model consists of various phases based on the.pdf
PDF
Hi, I am unable to understand the terminology in .pdf
PDF
The main function of cerebellum is to control the motor movements. H.pdf
PDF
solution of question no.6inputPresent stateNext stateoutput.pdf
PDF
Sexual reproduction has played the most crucial role in evolution of.pdf
PDF
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
PDF
And is option DIf variable interest rate decrease , asset value wi.pdf
PDF
import java.util.Scanner;public class Factorial { method usi.pdf
PDF
Hi please find my code.import java.util.HashMap;import java.util.pdf
PDF
Given below is the code for the question. Since the test files (ment.pdf
PDF
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
PDF
As we understand, when soil particles binds to each other more stron.pdf
PDF
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
PDF
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
PDF
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
ssian chemist, Dmitri Mendeleev is often consider.pdf
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
               CLOUD COMPUTING -----------------------------------.pdf
You cannot.SolutionYou cannot..pdf
ViVi is universally available on Unix systems. It has been around.pdf
Waterfall methodThe model consists of various phases based on the.pdf
Hi, I am unable to understand the terminology in .pdf
The main function of cerebellum is to control the motor movements. H.pdf
solution of question no.6inputPresent stateNext stateoutput.pdf
Sexual reproduction has played the most crucial role in evolution of.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
And is option DIf variable interest rate decrease , asset value wi.pdf
import java.util.Scanner;public class Factorial { method usi.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdf
Given below is the code for the question. Since the test files (ment.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
As we understand, when soil particles binds to each other more stron.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
1.They trade away higher fecundity for future reproduction.2.Resou.pdf
Ad

Recently uploaded (20)

PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Basic Mud Logging Guide for educational purpose
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Lesson notes of climatology university.
PDF
01-Introduction-to-Information-Management.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
O5-L3 Freight Transport Ops (International) V1.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Final Presentation General Medicine 03-08-2024.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Basic Mud Logging Guide for educational purpose
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Microbial disease of the cardiovascular and lymphatic systems
Abdominal Access Techniques with Prof. Dr. R K Mishra
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
102 student loan defaulters named and shamed – Is someone you know on the list?
Lesson notes of climatology university.
01-Introduction-to-Information-Management.pdf
VCE English Exam - Section C Student Revision Booklet
human mycosis Human fungal infections are called human mycosis..pptx
TR - Agricultural Crops Production NC III.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Structure & Organelles in detailed.
O7-L3 Supply Chain Operations - ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

Starting with Main.java, where I tested everythingimport College..pdf

  • 1. Starting with Main.java, where I tested everything: import College.*; import College.courses.*; public class Main { public static void main(String[] args) { Teacher willson = new Teacher("Willson"); Teacher modi = new Teacher("Modi"); Teacher lil = new Teacher("Lil"); Teacher jorge = new Teacher("Jorge"); Course[] courses = { new NetworkCourse(15, willson), new SwingCourse(30, modi), new APIDesignCourse(50, lil), new PerformanceCourse(5, jorge) }; College College = new College(courses); Student jhon = new Student("Jhon"); Student devid = new Student("Devid"); Student daniel = new Student("Daniel"); jhon.setPreferredCourses(NetworkCourse.class, SwingCourse.class); devid.setPreferredCourses(APIDesignCourse.class, PerformanceCourse.class, NetworkCourse.class); College.register(jhon, devid, daniel); test(College); } static void test(College College) { System.out.println("Students and their courses:"); for(Student student : College.getStudents()) { if(student != null) { String message = student.getName() + " is taking"; //message will reset for each new student, since we do = and not += here for(Course course : student.getCourses()) message += " - " + course.getName();
  • 2. System.out.println(message); } } System.out.println(" Courses and their students:"); for(Course course : College.getCourses()) { String message = course.getName() + " is taken by"; for(Student student : course.getStudents()) { if(student != null) message += " - " + student.getName(); } System.out.println(message); } } } College.java package College; import java.util.*; public class College { private Course[] courses; private Student[] students; public College(Course[] courses) { this.courses = courses; int numOfStudents = 0; for(Course course : courses) numOfStudents += course.getStudents().length; students = new Student[numOfStudents]; } public void register(Student...students) { if(isFull()) throw new IllegalStateException("Cannot register anymore students at this time"); for(Student student : students) { if(Arrays.asList(this.students).contains(student)) throw new IllegalArgumentException("You cannot add the same student to a College twice"); for(Course course : courses) {
  • 3. if(student.prefersCourse(course) && !course.isFull()) student.assignCourse(course); } verifyStudent(student); //make sure the student is ready for College student.setCollege(this); for(int i = 0; i < this.students.length; i++) { if(this.students[i] == null) { this.students[i] = student; break; } } } } private void verifyStudent(Student student) { verifyCourses(student); } private void verifyCourses(Student student) { boolean verified = false; while(!verified) { for(Course course : student.getCourses()) { if(course == null) { int index = (int) (Math.random() * courses.length); student.assignCourse(courses[index]); } } verified = !Arrays.asList(student.getCourses()).contains(null); } } public Student[] getStudents() { return Arrays.copyOf(students, students.length); } public Course[] getCourses() { return Arrays.copyOf(courses, courses.length); } public boolean isFull() { boolean full = true;
  • 4. for(Student student : students) if(student == null) return full = false; return full; } } Course.java package College; import java.util.*; public abstract class Course { private Teacher teacher; private Student[] students; private UUID id; protected Course(int maxStudents, Teacher teacher) { //might allow multiple teachers later students = new Student[maxStudents]; this.teacher = teacher; id = UUID.randomUUID(); } void addStudent(Student student) { for(int i = 0; i < students.length; i++) { if(student == students[i]) continue; if(students[i] == null) { students[i] = student; return; } } } public Teacher getTeacher() { return teacher; } public Student[] getStudents() { return Arrays.copyOf(students, students.length); } public boolean isFull() { boolean full = true;
  • 5. for(Student student : students) full = student != null; return full; } public abstract String getName(); } Student.java package College; import java.util.*; public class Student extends Entity { private College College; private Course[] courses; private Set> preferredCourses; public Student(String name) { super(name); courses = new Course[2]; preferredCourses = new HashSet<>(); } public void setPreferredCourses(Class...courses) { for(Class course : courses) { preferredCourses.add(course); } } void assignCourse(Course course) { for(int i = 0; i < courses.length; i++) { if(course == courses[i]) continue; if(courses[i] == null) { course.addStudent(this); courses[i] = course; return; } } } void setCollege(College College) { this.College = College;
  • 6. } public College getCollege() { return College; } public Course[] getCourses() { return Arrays.copyOf(courses, courses.length); } public boolean prefersCourse(Course course) { return preferredCourses.contains(course.getClass()); } public boolean isTakingCourse(Course course) { boolean contains = false; for(Course c : courses) return contains = (c == course); return contains; } } Teacher.java package College; public class Teacher extends Entity { public Teacher(String name) { super(name); } } Entity.java package College; public abstract class Entity { private String name; protected Entity(String name) { this.name = name; } public String getName() { return name; } } APIDesignCourse.java
  • 7. package College.courses; import College.*; public class APIDesignCourse extends Course { public APIDesignCourse(int numOfStudents, Teacher teacher) { super(numOfStudents, teacher); } public String getName() { return getTeacher().getName() + "'s API Design Course"; } } Solution Starting with Main.java, where I tested everything: import College.*; import College.courses.*; public class Main { public static void main(String[] args) { Teacher willson = new Teacher("Willson"); Teacher modi = new Teacher("Modi"); Teacher lil = new Teacher("Lil"); Teacher jorge = new Teacher("Jorge"); Course[] courses = { new NetworkCourse(15, willson), new SwingCourse(30, modi), new APIDesignCourse(50, lil), new PerformanceCourse(5, jorge) }; College College = new College(courses); Student jhon = new Student("Jhon"); Student devid = new Student("Devid"); Student daniel = new Student("Daniel"); jhon.setPreferredCourses(NetworkCourse.class, SwingCourse.class); devid.setPreferredCourses(APIDesignCourse.class, PerformanceCourse.class,
  • 8. NetworkCourse.class); College.register(jhon, devid, daniel); test(College); } static void test(College College) { System.out.println("Students and their courses:"); for(Student student : College.getStudents()) { if(student != null) { String message = student.getName() + " is taking"; //message will reset for each new student, since we do = and not += here for(Course course : student.getCourses()) message += " - " + course.getName(); System.out.println(message); } } System.out.println(" Courses and their students:"); for(Course course : College.getCourses()) { String message = course.getName() + " is taken by"; for(Student student : course.getStudents()) { if(student != null) message += " - " + student.getName(); } System.out.println(message); } } } College.java package College; import java.util.*; public class College { private Course[] courses; private Student[] students; public College(Course[] courses) { this.courses = courses; int numOfStudents = 0; for(Course course : courses)
  • 9. numOfStudents += course.getStudents().length; students = new Student[numOfStudents]; } public void register(Student...students) { if(isFull()) throw new IllegalStateException("Cannot register anymore students at this time"); for(Student student : students) { if(Arrays.asList(this.students).contains(student)) throw new IllegalArgumentException("You cannot add the same student to a College twice"); for(Course course : courses) { if(student.prefersCourse(course) && !course.isFull()) student.assignCourse(course); } verifyStudent(student); //make sure the student is ready for College student.setCollege(this); for(int i = 0; i < this.students.length; i++) { if(this.students[i] == null) { this.students[i] = student; break; } } } } private void verifyStudent(Student student) { verifyCourses(student); } private void verifyCourses(Student student) { boolean verified = false; while(!verified) { for(Course course : student.getCourses()) { if(course == null) { int index = (int) (Math.random() * courses.length); student.assignCourse(courses[index]); }
  • 10. } verified = !Arrays.asList(student.getCourses()).contains(null); } } public Student[] getStudents() { return Arrays.copyOf(students, students.length); } public Course[] getCourses() { return Arrays.copyOf(courses, courses.length); } public boolean isFull() { boolean full = true; for(Student student : students) if(student == null) return full = false; return full; } } Course.java package College; import java.util.*; public abstract class Course { private Teacher teacher; private Student[] students; private UUID id; protected Course(int maxStudents, Teacher teacher) { //might allow multiple teachers later students = new Student[maxStudents]; this.teacher = teacher; id = UUID.randomUUID(); } void addStudent(Student student) { for(int i = 0; i < students.length; i++) { if(student == students[i]) continue; if(students[i] == null) { students[i] = student;
  • 11. return; } } } public Teacher getTeacher() { return teacher; } public Student[] getStudents() { return Arrays.copyOf(students, students.length); } public boolean isFull() { boolean full = true; for(Student student : students) full = student != null; return full; } public abstract String getName(); } Student.java package College; import java.util.*; public class Student extends Entity { private College College; private Course[] courses; private Set> preferredCourses; public Student(String name) { super(name); courses = new Course[2]; preferredCourses = new HashSet<>(); } public void setPreferredCourses(Class...courses) { for(Class course : courses) { preferredCourses.add(course); } } void assignCourse(Course course) {
  • 12. for(int i = 0; i < courses.length; i++) { if(course == courses[i]) continue; if(courses[i] == null) { course.addStudent(this); courses[i] = course; return; } } } void setCollege(College College) { this.College = College; } public College getCollege() { return College; } public Course[] getCourses() { return Arrays.copyOf(courses, courses.length); } public boolean prefersCourse(Course course) { return preferredCourses.contains(course.getClass()); } public boolean isTakingCourse(Course course) { boolean contains = false; for(Course c : courses) return contains = (c == course); return contains; } } Teacher.java package College; public class Teacher extends Entity { public Teacher(String name) { super(name); } }
  • 13. Entity.java package College; public abstract class Entity { private String name; protected Entity(String name) { this.name = name; } public String getName() { return name; } } APIDesignCourse.java package College.courses; import College.*; public class APIDesignCourse extends Course { public APIDesignCourse(int numOfStudents, Teacher teacher) { super(numOfStudents, teacher); } public String getName() { return getTeacher().getName() + "'s API Design Course"; } }