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)
- Prevent duplicate course registration
- Prevent registration when capacity is full
- Limit the number of courses per student
- Add search by course name
+8 -21
View File
@@ -2,26 +2,13 @@ package CourseRegistrationStarter;
public class Main {
public static void main(String[] args) {
Course AP = new Course("Advanced Programming", "4041122334", 40);
Course FM = new Course("Foundation of Math", "4041212334", 30);
Course GM = new Course("General Math","4041234123",60);
Student farnam = new Student("Farnam", "404111234");
farnam.setLimitOfCourses(3);
RegistrationSystem regSys = new RegistrationSystem();
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();
// TODO:
// 1. Create at least 3 courses
// 2. Create 1 student
// 3. Create a registration system
// 4. Add courses to the system
// 5. Register the student in some courses
// 6. Drop one course
// 7. Print the final registered course list
}
}
@@ -9,35 +9,16 @@ public class RegistrationSystem {
}
public void addCourseToSystem(Course c) {
courses.add(c);
// TODO: Add the course to the system
}
public Course findCourseByCode(String code) {
for (int i = 0; i < courses.size(); i++) {
if (courses.get(i).getCourseCode().equals(code)){
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);
}
}
// TODO: Search for a course by course code
// Return the matching course if found, otherwise return null
return null;
}
public void showAllCourses() {
// 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 studentId;
private ArrayList<Course> registeredCourses;
private int limitOfCourses;
public Student(String name, String studentId) {
this.name = name;
this.studentId = studentId;
this.registeredCourses = new ArrayList<>();
this.limitOfCourses = 18;
}
public String getName() {
@@ -22,37 +20,20 @@ public class Student {
return studentId;
}
public void setLimitOfCourses(int limit){
this.limitOfCourses = limit;
}
public boolean addCourse(Course c) {
if (registeredCourses.contains(c)){
return false;
}
if ((c.getCapacity() > 0) && (this.limitOfCourses >= registeredCourses.size() + 1)){
c.reduceCapacity();
registeredCourses.add(c);
return true;
}
// TODO: Register the student in the course if capacity allows
// Return true if registration was successful, otherwise false
return false;
}
public boolean dropCourse(Course c) {
if (registeredCourses.remove(c)){
c.increaseCapacity();
return true;
}
// TODO: Remove the course from the student's list
// Restore the course capacity if removal was successful
// Return true if the course was dropped, otherwise false
return false;
}
public void showRegisteredCourses() {
for (int i = 0; i < registeredCourses.size(); i++) {
System.out.printf("%25s %15s", registeredCourses.get(i).getCourseName() , registeredCourses.get(i).getCourseCode());
System.out.print('\n');
}
// TODO: Print all registered courses
}
}