1 Commits
Author SHA1 Message Date
Saba_frm 00cbb1c0e4 develop 2026-04-21 12:19:40 +03:30
5 changed files with 158 additions and 36 deletions
+1
View File
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
@@ -35,3 +35,4 @@ public class Course {
capacity++; capacity++;
} }
} }
+91 -12
View File
@@ -1,14 +1,93 @@
package CourseRegistrationStarter; import java.util.Scanner;
public class Main { public class Main
public static void main(String[] args) { {
// TODO:
// 1. Create at least 3 courses public static void main(String[] args)
// 2. Create 1 student {
// 3. Create a registration system
// 4. Add courses to the system Scanner input = new Scanner(System.in);
// 5. Register the student in some courses
// 6. Drop one course // Courses
// 7. Print the final registered course list 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; 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 {
// 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 // Search for a course by course code
for (Course c : courses)
{
if (c.getCourseCode().equals(code))
{
return c;
}
}
return null; return null;
} }
public void showAllCourses() { public void showAllCourses()
// TODO: Print all courses in the system {
// 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; 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;
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<>();
} }
public String getName() { public String getName()
{
return name; return name;
} }
public String getStudentId() { public String getStudentId()
{
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 // Check capacity > 0
if (c.getCapacity() > 0)
{
registeredCourses.add(c);
c.reduceCapacity();
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.remove(c))
// Return true if the course was dropped, otherwise false {
c.increaseCapacity();
return true;
}
return false; return false;
} }
public void showRegisteredCourses() { public void showRegisteredCourses()
// TODO: Print all registered courses {
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());
}
} }
} }