complete code
This commit is contained in:
@@ -2,13 +2,28 @@ package CourseRegistrationStarter;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
// 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
|
||||
Course course1 = new Course("Advanced Programming","AP1404", 2);
|
||||
Course course2 = new Course("Data Structures","DS2201",1);
|
||||
Course course3 = new Course("Database Systems","DB3301",3);
|
||||
|
||||
Student student1 = new Student("Ali" , "402222001");
|
||||
|
||||
RegistrationSystem registrationSystem = new RegistrationSystem();
|
||||
|
||||
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) {
|
||||
// TODO: Add the course to the system
|
||||
courses.add(c);
|
||||
}
|
||||
|
||||
public Course findCourseByCode(String code) {
|
||||
// TODO: Search for a course by course code
|
||||
// Return the matching course if found, otherwise return null
|
||||
for(int i = 0 ; i < courses.size() ; i++){
|
||||
Course course = courses.get(i);
|
||||
if(course.getCourseCode().equals(code)){
|
||||
return course;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
package CourseRegistrationStarter;
|
||||
import com.sun.net.httpserver.Authenticator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Student {
|
||||
@@ -21,19 +23,55 @@ public class Student {
|
||||
}
|
||||
|
||||
public boolean addCourse(Course c) {
|
||||
// TODO: Register the student in the course if capacity allows
|
||||
// Return true if registration was successful, otherwise false
|
||||
return false;
|
||||
if(registeredCourses.contains(c)){
|
||||
System.out.println( " already registered for " + c.getCourseName());
|
||||
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) {
|
||||
// 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;
|
||||
boolean removed = registeredCourses.remove(c);
|
||||
if(removed){
|
||||
c.increaseCapacity();
|
||||
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() {
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user