Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2463d1ed91 | ||
|
|
3195b392af | ||
|
|
cd3436131d | ||
|
|
e8bafc4140 |
@@ -1,14 +1,26 @@
|
||||
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
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Course c1 = new Course("Advanced Programming","4041",40);
|
||||
Course c2 = new Course("Math","4042",50);
|
||||
Course c3 = new Course("English","4043",20);
|
||||
|
||||
RegistrationSystem registrationSystem = new RegistrationSystem();
|
||||
|
||||
registrationSystem.addCourseToSystem(c1);
|
||||
registrationSystem.addCourseToSystem(c2);
|
||||
registrationSystem.addCourseToSystem(c3);
|
||||
|
||||
Student st1 = new Student("Lionel Messi","404222010");
|
||||
|
||||
st1.addCourse(c1);
|
||||
st1.addCourse(c2);
|
||||
st1.addCourse(c3);
|
||||
|
||||
st1.dropCourse(c2);
|
||||
|
||||
st1.showRegisteredCourses();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,54 @@
|
||||
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) {
|
||||
// TODO: Add the course to the system
|
||||
public void addCourseToSystem(Course c)
|
||||
{
|
||||
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
|
||||
public Course findCourseByCode(String code)
|
||||
{
|
||||
if(courses.isEmpty())
|
||||
{
|
||||
System.out.println("NOTHING HAS FOUND !");
|
||||
return null;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(int i = 0; i < courses.size(); i++)
|
||||
{
|
||||
Course c = courses.get(i);
|
||||
if(code.equals(c.getCourseCode()))
|
||||
{
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("NOTHING HAS FOUND !");
|
||||
return null;
|
||||
}
|
||||
|
||||
public void showAllCourses() {
|
||||
// TODO: Print all courses in the system
|
||||
public void showAllCourses()
|
||||
{
|
||||
if(courses.isEmpty())
|
||||
{
|
||||
System.out.println("NOTHING HAS FOUND !");
|
||||
}
|
||||
|
||||
for(int i = 0; i < courses.size(); i++)
|
||||
{
|
||||
Course c = courses.get(i);
|
||||
System.out.println("Course "+ (i + 1) + " : " + c.getCourseName()
|
||||
+ " | code : " + c.getCourseCode()
|
||||
+ " | cpacity : " + c.getCapacity());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,15 @@
|
||||
package CourseRegistrationStarter;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Student {
|
||||
public class Student
|
||||
{
|
||||
private String name;
|
||||
private String studentId;
|
||||
private ArrayList<Course> registeredCourses;
|
||||
private int limit = 15;
|
||||
|
||||
public Student(String name, String studentId) {
|
||||
public Student(String name, String studentId)
|
||||
{
|
||||
this.name = name;
|
||||
this.studentId = studentId;
|
||||
this.registeredCourses = new ArrayList<>();
|
||||
@@ -20,20 +23,45 @@ public class Student {
|
||||
return studentId;
|
||||
}
|
||||
|
||||
public boolean addCourse(Course c) {
|
||||
// TODO: Register the student in the course if capacity allows
|
||||
// Return true if registration was successful, otherwise false
|
||||
public boolean addCourse(Course c)
|
||||
{
|
||||
if(registeredCourses.contains(c) || c.getCapacity() == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if(registeredCourses.size() <= limit && c.getCapacity() > 0)
|
||||
{
|
||||
c.reduceCapacity();
|
||||
registeredCourses.add(c);
|
||||
return true;
|
||||
}
|
||||
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
|
||||
public boolean dropCourse(Course c)
|
||||
{
|
||||
if(registeredCourses.contains(c))
|
||||
{
|
||||
registeredCourses.remove(c);
|
||||
c.increaseCapacity();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void showRegisteredCourses() {
|
||||
// TODO: Print all registered courses
|
||||
public void showRegisteredCourses()
|
||||
{
|
||||
if(registeredCourses.size() > 0)
|
||||
{
|
||||
System.out.println("Registered Courses : ");
|
||||
for(int i = 0; i < registeredCourses.size(); i++) {
|
||||
Course c = registeredCourses.get(i);
|
||||
System.out.println(c.getCourseName());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
System.out.println("NO COURSES !");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user