Complete course registration system + bonus features #1

Merged
peyman merged 1 commits from dev into main 2026-04-25 21:48:53 +00:00
3 changed files with 152 additions and 20 deletions
+98 -8
View File
@@ -1,14 +1,104 @@
package CourseRegistrationStarter;
import java.util.Scanner;
public class Main {
static Scanner input = new Scanner(System.in);
static Course x1 =new Course("Advanced Programming","AP1404",2);
static Course x2 =new Course("Data Structures","DS2201",1);
static Course x3 =new Course("Database Systems","DB3301",3);
static RegistrationSystem n3 = new RegistrationSystem();
static Student st = new Student("navid","41148");
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
n3.addCourseToSystem(x1);
n3.addCourseToSystem(x2);
n3.addCourseToSystem(x3);
while(true){
asli();
}
}
public static boolean asli(){
System.out.println("----Course Registration Menu ---"+"\n"+"1. Show all courses "+"\n"+"2. Register course"+"\n"+"3. Drop course"+"\n"+"4. Show my courses"+"\n"+"5. Exit"+"\n"+"Choose an option: ");
int n1 = input.nextInt();
if(n1 == 1){
System.out.println("All courses in system: ");
n3.showAllCourses();
return (true);
}else if (n1 ==2){
System.out.println("Choos an option: ");
System.out.println("1. Enter course code ");
System.out.println("2. Enter course name ");
int n2 = input.nextInt();
input.nextLine();
if (n2 == 1){
System.out.println("Enter course code to register: ");
String courseCode = input.nextLine();
if(n3.findCourseByCode(courseCode) != null ){
if (st.addCourse(n3.findCourseByCode(courseCode))){
System.out.println("Registration was successful."+"\n");
return(true);
}
}else{
System.out.println("No course found with this code!"+"\n");
return(true);
}
}else if (n2 == 2){
System.out.println("Enter course name to register: ");
String courseName = input.nextLine();
if(n3.findCourseByName(courseName) != null ){
if (st.addCourse(n3.findCourseByName(courseName))){
System.out.println("Registration was successful."+"\n");
return(true);
}
}else{
System.out.println("No course found with this name!"+"\n");
return(true);
}
}
}else if (n1==3){
System.out.println("Choos an option: ");
System.out.println("1. Enter course code ");
System.out.println("2. Enter course name ");
int n2 = input.nextInt();
input.nextLine();
if (n2 == 1){
System.out.println("Enter course code to register: ");
String courseCode = input.nextLine();
if(n3.findCourseByCode(courseCode) != null ){
if (st.dropCourse(n3.findCourseByCode(courseCode))){
System.out.println("Course dropped successfully."+"\n");
return (true);
}
}else{
System.out.println("No course found with this code!"+"\n");
return(true);
}
}else if (n2==2){
System.out.println("Enter course name to register: ");
String courseName = input.nextLine();
if(n3.findCourseByName(courseName) != null ){
if (st.dropCourse(n3.findCourseByName(courseName))){
System.out.println("Course dropped successfully."+"\n");
return (true);
}
}else{
System.out.println("No course found with this name!"+"\n");
return(true);
}
}
return (true);
}else if (n1==4 ){
System.out.println("Registered courses for "+ st.getName()+":");
st.showRegisteredCourses();
return (true);
}else if (n1==5 ){
System.exit(0);
return (true);
}
return false;
}
}
@@ -9,16 +9,33 @@ public class RegistrationSystem {
}
public void addCourseToSystem(Course c) {
// TODO: Add the course to the system
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()-1; i++){
if (code.equals(courses.get(i).getCourseCode())){
return (courses.get(i));
}
}
return null;
}
public Course findCourseByName(String name){
for(int i = 0 ; i < courses.size(); i++){
if (name.equals(courses.get(i).getCourseName())){
return (courses.get(i));
}
}
return null;
}
public void showAllCourses() {
// TODO: Print all courses in the system
for (int i = 0; i <= courses.size() - 1; i++) {
System.out.println(courses.get(i).getCourseName() + "(" + courses.get(i).getCourseCode() + ")" + " = Remaining capacity: " + courses.get(i).getCapacity() + "\n");
}
}
}
+33 -8
View File
@@ -21,19 +21,44 @@ 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
return false;
if (registeredCourses.contains(c)){
System.out.println("You have already registered in this course!"+"\n");
return (false);
} else if (registeredCourses.size()== 2){
System.out.println("You have reached the maximum number of allowed courses!"+"\n");
return (false);
}
else if (c.getCapacity()>0){
registeredCourses.add(c);
c.reduceCapacity();
return (true);
}else{
System.out.println("Registration was not successful!"+"\n"+"This course has no capacity left."+"\n");
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
return false;
if (registeredCourses.contains(c)){
registeredCourses.remove(c);
c.increaseCapacity();
return (true);
}else{
System.out.println("Course drop failed!"+"\n"+"You are not registered for this course."+"\n");
return (false);
}
}
public void showRegisteredCourses() {
// TODO: Print all registered courses
if(registeredCourses.size()==0){
System.out.println(Main.st.getName() + "hasn't registered for any course."+"\n");
}
else {
for (int i = 0; i <= registeredCourses.size() - 1; i++) {
System.out.println(registeredCourses.get(i).getCourseName() + "(" + registeredCourses.get(i).getCourseCode() + ")" + "\n");
}
}
}
}