This commit is contained in:
2026-04-21 12:19:40 +03:30
parent 8ddd529c85
commit 00cbb1c0e4
5 changed files with 158 additions and 36 deletions
+1
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
@@ -35,3 +35,4 @@ public class Course {
capacity++;
}
}
+91 -12
View File
@@ -1,14 +1,93 @@
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
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// Courses
Course ap = new Course("Advanced Programming", "AP1404", 2);
Course ds = new Course("Data Structures", "DS2201", 1);
Course db = new Course("Database Systems", "DB3301", 3);
// System
RegistrationSystem system = new RegistrationSystem();
system.addCourseToSystem(ap);
system.addCourseToSystem(ds);
system.addCourseToSystem(db);
// Student
System.out.print("Enter student name: ");
String name = input.nextLine();
System.out.print("Enter student id: ");
String id = input.nextLine();
Student student = new Student(name, id);
int choice = 0;
while (choice != 5)
{
System.out.println("\n--- MENU ---");
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("Choice: ");
choice = input.nextInt();
input.nextLine();
switch (choice)
{
case 1:
system.showAllCourses();
break;
case 2:
System.out.print("Enter course code: ");
String regCode = input.nextLine();
Course c1 = system.findCourseByCode(regCode);
if (c1 != null)
{
student.addCourse(c1);
} else
{
System.out.println("Course not found");
}
break;
case 3:
System.out.print("Enter course code: ");
String dropCode = input.nextLine();
Course c2 = system.findCourseByCode(dropCode);
if (c2 != null)
{
student.dropCourse(c2);
} 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");
}
}
}
}
}
@@ -1,24 +1,44 @@
package CourseRegistrationStarter;
import java.util.ArrayList;
public class RegistrationSystem {
public class RegistrationSystem
{
private ArrayList<Course> courses;
public RegistrationSystem() {
public RegistrationSystem()
{
courses = new ArrayList<>();
}
public void addCourseToSystem(Course c) {
// TODO: Add the course to the system
public void addCourseToSystem(Course c)
{
// 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
public Course findCourseByCode(String code)
{
// Search for a course by course code
for (Course c : courses)
{
if (c.getCourseCode().equals(code))
{
return c;
}
}
return null;
}
public void showAllCourses() {
// TODO: Print all courses in the system
public void showAllCourses()
{
// Print all courses in the system
for (Course c : courses)
{
System.out.println(
c.getCourseName() + " - " +
c.getCourseCode() +
" | Remaining capacity: " +
c.getCapacity()
);
}
}
}
+35 -14
View File
@@ -1,39 +1,60 @@
package CourseRegistrationStarter;
import java.util.ArrayList;
public class Student {
public class Student
{
private String name;
private String studentId;
private ArrayList<Course> registeredCourses;
public Student(String name, String studentId) {
public Student(String name, String studentId)
{
this.name = name;
this.studentId = studentId;
this.registeredCourses = new ArrayList<>();
}
public String getName() {
public String getName()
{
return name;
}
public String getStudentId() {
public String getStudentId()
{
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)
{
// Check capacity > 0
if (c.getCapacity() > 0)
{
registeredCourses.add(c);
c.reduceCapacity();
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.remove(c))
{
c.increaseCapacity();
return true;
}
return false;
}
public void showRegisteredCourses() {
// TODO: Print all registered courses
public void showRegisteredCourses()
{
System.out.println("Registered Courses for " + name + ":");
if (registeredCourses.isEmpty())
{
System.out.println("No courses registered.");
}
for (Course c : registeredCourses)
{
System.out.println(c.getCourseName() + " - " + c.getCourseCode());
}
}
}