Compare commits
6
Commits
8ddd529c85
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66e86acde1 | ||
|
|
41b8d9af0a | ||
|
|
e754e9bc46 | ||
|
|
dc4a063177 | ||
|
|
a4245b41a6 | ||
|
|
1c54896277 |
@@ -2,13 +2,25 @@ package CourseRegistrationStarter;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// TODO:
|
|
||||||
// 1. Create at least 3 courses
|
Course AdvancedProgramming = new Course("AdvancedProgramming", "AP1404", 2);
|
||||||
// 2. Create 1 student
|
Course DataStructures = new Course("DataStructures", "DS2201", 1);
|
||||||
// 3. Create a registration system
|
Course DatabaseSystems = new Course("DatabaseSystems", "DB3301", 3);
|
||||||
// 4. Add courses to the system
|
|
||||||
// 5. Register the student in some courses
|
Student fateme = new Student("fateme", "404222000", 10);
|
||||||
// 6. Drop one course
|
|
||||||
// 7. Print the final registered course list
|
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) {
|
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) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showAllCourses() {
|
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;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Student {
|
public class Student {
|
||||||
private String name;
|
private final String name;
|
||||||
private String studentId;
|
private final String studentId;
|
||||||
private ArrayList<Course> registeredCourses;
|
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.name = name;
|
||||||
this.studentId = studentId;
|
this.studentId = studentId;
|
||||||
this.registeredCourses = new ArrayList<>();
|
this.registeredCourses = new ArrayList<>();
|
||||||
|
this.courseLimit = courseLimit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@@ -21,19 +23,35 @@ public class Student {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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.size() < courseLimit) {
|
||||||
|
registeredCourses.add(c);
|
||||||
|
c.reduceCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCourseLimit(int amount) {
|
||||||
|
if(amount>0)
|
||||||
|
courseLimit = amount;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean dropCourse(Course c) {
|
public boolean dropCourse(Course c) {
|
||||||
// TODO: Remove the course from the student's list
|
if (registeredCourses.remove(c)) {
|
||||||
// Restore the course capacity if removal was successful
|
c.increaseCapacity();
|
||||||
// Return true if the course was dropped, otherwise false
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
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