Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60684400ad |
@@ -4,11 +4,31 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO:
|
// TODO:
|
||||||
// 1. Create at least 3 courses
|
// 1. Create at least 3 courses
|
||||||
|
Course course1 = new Course("AP", "AP1404", 30);
|
||||||
|
Course course2 = new Course("python", "python1405", 25);
|
||||||
|
Course course3 = new Course("math", "math1406", 60);
|
||||||
|
Course course4 = new Course("physics", "Ph1404", 15);
|
||||||
// 2. Create 1 student
|
// 2. Create 1 student
|
||||||
|
Student student1 = new Student("emad", "1422");
|
||||||
// 3. Create a registration system
|
// 3. Create a registration system
|
||||||
|
RegistrationSystem Rs = new RegistrationSystem();
|
||||||
// 4. Add courses to the system
|
// 4. Add courses to the system
|
||||||
|
Rs.addCourseToSystem(course1);
|
||||||
|
Rs.addCourseToSystem(course2);
|
||||||
|
Rs.addCourseToSystem(course3);
|
||||||
|
Rs.addCourseToSystem(course4);
|
||||||
// 5. Register the student in some courses
|
// 5. Register the student in some courses
|
||||||
|
student1.addCourse(course1);
|
||||||
|
student1.addCourse(course3);
|
||||||
|
student1.addCourse(course4);
|
||||||
|
student1.addCourse(course1);
|
||||||
// 6. Drop one course
|
// 6. Drop one course
|
||||||
|
student1.dropCourse(course3);
|
||||||
// 7. Print the final registered course list
|
// 7. Print the final registered course list
|
||||||
|
student1.showRegisteredCourses();
|
||||||
|
System.out.println();
|
||||||
|
Rs.showAllCourses();
|
||||||
|
System.out.println();
|
||||||
|
Rs.showCoursesCapacity();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,64 @@
|
|||||||
package CourseRegistrationStarter;
|
package CourseRegistrationStarter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class RegistrationSystem {
|
public class RegistrationSystem
|
||||||
|
{
|
||||||
private ArrayList<Course> courses;
|
private ArrayList<Course> courses;
|
||||||
|
|
||||||
public RegistrationSystem() {
|
public RegistrationSystem() {
|
||||||
courses = new ArrayList<>();
|
courses = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCourseToSystem(Course c) {
|
public void addCourseToSystem(Course c)
|
||||||
|
{
|
||||||
// TODO: Add the course to the system
|
// TODO: Add the course to the system
|
||||||
|
courses.add(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Course findCourseByCode(String code) {
|
public Course findCourseByCode(String code)
|
||||||
|
{
|
||||||
// TODO: Search for a course by course code
|
// TODO: Search for a course by course code
|
||||||
// Return the matching course if found, otherwise return null
|
// Return the matching course if found, otherwise return null
|
||||||
|
for(Course course: courses)
|
||||||
|
{
|
||||||
|
if(course.getCourseCode().equals(code))
|
||||||
|
{
|
||||||
|
return course;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
public Course findCourseByName(String name)
|
||||||
|
{
|
||||||
|
// TODO: Search for a course by course code
|
||||||
|
// Return the matching course if found, otherwise return null
|
||||||
|
for(Course course: courses)
|
||||||
|
{
|
||||||
|
if(course.getCourseName().equals(name))
|
||||||
|
{
|
||||||
|
return course;
|
||||||
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showAllCourses() {
|
public void showAllCourses() {
|
||||||
// TODO: Print all courses in the system
|
// TODO: Print all courses in the system
|
||||||
|
for(Course course: courses)
|
||||||
|
{
|
||||||
|
String name = course.getCourseName();
|
||||||
|
String code = course.getCourseCode();
|
||||||
|
int capacity = course.getCapacity();
|
||||||
|
System.out.printf("%s:\n", name);
|
||||||
|
System.out.printf(" %s: %s\n %s: %d\n", "code", code, "capacity", capacity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void showCoursesCapacity()
|
||||||
|
{
|
||||||
|
System.out.println("Capacities:");
|
||||||
|
for(Course c: courses)
|
||||||
|
{
|
||||||
|
System.out.printf(" %S: %d", c.getCourseName(), c.getCapacity());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ public class Student {
|
|||||||
private String name;
|
private String name;
|
||||||
private String studentId;
|
private String studentId;
|
||||||
private ArrayList<Course> registeredCourses;
|
private ArrayList<Course> registeredCourses;
|
||||||
|
private int limitation = 5; //you can't get more than 5 courses
|
||||||
public Student(String name, String studentId) {
|
public Student(String name, String studentId) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.studentId = studentId;
|
this.studentId = studentId;
|
||||||
@@ -20,20 +20,42 @@ public class Student {
|
|||||||
return studentId;
|
return studentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean addCourse(Course c) {
|
public boolean addCourse(Course c)
|
||||||
|
{
|
||||||
// TODO: Register the student in the course if capacity allows
|
// TODO: Register the student in the course if capacity allows
|
||||||
// Return true if registration was successful, otherwise false
|
// Return true if registration was successful, otherwise false
|
||||||
|
if(c.getCapacity() > 0 && !registeredCourses.contains(c)
|
||||||
|
&& registeredCourses.size() < limitation)
|
||||||
|
{
|
||||||
|
registeredCourses.add(c);
|
||||||
|
c.reduceCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean dropCourse(Course c) {
|
public boolean dropCourse(Course c)
|
||||||
|
{
|
||||||
// TODO: Remove the course from the student's list
|
// TODO: Remove the course from the student's list
|
||||||
// Restore the course capacity if removal was successful
|
// Restore the course capacity if removal was successful
|
||||||
// Return true if the course was dropped, otherwise false
|
// Return true if the course was dropped, otherwise false
|
||||||
|
registeredCourses.remove(c);
|
||||||
|
if (!registeredCourses.contains(c))
|
||||||
|
{
|
||||||
|
c.increaseCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
public void showRegisteredCourses()
|
||||||
|
{
|
||||||
// TODO: Print all registered courses
|
// TODO: Print all registered courses
|
||||||
|
for (Course course : registeredCourses)
|
||||||
|
{
|
||||||
|
String courseName = course.getCourseName();
|
||||||
|
String courseCode = course.getCourseCode();
|
||||||
|
System.out.printf("%s: %s\n", courseName, courseCode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user