3 Commits
3 changed files with 118 additions and 0 deletions
+96
View File
@@ -1,8 +1,25 @@
package CourseRegistrationStarter; package CourseRegistrationStarter;
import java.util.ArrayList;
import java.util.Scanner;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
ArrayList<Student> students= new ArrayList<>();
// TODO: // TODO:
Course BP = new Course("Basic Programming", "BP1404", 3);
Course AP = new Course("Advanced Programming", "AP1405", 2);
Course DS = new Course("Data Structure", "DS1406", 2);
Student saeedJam = new Student("Saeed Jamali", "404222000");
RegistrationSystem register = new RegistrationSystem();
register.addCourseToSystem(BP);
register.addCourseToSystem(AP);
register.addCourseToSystem(DS);
saeedJam.addCourse(AP);
saeedJam.addCourse(DS);
saeedJam.dropCourse(DS);
saeedJam.showRegisteredCourses();
// 1. Create at least 3 courses // 1. Create at least 3 courses
// 2. Create 1 student // 2. Create 1 student
// 3. Create a registration system // 3. Create a registration system
@@ -10,5 +27,84 @@ public class Main {
// 5. Register the student in some courses // 5. Register the student in some courses
// 6. Drop one course // 6. Drop one course
// 7. Print the final registered course list // 7. Print the final registered course list
int command = 100;
Scanner scanner = new Scanner(System.in);
while (command != 0) {
if (command == 1) {
String courseName = "";
String courseCode = "";
int courseCapacity = 0;
System.out.println("Please enter course name: ");
courseName = scanner.nextLine();
System.out.println("Enter course code: ");
courseCode = scanner.nextLine();
System.out.println("Enter course capacity: ");
courseCapacity = scanner.nextInt();
scanner.nextLine();
register.addCourseToSystem(new Course(courseName, courseCode, courseCapacity));
System.out.println("Course added successfully.");
} else if (command == 2) {
String studentName = "";
String studentId = "";
System.out.println("Please enter student name: ");
studentName = scanner.nextLine();
System.out.println("Enter student ID: ");
studentId = scanner.nextLine();
students.add(new Student(studentName, studentId));
System.out.println("Student added successfully.");
} else if (command == 3) {
String studentId = "";
String courseCode = "";
Student studentToAdd;
System.out.println("Please enter student ID: ");
studentId = scanner.nextLine();
System.out.println("Enter course code: ");
courseCode = scanner.nextLine();
studentToAdd = null;
studentToAdd = findStudentById(students, studentId);
Course courseToAdd = register.findCourseByCode(courseCode);
studentToAdd.addCourse(courseToAdd);
System.out.println("Course "+ courseCode + " added successfuly for Student " + studentId);
} else if (command == 4) {
String studentId = "";
Student studentToShow = null;
System.out.println("Please enter student ID: ");
studentId = scanner.nextLine();
studentToShow = findStudentById(students, studentId);
studentToShow.showRegisteredCourses();
} else if (command == 5) {
String courseCode = "";
System.out.println("Enter course code: ");
courseCode = scanner.nextLine();
Course courseToShow = register.findCourseByCode(courseCode);
System.out.println(courseToShow.getCourseName() + ", Code: "
+ courseToShow.getCourseCode() + ", Capacity: " + courseToShow.getCapacity());
} else if (command == 6) {
register.showAllCourses();
}
System.out.println("----------------MENU----------------");
System.out.println("1-Add course to system");
System.out.println("2-Add student");
System.out.println("3-Add course for student");
System.out.println("4-Show student courses");
System.out.println("5-Find course by code");
System.out.println("6-Show all courses");
System.out.println("0-Exit\n");
command = scanner.nextInt();
scanner.nextLine();
}
} }
public static Student findStudentById(ArrayList<Student> studentList, String id) {
for (Student s: studentList) {
if (s.getStudentId().equals(id)) {
return s;
}
}
return null;
}
} }
@@ -9,16 +9,25 @@ public class RegistrationSystem {
} }
public void addCourseToSystem(Course c) { public void addCourseToSystem(Course c) {
this.courses.add(c);
// TODO: Add the course to the system // TODO: Add the course to the system
} }
public Course findCourseByCode(String code) { public Course findCourseByCode(String code) {
for (Course c: this.courses) {
if (c.getCourseCode().equals(code)) {
return c;
}
}
// TODO: Search for a course by course code // TODO: Search for a course by course code
// Return the matching course if found, otherwise return null // Return the matching course if found, otherwise return null
return null; return null;
} }
public void showAllCourses() { public void showAllCourses() {
for(Course c: this.courses) {
System.out.println(c.getCourseName() + ", code: " + c.getCourseCode() + ", capacity: " + c.getCapacity());
}
// TODO: Print all courses in the system // TODO: Print all courses in the system
} }
} }
@@ -21,12 +21,22 @@ public class Student {
} }
public boolean addCourse(Course c) { public boolean addCourse(Course c) {
if (c.getCapacity() > 0 && !this.registeredCourses.contains(c)) {
this.registeredCourses.add(c);
c.reduceCapacity();
return true;
}
// TODO: Register the student in the course if capacity allows // TODO: Register the student in the course if capacity allows
// Return true if registration was successful, otherwise false // Return true if registration was successful, otherwise false
return false; return false;
} }
public boolean dropCourse(Course c) { public boolean dropCourse(Course c) {
if (this.registeredCourses.contains(c)) {
this.registeredCourses.remove(c);
c.increaseCapacity();
return true;
}
// TODO: Remove the course from the student's list // TODO: Remove the course from the student's list
// Restore the course capacity if removal was successful // Restore the course capacity if removal was successful
// Return true if the course was dropped, otherwise false // Return true if the course was dropped, otherwise false
@@ -34,6 +44,9 @@ public class Student {
} }
public void showRegisteredCourses() { public void showRegisteredCourses() {
for(Course c: this.registeredCourses) {
System.out.println(c.getCourseName() + ", code: " + c.getCourseCode());
}
// TODO: Print all registered courses // TODO: Print all registered courses
} }
} }