2 Commits
Author SHA1 Message Date
Aeen fa22a42521 Merge pull request 'Assignment 02' (#1) from develop into main
Reviewed-on: Parmis_jamami/WS-02-intro-to-oop#1
2026-04-20 23:05:21 +00:00
Parmis_jamami 9fe6cd709d complete code 2026-04-20 19:28:25 +03:30
3 changed files with 85 additions and 21 deletions
+23 -8
View File
@@ -2,13 +2,28 @@ package CourseRegistrationStarter;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// TODO: Course course1 = new Course("Advanced Programming","AP1404", 2);
// 1. Create at least 3 courses Course course2 = new Course("Data Structures","DS2201",1);
// 2. Create 1 student Course course3 = new Course("Database Systems","DB3301",3);
// 3. Create a registration system
// 4. Add courses to the system Student student1 = new Student("Ali" , "402222001");
// 5. Register the student in some courses
// 6. Drop one course RegistrationSystem registrationSystem = new RegistrationSystem();
// 7. Print the final registered course list
registrationSystem.addCourseToSystem(course1);
registrationSystem.addCourseToSystem(course2);
registrationSystem.addCourseToSystem(course3);
student1.addCourse(course1);
student1.addCourse(course3);
student1.dropCourse(course1);
student1.showRegisteredCourses();
System.out.println("Course " + course1.getCourseName() + " capacity: " + course1.getCapacity());
System.out.println("Course " + course2.getCourseName() + " capacity: " + course2.getCapacity());
System.out.println("Course " + course3.getCourseName() + " capacity: " + course3.getCapacity());
System.out.println("the number of courses: " + student1.countCourse());
} }
} }
@@ -9,16 +9,27 @@ 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(int i = 0 ; i < courses.size() ; i++){
// Return the matching course if found, otherwise return null Course course = courses.get(i);
if(course.getCourseCode().equals(code)){
return course;
}
}
return null; return null;
} }
public void showAllCourses() { public void showAllCourses() {
// TODO: Print all courses in the system System.out.println("Courses in the system: ");
for(int i = 0 ; i < courses.size() ; i++ ){
Course c = courses.get(i);
System.out.println(" Name: " + c.getCourseName() +
" , code: " + c.getCourseCode() +
", capicity: " + c.getCapacity());
}
} }
} }
+47 -9
View File
@@ -1,4 +1,6 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
import com.sun.net.httpserver.Authenticator;
import java.util.ArrayList; import java.util.ArrayList;
public class Student { public class Student {
@@ -21,19 +23,55 @@ public class Student {
} }
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 System.out.println( " already registered for " + c.getCourseName());
return false; return false;
}
if(c.reduceCapacity()){
registeredCourses.add(c);
System.out.println("Successfully registered for " + c.getCourseName());
return true;
}
else{
System.out.println("Course is full");
return false;
}
} }
public boolean dropCourse(Course c) { public boolean dropCourse(Course c) {
// TODO: Remove the course from the student's list boolean removed = registeredCourses.remove(c);
// Restore the course capacity if removal was successful if(removed){
// Return true if the course was dropped, otherwise false c.increaseCapacity();
return false; System.out.println("Course " + c.getCourseName() + " succsessfully removed");
return true;
}
else{
System.out.println("Corse " + c.getCourseName() + "is not found");
return false;
}
} }
public void showRegisteredCourses() { public void showRegisteredCourses() {
// TODO: Print all registered courses System.out.print("Courses registered by " + name + " : ");
if(registeredCourses.isEmpty()){
System.out.println("No course found! ");
}
else{
for(int i = 0 ; i < registeredCourses.size() ; i++ ){
Course course = registeredCourses.get(i);
System.out.println(course.getCourseName() + "(" + course.getCourseCode() + ")" + " , ");
}
}
} }
}
public int countCourse(){
int count=0;
for(int i = 0 ; i < registeredCourses.size() ; i++){
count++;
}
return count;
}
}