Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8332230000 |
@@ -1,27 +1,19 @@
|
|||||||
package CourseRegistrationStarter;
|
package CourseRegistrationStarter;
|
||||||
|
|
||||||
public class Course {
|
public class Course {
|
||||||
private String courseName;
|
private String name;
|
||||||
private String courseCode;
|
private String code;
|
||||||
private int capacity;
|
private int capacity;
|
||||||
|
|
||||||
public Course(String courseName, String courseCode, int capacity) {
|
public Course(String name, String code, int capacity) {
|
||||||
this.courseName = courseName;
|
this.name = name;
|
||||||
this.courseCode = courseCode;
|
this.code = code;
|
||||||
this.capacity = capacity;
|
this.capacity = capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCourseName() {
|
public String getName() { return name; }
|
||||||
return courseName;
|
public String getCode() { return code; }
|
||||||
}
|
public int getCapacity() { return capacity; }
|
||||||
|
|
||||||
public String getCourseCode() {
|
|
||||||
return courseCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCapacity() {
|
|
||||||
return capacity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean reduceCapacity() {
|
public boolean reduceCapacity() {
|
||||||
if (capacity > 0) {
|
if (capacity > 0) {
|
||||||
|
|||||||
@@ -4,72 +4,58 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Course course1 = new Course("َAP", "1414", 2);
|
Scanner scanner = new Scanner(System.in);
|
||||||
Course course2 = new Course("DS", "2201", 1);
|
RegistrationSystem system = new RegistrationSystem();
|
||||||
Course course3 = new Course("DB", "3301", 3);
|
|
||||||
Student student1 = new Student("Ali", "402222001");
|
|
||||||
RegistrationSystem registrationSystem = new RegistrationSystem();
|
|
||||||
registrationSystem.addCourseToSystem(course1);
|
|
||||||
registrationSystem.addCourseToSystem(course2);
|
|
||||||
registrationSystem.addCourseToSystem(course3);
|
|
||||||
|
|
||||||
boolean isChosen = false;
|
|
||||||
while (!isChosen)
|
|
||||||
{
|
|
||||||
System.out.println("\n--- Menu ---");
|
|
||||||
System.out.println("1) Add course");
|
|
||||||
System.out.println("2) Drop course");
|
|
||||||
System.out.println("3) Show registered courses");
|
|
||||||
System.out.println("4) Show all courses");
|
|
||||||
System.out.println("5) Exit");
|
|
||||||
System.out.print("Enter your choice: ");
|
|
||||||
Scanner in = new Scanner(System.in);
|
|
||||||
int chosen = in.nextInt();
|
|
||||||
if (chosen == 1)
|
|
||||||
{
|
|
||||||
System.out.print("Enter Course Code to add: ");
|
|
||||||
String codeToAdd = in.next();
|
|
||||||
Course courseToAdd = registrationSystem.findCourseByCode(codeToAdd);
|
|
||||||
|
|
||||||
if (courseToAdd != null)
|
system.addCourseToSystem(new Course("Advanced Programming", "CS101", 30));
|
||||||
{
|
system.addCourseToSystem(new Course("Data Structures", "CS102", 25));
|
||||||
student1.addCourse(courseToAdd);
|
system.addCourseToSystem(new Course("Database Systems", "CS103", 20));
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
System.out.println("Error: Course not found in system.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (chosen == 2)
|
|
||||||
{
|
|
||||||
System.out.println("\n--- Drop Course ---");
|
|
||||||
System.out.print("Enter Course Code to drop: ");
|
|
||||||
String codeToDrop = in.next();
|
|
||||||
Course courseToDrop = registrationSystem.findCourseByCode(codeToDrop);
|
|
||||||
|
|
||||||
if (courseToDrop != null)
|
|
||||||
{
|
System.out.print("Enter Student Name: ");
|
||||||
student1.dropCourse(courseToDrop);
|
String sName = scanner.nextLine();
|
||||||
}
|
System.out.print("Enter Student ID: ");
|
||||||
}
|
String sId = scanner.nextLine();
|
||||||
else if (chosen == 3)
|
Student student = new Student(sName, sId);
|
||||||
{
|
|
||||||
System.out.println("\n--- Registered Courses ---");
|
boolean running = true;
|
||||||
student1.showRegisteredCourses();
|
while (running) {
|
||||||
}
|
System.out.println("\n1. View All Courses\n2. Add Course\n3. Drop Course\n4. My Courses\n5. Exit");
|
||||||
else if (chosen == 4)
|
System.out.print("Choice: ");
|
||||||
{
|
String choice = scanner.nextLine();
|
||||||
System.out.println("\n--- All Available Courses ---");
|
|
||||||
registrationSystem.showAllCourses();
|
switch (choice) {
|
||||||
}
|
case "1":
|
||||||
else if (chosen == 5)
|
system.showAllCourses();
|
||||||
{
|
break;
|
||||||
isChosen = true;
|
case "2":
|
||||||
}
|
System.out.print("Enter course code to add: ");
|
||||||
else
|
String addCode = scanner.nextLine();
|
||||||
{
|
Course toAdd = system.findCourseByCode(addCode);
|
||||||
System.out.println("Invalid! Please enter a number between 1 and 5.");
|
if (toAdd != null) {
|
||||||
|
if (student.addCourse(toAdd)) System.out.println("Successfully added.");
|
||||||
|
} else {
|
||||||
|
System.out.println("Course not found!");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case "3":
|
||||||
|
System.out.print("Enter course code to drop: ");
|
||||||
|
String dropCode = scanner.nextLine();
|
||||||
|
if (student.dropCourse(dropCode)) System.out.println("Successfully dropped.");
|
||||||
|
else System.out.println("You are not registered in this course.");
|
||||||
|
break;
|
||||||
|
case "4":
|
||||||
|
student.showRegisteredCourses();
|
||||||
|
break;
|
||||||
|
case "5":
|
||||||
|
running = false;
|
||||||
|
System.out.println("Exiting...");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Invalid option.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
scanner.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +1,29 @@
|
|||||||
package CourseRegistrationStarter;
|
package CourseRegistrationStarter;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class RegistrationSystem {
|
public class RegistrationSystem {
|
||||||
private ArrayList<Course> courses;
|
private ArrayList<Course> allCourses;
|
||||||
|
|
||||||
public RegistrationSystem() {
|
public RegistrationSystem() {
|
||||||
courses = new ArrayList<>();
|
this.allCourses = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addCourseToSystem(Course c) {
|
public void addCourseToSystem(Course course) {
|
||||||
courses.add(c);
|
allCourses.add(course);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Course findCourseByCode(String code) {
|
public Course findCourseByCode(String code) {
|
||||||
for (Course a : courses)
|
for (Course c : allCourses) {
|
||||||
{
|
if (c.getCode().equalsIgnoreCase(code)) return c;
|
||||||
if (a.getCourseCode().equals(code))
|
|
||||||
{
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
public Course findCourseByName(String name)
|
|
||||||
{
|
|
||||||
for (Course a : courses)
|
|
||||||
{
|
|
||||||
if (a.getCourseName().equals(name))
|
|
||||||
{
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showAllCourses() {
|
public void showAllCourses() {
|
||||||
for (Course c : courses)
|
System.out.println("\n--- Available Courses in System ---");
|
||||||
{
|
for (Course c : allCourses) {
|
||||||
System.out.println(c.getCourseName() + " - " + c.getCourseCode() + " - capacity: " + c.getCapacity());
|
System.out.println("Name: " + c.getName() + " | Code: " + c.getCode() + " | Capacity: " + c.getCapacity());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,79 +1,61 @@
|
|||||||
package CourseRegistrationStarter;
|
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 id;
|
||||||
private ArrayList<Course> registeredCourses;
|
private ArrayList<Course> registeredCourses;
|
||||||
|
private final int MAX_COURSES = 5;
|
||||||
|
|
||||||
public Student(String name, String studentId) {
|
public Student(String name, String id) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.studentId = studentId;
|
this.id = id;
|
||||||
this.registeredCourses = new ArrayList<>();
|
this.registeredCourses = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public boolean addCourse(Course course) {
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getStudentId() {
|
if (registeredCourses.size() >= MAX_COURSES) {
|
||||||
return studentId;
|
System.out.println("Error: Maximum course limit reached (5).");
|
||||||
}
|
|
||||||
|
|
||||||
public boolean addCourse(Course c) {
|
|
||||||
if (registeredCourses.size() == 5)
|
|
||||||
{
|
|
||||||
System.out.println("Maximum course limit (5) reached.");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (Course a : registeredCourses)
|
|
||||||
{
|
for (Course c : registeredCourses) {
|
||||||
if (a.getCourseCode().equals(c.getCourseCode()))
|
if (c.getCode().equals(course.getCode())) {
|
||||||
{
|
System.out.println("Error: Already registered in " + course.getName());
|
||||||
System.out.println("Error: you are already registered for this course!");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (c.getCapacity() > 0)
|
|
||||||
{
|
if (course.reduceCapacity()) {
|
||||||
registeredCourses.add(c);
|
registeredCourses.add(course);
|
||||||
c.reduceCapacity();
|
|
||||||
System.out.println("Registration was successful.");
|
|
||||||
return true;
|
return true;
|
||||||
|
} else {
|
||||||
|
System.out.println("Error: No capacity left in " + course.getName());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean dropCourse(String courseCode) {
|
||||||
|
for (Course c : registeredCourses) {
|
||||||
|
if (c.getCode().equals(courseCode)) {
|
||||||
|
c.increaseCapacity();
|
||||||
|
registeredCourses.remove(c);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean dropCourse(Course c) {
|
|
||||||
Course courseToRempove = null;
|
|
||||||
for (Course a : registeredCourses)
|
|
||||||
{
|
|
||||||
if (a.getCourseCode().equals(c.getCourseCode()))
|
|
||||||
{
|
|
||||||
courseToRempove = a;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (courseToRempove == null)
|
|
||||||
{
|
|
||||||
System.out.println("Error: you are not registered for this course!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
registeredCourses.remove(courseToRempove);
|
|
||||||
System.out.println("Successfully dropped course.");
|
|
||||||
c.increaseCapacity();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void showRegisteredCourses() {
|
public void showRegisteredCourses() {
|
||||||
if (registeredCourses.isEmpty())
|
System.out.println("\n--- Registered Courses for " + name + " (" + id + ") ---");
|
||||||
{
|
if (registeredCourses.isEmpty()) {
|
||||||
System.out.println("No courses registered.");
|
System.out.println("No courses registered yet.");
|
||||||
return;
|
} else {
|
||||||
}
|
for (Course c : registeredCourses) {
|
||||||
for(Course a : registeredCourses)
|
System.out.println("- " + c.getName() + " [" + c.getCode() + "]");
|
||||||
{
|
}
|
||||||
System.out.println(a.getCourseName() + " - " + a.getCourseCode());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user