1 Commits
Author SHA1 Message Date
Partow 8ddd529c85 Update README.md 2026-04-19 15:27:44 +00:00
4 changed files with 17 additions and 69 deletions
-1
View File
@@ -137,7 +137,6 @@ Expected actions:
## Bonus (Optional) ## Bonus (Optional)
- Prevent duplicate course registration - Prevent duplicate course registration
- Prevent registration when capacity is full
- Limit the number of courses per student - Limit the number of courses per student
- Add search by course name - Add search by course name
+8 -21
View File
@@ -2,26 +2,13 @@ package CourseRegistrationStarter;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Course AP = new Course("Advanced Programming", "4041122334", 40); // TODO:
Course FM = new Course("Foundation of Math", "4041212334", 30); // 1. Create at least 3 courses
Course GM = new Course("General Math","4041234123",60); // 2. Create 1 student
// 3. Create a registration system
Student farnam = new Student("Farnam", "404111234"); // 4. Add courses to the system
farnam.setLimitOfCourses(3); // 5. Register the student in some courses
RegistrationSystem regSys = new RegistrationSystem(); // 6. Drop one course
// 7. Print the final registered course list
regSys.addCourseToSystem(AP);
regSys.addCourseToSystem(FM);
regSys.addCourseToSystem(GM);
farnam.addCourse(AP);
farnam.addCourse(FM);
farnam.addCourse(GM);
farnam.dropCourse(FM);
farnam.showRegisteredCourses();
regSys.showAllCourses();
} }
} }
@@ -9,35 +9,16 @@ public class RegistrationSystem {
} }
public void addCourseToSystem(Course c) { public void addCourseToSystem(Course c) {
courses.add(c); // TODO: Add the course to the system
} }
public Course findCourseByCode(String code) { public Course findCourseByCode(String code) {
for (int i = 0; i < courses.size(); i++) { // TODO: Search for a course by course code
if (courses.get(i).getCourseCode().equals(code)){ // Return the matching course if found, otherwise return null
return courses.get(i);
}
}
return null;
}
public Course findCourseByName(String name) {
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getCourseCode().equals(name)){
return courses.get(i);
}
}
return null; return null;
} }
public void showAllCourses() { public void showAllCourses() {
// TODO: Print all courses in the system // TODO: Print all courses in the system
System.out.printf("%25s %15s %18s", "Name","Course code","Remaining capacity");
System.out.print('\n');
for (int i = 0; i < courses.size(); i++) {
System.out.printf("%25s %15s %d", courses.get(i).getCourseName() , courses.get(i).getCourseCode(), courses.get(i).getCapacity());
System.out.print('\n');
}
} }
} }
+6 -25
View File
@@ -5,13 +5,11 @@ 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 limitOfCourses;
public Student(String name, String studentId) { public Student(String name, String studentId) {
this.name = name; this.name = name;
this.studentId = studentId; this.studentId = studentId;
this.registeredCourses = new ArrayList<>(); this.registeredCourses = new ArrayList<>();
this.limitOfCourses = 18;
} }
public String getName() { public String getName() {
@@ -22,37 +20,20 @@ public class Student {
return studentId; return studentId;
} }
public void setLimitOfCourses(int limit){
this.limitOfCourses = limit;
}
public boolean addCourse(Course c) { public boolean addCourse(Course c) {
// TODO: Register the student in the course if capacity allows
if (registeredCourses.contains(c)){ // Return true if registration was successful, otherwise false
return false;
}
if ((c.getCapacity() > 0) && (this.limitOfCourses >= registeredCourses.size() + 1)){
c.reduceCapacity();
registeredCourses.add(c);
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() {
for (int i = 0; i < registeredCourses.size(); i++) { // TODO: Print all registered courses
System.out.printf("%25s %15s", registeredCourses.get(i).getCourseName() , registeredCourses.get(i).getCourseCode());
System.out.print('\n');
}
} }
} }