develop #2
@@ -2,13 +2,25 @@ 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 AdvancedProgramming = new Course("AdvancedProgramming", "AP1404", 2);
|
||||
Course DataStructures = new Course("DataStructures", "DS2201", 1);
|
||||
Course DatabaseSystems = new Course("DatabaseSystems", "DB3301", 3);
|
||||
|
||||
Student fateme = new Student("fateme", "404222000", 10);
|
||||
|
||||
RegistrationSystem A = new RegistrationSystem();
|
||||
|
||||
A.addCourseToSystem(AdvancedProgramming);
|
||||
A.addCourseToSystem(DataStructures);
|
||||
A.addCourseToSystem(DatabaseSystems);
|
||||
|
||||
fateme.addCourse(AdvancedProgramming);
|
||||
fateme.addCourse(DataStructures);
|
||||
fateme.addCourse(DatabaseSystems);
|
||||
|
||||
fateme.dropCourse(DataStructures);
|
||||
|
||||
fateme.showRegisteredCourses();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,46 @@ public class RegistrationSystem {
|
||||
}
|
||||
|
||||
public void addCourseToSystem(Course c) {
|
||||
// TODO: Add the course to the system
|
||||
|
||||
for (int i=0 ; i < courses.size() ; i++) {
|
||||
|
||||
if(courses.get(i).getCourseCode().equals(c.getCourseCode()) ||
|
||||
courses.get(i).getCourseName().equals(c.getCourseName())) {
|
||||
|
||||
System.out.println("Course name or code already exists.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
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++) {
|
||||
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).getCourseName().equals(name))
|
||||
return courses.get(i);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void showAllCourses() {
|
||||
// TODO: Print all courses in the system
|
||||
for(int i=0 ; i < courses.size() ; i++) {
|
||||
System.out.print("Course");
|
||||
System.out.print(i+1);
|
||||
System.out.print(" : " + courses.get(i).getCourseName() + " " + courses.get(i).getCourseCode());
|
||||
System.out.println(", capacity : " + courses.get(i).getCapacity());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,16 @@ package CourseRegistrationStarter;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Student {
|
||||
private String name;
|
||||
private String studentId;
|
||||
private final String name;
|
||||
private final String studentId;
|
||||
private ArrayList<Course> registeredCourses;
|
||||
private int courseLimit;
|
||||
|
||||
public Student(String name, String studentId) {
|
||||
public Student(String name, String studentId, int courseLimit) {
|
||||
this.name = name;
|
||||
this.studentId = studentId;
|
||||
this.registeredCourses = new ArrayList<>();
|
||||
this.courseLimit = courseLimit;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
@@ -21,19 +23,35 @@ 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
|
||||
|
||||
if(c.getCapacity() > 0 && registeredCourses.size() < courseLimit) {
|
||||
registeredCourses.add(c);
|
||||
c.reduceCapacity();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void setCourseLimit(int amount) {
|
||||
if(amount>0)
|
||||
courseLimit = amount;
|
||||
}
|
||||
|
||||
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
|
||||
if (registeredCourses.remove(c)) {
|
||||
c.increaseCapacity();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
// TODO: Print all registered courses
|
||||
|
||||
for(int i=0 ; i < registeredCourses.size() ; i++) {
|
||||
System.out.print("Course");
|
||||
System.out.print(i+1);
|
||||
System.out.print(" : " + registeredCourses.get(i).getCourseName() + " " + registeredCourses.get(i).getCourseCode());
|
||||
System.out.println(", capacity : " +registeredCourses.get(i).getCapacity());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user