1 Commits
Author SHA1 Message Date
Reza ef4f7f4885 Completed 2026-04-20 13:32:40 +03:30
3 changed files with 63 additions and 18 deletions
+20 -8
View File
@@ -2,13 +2,25 @@ package CourseRegistrationStarter;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// TODO:
// 1. Create at least 3 courses Course c1 = new Course("Math", "M10", 5);
// 2. Create 1 student Course c2 = new Course("History", "HK1", 4);
// 3. Create a registration system Course c3 = new Course("Programming", "CS2",2);
// 4. Add courses to the system
// 5. Register the student in some courses Student s1 = new Student("Reza", "404111111");
// 6. Drop one course
// 7. Print the final registered course list RegistrationSystem regSystem = new RegistrationSystem();
regSystem.addCourseToSystem(c1);
regSystem.addCourseToSystem(c2);
regSystem.addCourseToSystem(c3);
s1.addCourse(c1);
s1.addCourse(c3);
s1.dropCourse(c1);
s1.showRegisteredCourses();
regSystem.showAllCourses();
} }
} }
@@ -9,16 +9,31 @@ public class RegistrationSystem {
} }
public void addCourseToSystem(Course c) { public void addCourseToSystem(Course c) {
// 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 for (Course c : courses) {
// Return the matching course if found, otherwise return null if (c.getCourseCode().equals(code)) {
return c ;
}
}
return null;
}
//Bonus
public Course findCourseByName(String name) {
for (Course c : courses) {
if (c.getCourseName().equals(name)) {
return c;
}
}
return null; return null;
} }
public void showAllCourses() { public void showAllCourses() {
// TODO: Print all courses in the system System.out.println("All courses : ");
for (Course c : courses) {
System.out.println(" " + c.getCourseName() + " -- " + c.getCourseCode() + " - " + c.getCapacity());
}
} }
} }
+24 -6
View File
@@ -5,6 +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 static final int maxCourses = 2; //Bonus
public Student(String name, String studentId) { public Student(String name, String studentId) {
this.name = name; this.name = name;
@@ -21,19 +22,36 @@ public class Student {
} }
public boolean addCourse(Course c) { public boolean addCourse(Course c) {
// TODO: Register the student in the course if capacity allows //Bonus
// Return true if registration was successful, otherwise false for (Course course : registeredCourses) {
if (course.getCourseCode().equals(c.getCourseCode())) {
return false;
}
}
//Bonus
if (registeredCourses.size() >= maxCourses) {
return false;
}
if (c.getCapacity() > 0) {
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 if (registeredCourses.remove(c)) {
// Restore the course capacity if removal was successful c.increaseCapacity();
// Return true if the course was dropped, otherwise false return true;
}
return false; return false;
} }
public void showRegisteredCourses() { public void showRegisteredCourses() {
// TODO: Print all registered courses System.out.println("Registered courses : ");
for (Course c : registeredCourses) {
System.out.println(" " + c.getCourseName() + " -- " + c.getCourseCode());
}
} }
} }