Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2463d1ed91 | ||
|
|
3195b392af | ||
|
|
cd3436131d | ||
|
|
e8bafc4140 |
@@ -1,14 +1,26 @@
|
|||||||
package CourseRegistrationStarter;
|
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 c1 = new Course("Advanced Programming","4041",40);
|
||||||
// 2. Create 1 student
|
Course c2 = new Course("Math","4042",50);
|
||||||
// 3. Create a registration system
|
Course c3 = new Course("English","4043",20);
|
||||||
// 4. Add courses to the system
|
|
||||||
// 5. Register the student in some courses
|
RegistrationSystem registrationSystem = new RegistrationSystem();
|
||||||
// 6. Drop one course
|
|
||||||
// 7. Print the final registered course list
|
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;
|
package CourseRegistrationStarter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class RegistrationSystem {
|
public class RegistrationSystem
|
||||||
|
{
|
||||||
private ArrayList<Course> courses;
|
private ArrayList<Course> courses;
|
||||||
|
|
||||||
public RegistrationSystem() {
|
public RegistrationSystem() {
|
||||||
courses = new ArrayList<>();
|
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
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showAllCourses() {
|
public void showAllCourses()
|
||||||
// TODO: Print all courses in the system
|
{
|
||||||
|
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;
|
package CourseRegistrationStarter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Student {
|
public class Student
|
||||||
|
{
|
||||||
private String name;
|
private String name;
|
||||||
private String studentId;
|
private String studentId;
|
||||||
private ArrayList<Course> registeredCourses;
|
private ArrayList<Course> registeredCourses;
|
||||||
|
private int limit = 15;
|
||||||
|
|
||||||
public Student(String name, String studentId) {
|
public Student(String name, String studentId)
|
||||||
|
{
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.studentId = studentId;
|
this.studentId = studentId;
|
||||||
this.registeredCourses = new ArrayList<>();
|
this.registeredCourses = new ArrayList<>();
|
||||||
@@ -20,20 +23,45 @@ public class Student {
|
|||||||
return studentId;
|
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(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;
|
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
|
if(registeredCourses.contains(c))
|
||||||
// Return true if the course was dropped, otherwise false
|
{
|
||||||
|
registeredCourses.remove(c);
|
||||||
|
c.increaseCapacity();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
public void showRegisteredCourses()
|
||||||
// TODO: Print all registered courses
|
{
|
||||||
|
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