This commit is contained in:
neda khosrojerdi
2026-04-27 02:46:12 +03:30
parent 8ddd529c85
commit 9fad2d020b
3 changed files with 147 additions and 28 deletions
+84 -8
View File
@@ -1,14 +1,90 @@
package CourseRegistrationStarter;
import java.util.Scanner;
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
Scanner input = new Scanner(System.in);
Course c1 = new Course("Advanced Programming", "AP1404", 2);
Course c2 = new Course("Data Structures", "DS2201", 1);
Course c3 = new Course("Database Systems", "DB3301", 3);
Student student = new Student("Ali", "402222001");
RegistrationSystem system = new RegistrationSystem();
system.addCourseToSystem(c1);
system.addCourseToSystem(c2);
system.addCourseToSystem(c3);
int choice;
do {
System.out.println();
System.out.println("1. Show all courses");
System.out.println("2. Register course");
System.out.println("3. Drop course");
System.out.println("4. Show my courses");
System.out.println("5. Exit");
System.out.print("Choose: ");
choice = input.nextInt();
input.nextLine();
switch (choice) {
case 1:
system.showAllCourses();
break;
case 2:
System.out.print("Enter course code: ");
String code = input.nextLine();
Course course = system.findCourseByCode(code);
if (course != null) {
student.addCourse(course);
} else {
System.out.println("Course not found.");
}
break;
case 3:
System.out.print("Enter course code: ");
String dropCode = input.nextLine();
Course dropCourse = system.findCourseByCode(dropCode);
if (dropCourse != null) {
student.dropCourse(dropCourse);
} else {
System.out.println("Course not found.");
}
break;
case 4:
student.showRegisteredCourses();
break;
case 5:
System.out.println("Goodbye");
break;
default:
System.out.println("Invalid choice");
}
} while (choice != 5);
}
}
@@ -1,7 +1,9 @@
package CourseRegistrationStarter;
import java.util.ArrayList;
public class RegistrationSystem {
private ArrayList<Course> courses;
public RegistrationSystem() {
@@ -9,16 +11,34 @@ 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 (Course c : courses) {
if (c.getCourseCode().equalsIgnoreCase(code)) {
return c;
}
}
return null;
}
public void showAllCourses() {
// TODO: Print all courses in the system
for (Course c : courses) {
System.out.println(
c.getCourseName() +
" (" + c.getCourseCode() + ")" +
" - Capacity: " +
c.getCapacity()
);
}
}
}
+39 -16
View File
@@ -1,7 +1,9 @@
package CourseRegistrationStarter;
import java.util.ArrayList;
public class Student {
private String name;
private String studentId;
private ArrayList<Course> registeredCourses;
@@ -9,31 +11,52 @@ public class Student {
public Student(String name, String studentId) {
this.name = name;
this.studentId = studentId;
this.registeredCourses = new ArrayList<>();
}
public String getName() {
return name;
}
public String getStudentId() {
return studentId;
registeredCourses = new ArrayList<>();
}
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)) {
System.out.println("Already registered in this course.");
return false;
}
if (c.reduceCapacity()) {
registeredCourses.add(c);
System.out.println("Course registered successfully.");
return true;
}
System.out.println("Course capacity is full.");
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
if (registeredCourses.remove(c)) {
c.increaseCapacity();
System.out.println("Course dropped successfully.");
return true;
}
System.out.println("You are not registered in this course.");
return false;
}
public void showRegisteredCourses() {
// TODO: Print all registered courses
if (registeredCourses.isEmpty()) {
System.out.println("No registered courses.");
return;
}
System.out.println("Your Courses:");
for (Course c : registeredCourses) {
System.out.println(
c.getCourseName() + " (" + c.getCourseCode() + ")"
);
}
}
}
}