1 Commits
Author SHA1 Message Date
emad 60684400ad implement course registration system 2026-04-20 22:11:26 +04:30
3 changed files with 89 additions and 7 deletions
+20
View File
@@ -4,11 +4,31 @@ public class Main {
public static void main(String[] args) {
// TODO:
// 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
Student student1 = new Student("emad", "1422");
// 3. Create a registration system
RegistrationSystem Rs = new RegistrationSystem();
// 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
student1.addCourse(course1);
student1.addCourse(course3);
student1.addCourse(course4);
student1.addCourse(course1);
// 6. Drop one course
student1.dropCourse(course3);
// 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;
import java.util.ArrayList;
public class RegistrationSystem {
public class RegistrationSystem
{
private ArrayList<Course> courses;
public RegistrationSystem() {
courses = new ArrayList<>();
}
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
// 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;
}
public void showAllCourses() {
// 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());
}
}
}
+26 -4
View File
@@ -5,7 +5,7 @@ public class Student {
private String name;
private String studentId;
private ArrayList<Course> registeredCourses;
private int limitation = 5; //you can't get more than 5 courses
public Student(String name, String studentId) {
this.name = name;
this.studentId = studentId;
@@ -20,20 +20,42 @@ public class Student {
return studentId;
}
public boolean addCourse(Course c) {
public boolean addCourse(Course c)
{
// TODO: Register the student in the course if capacity allows
// 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;
}
public boolean dropCourse(Course c) {
public boolean dropCourse(Course c)
{
// 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
registeredCourses.remove(c);
if (!registeredCourses.contains(c))
{
c.increaseCapacity();
return true;
}
return false;
}
public void showRegisteredCourses() {
public void showRegisteredCourses()
{
// 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);
}
}
}