diff --git a/.gitignore b/.gitignore index 2b76d7c..fbd93fe 100644 --- a/.gitignore +++ b/.gitignore @@ -155,4 +155,6 @@ dist # Vite logs files vite.config.js.timestamp-* vite.config.ts.timestamp-* +# Java class files +*.class diff --git a/task-1/com.hyfacademy/Main.java b/task-1/com.hyfacademy/Main.java deleted file mode 100644 index d97363c..0000000 --- a/task-1/com.hyfacademy/Main.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.hyfacademy; - -public class Main { - static void main(String[] args) { - System.out.println("Hello World!"); - } -} diff --git a/task-1/com.hyfacademy/exception/AlreadyEnrolledException.java b/task-1/com.hyfacademy/exception/AlreadyEnrolledException.java deleted file mode 100644 index a82837e..0000000 --- a/task-1/com.hyfacademy/exception/AlreadyEnrolledException.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.exception; - -public class AlreadyEnrolledException { -} diff --git a/task-1/com.hyfacademy/exception/CourseFullException.java b/task-1/com.hyfacademy/exception/CourseFullException.java deleted file mode 100644 index 11a76f1..0000000 --- a/task-1/com.hyfacademy/exception/CourseFullException.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.exception; - -public class CourseFullException { -} diff --git a/task-1/com.hyfacademy/exception/EnrolmentException.java b/task-1/com.hyfacademy/exception/EnrolmentException.java deleted file mode 100644 index 8454c32..0000000 --- a/task-1/com.hyfacademy/exception/EnrolmentException.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.exception; - -public class EnrolmentException { -} diff --git a/task-1/com.hyfacademy/exception/InvalidProgressException.java b/task-1/com.hyfacademy/exception/InvalidProgressException.java deleted file mode 100644 index c0c0037..0000000 --- a/task-1/com.hyfacademy/exception/InvalidProgressException.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.exception; - -public class InvalidProgressException { -} diff --git a/task-1/com.hyfacademy/model/Course.java b/task-1/com.hyfacademy/model/Course.java deleted file mode 100644 index e80513e..0000000 --- a/task-1/com.hyfacademy/model/Course.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.model; - -public class Course { -} diff --git a/task-1/com.hyfacademy/model/LiveCohortCourse.java b/task-1/com.hyfacademy/model/LiveCohortCourse.java deleted file mode 100644 index a69fb63..0000000 --- a/task-1/com.hyfacademy/model/LiveCohortCourse.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.model; - -public class LiveCohortCourse { -} diff --git a/task-1/com.hyfacademy/model/Mentor.java b/task-1/com.hyfacademy/model/Mentor.java deleted file mode 100644 index b8738d5..0000000 --- a/task-1/com.hyfacademy/model/Mentor.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.model; - -public class Mentor { -} diff --git a/task-1/com.hyfacademy/model/SelfPacedCourse.java b/task-1/com.hyfacademy/model/SelfPacedCourse.java deleted file mode 100644 index 055d64a..0000000 --- a/task-1/com.hyfacademy/model/SelfPacedCourse.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.model; - -public class SelfPacedCourse { -} diff --git a/task-1/com.hyfacademy/model/Student.java b/task-1/com.hyfacademy/model/Student.java deleted file mode 100644 index 9f34d88..0000000 --- a/task-1/com.hyfacademy/model/Student.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.model; - -public class Student { -} diff --git a/task-1/com.hyfacademy/model/User.java b/task-1/com.hyfacademy/model/User.java deleted file mode 100644 index 43cba56..0000000 --- a/task-1/com.hyfacademy/model/User.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.model; - -public class User { -} diff --git a/task-1/com.hyfacademy/service/Enrollable.java b/task-1/com.hyfacademy/service/Enrollable.java deleted file mode 100644 index a8151b7..0000000 --- a/task-1/com.hyfacademy/service/Enrollable.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.service; - -public class Enrollable { -} diff --git a/task-1/com.hyfacademy/service/PlatformService.java b/task-1/com.hyfacademy/service/PlatformService.java deleted file mode 100644 index 4248597..0000000 --- a/task-1/com.hyfacademy/service/PlatformService.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.service; - -public class PlatformService { -} diff --git a/task-1/com.hyfacademy/service/Reportable.java b/task-1/com.hyfacademy/service/Reportable.java deleted file mode 100644 index 0c8ae05..0000000 --- a/task-1/com.hyfacademy/service/Reportable.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.hyfacademy.service; - -public class Reportable { -} diff --git a/task-1/com/hyfacademy/Main.java b/task-1/com/hyfacademy/Main.java new file mode 100644 index 0000000..de739ca --- /dev/null +++ b/task-1/com/hyfacademy/Main.java @@ -0,0 +1,10 @@ +package com.hyfacademy; + +import com.hyfacademy.service.PlatformService; + +public class Main { + public static void main(String[] args) { + PlatformService platform = new PlatformService(); + platform.run(); + } +} diff --git a/task-1/com/hyfacademy/exception/AlreadyEnrolledException.java b/task-1/com/hyfacademy/exception/AlreadyEnrolledException.java new file mode 100644 index 0000000..d3382fb --- /dev/null +++ b/task-1/com/hyfacademy/exception/AlreadyEnrolledException.java @@ -0,0 +1,17 @@ +package com.hyfacademy.exception; + +public class AlreadyEnrolledException extends EnrolmentException { + private String studentName; + private String courseName; + public AlreadyEnrolledException(String studentName, String courseName){ + super(studentName + " is already enrolled in " + courseName + " "); + this.studentName = studentName; + this.courseName = courseName; + } + public String getStudentName(){ + return studentName; + } + public String getCourseName(){ + return courseName; + } +} diff --git a/task-1/com/hyfacademy/exception/CourseFullException.java b/task-1/com/hyfacademy/exception/CourseFullException.java new file mode 100644 index 0000000..8d41dfb --- /dev/null +++ b/task-1/com/hyfacademy/exception/CourseFullException.java @@ -0,0 +1,22 @@ +package com.hyfacademy.exception; + +public class CourseFullException extends EnrolmentException { + + private final String courseName; + private final int maxCapacity; + + public CourseFullException(String courseName, int maxCapacity) { + super("Course '" + courseName + "' is full (max: " + maxCapacity + " students)"); + this.courseName = courseName; + this.maxCapacity = maxCapacity; + } + + public String getCourseName() { + return courseName; + } + + public int getMaxCapacity() { + return maxCapacity; + } +} + diff --git a/task-1/com/hyfacademy/exception/EnrolmentException.java b/task-1/com/hyfacademy/exception/EnrolmentException.java new file mode 100644 index 0000000..e26f912 --- /dev/null +++ b/task-1/com/hyfacademy/exception/EnrolmentException.java @@ -0,0 +1,7 @@ +package com.hyfacademy.exception; + +public class EnrolmentException extends RuntimeException { + public EnrolmentException(String message){ + super(message); + } +} diff --git a/task-1/com/hyfacademy/exception/InvalidProgressException.java b/task-1/com/hyfacademy/exception/InvalidProgressException.java new file mode 100644 index 0000000..0df1908 --- /dev/null +++ b/task-1/com/hyfacademy/exception/InvalidProgressException.java @@ -0,0 +1,16 @@ +package com.hyfacademy.exception; + +public class InvalidProgressException extends EnrolmentException { + + private final int attemptedValue; + + public InvalidProgressException(int attemptedValue) { + super("Progress must be between 0 and 100, but received: " + attemptedValue); + this.attemptedValue = attemptedValue; + } + + public int getAttemptedValue() { + return attemptedValue; + } +} + diff --git a/task-1/com/hyfacademy/model/Course.java b/task-1/com/hyfacademy/model/Course.java new file mode 100644 index 0000000..1188717 --- /dev/null +++ b/task-1/com/hyfacademy/model/Course.java @@ -0,0 +1,148 @@ +package com.hyfacademy.model; + +import com.hyfacademy.exception.AlreadyEnrolledException; +import com.hyfacademy.exception.CourseFullException; +import com.hyfacademy.exception.EnrolmentException; +import com.hyfacademy.exception.InvalidProgressException; +import com.hyfacademy.service.Enrollable; + +public abstract class Course implements Enrollable { + + private static int counter = 1; + + private String courseName; + private String courseId; + private int maxStudents; + private int enrolledCount; + + protected Student[] students; + protected int[] studentProgress; + + public Course(String courseName, int maxStudents) { + this.courseName = courseName; + this.maxStudents = maxStudents; + this.courseId = String.format("CRS-%03d", counter++); + + this.students = new Student[maxStudents]; + this.studentProgress = new int[maxStudents]; + this.enrolledCount = 0; + } + + + public String getCourseName() { + return courseName; + } + + public String getCourseId() { + return courseId; + } + + public int getMaxStudents() { + return maxStudents; + } + + public int getEnrolledCount() { + return enrolledCount; + } + + public Student[] getStudents() { + return students; + } + + public int[] getStudentProgressArray() { + return studentProgress; + } + + + @Override + public boolean isFull() { + return enrolledCount >= maxStudents; + } + + @Override + public void enrol(Student student) throws EnrolmentException { + + if (isFull()) { + throw new CourseFullException(courseName, maxStudents); + } + + + for (int i = 0; i < enrolledCount; i++) { + if (students[i].getUserId().equals(student.getUserId())) { + throw new AlreadyEnrolledException( + student.getName(), + courseName + ); + } + } + + + students[enrolledCount] = student; + studentProgress[enrolledCount] = 0; + enrolledCount++; + + student.enrol(this); + } + + + public void updateProgress(Student student, int progress) + throws EnrolmentException, InvalidProgressException { + + if (progress < 0 || progress > 100) { + throw new InvalidProgressException(progress); + } + + int index = findStudentIndex(student); + if (index == -1) { + throw new EnrolmentException( + student.getName() + " is not enrolled in '" + courseName + "'" + ); + } + + studentProgress[index] = progress; + } + + public int getStudentProgress(Student student) throws EnrolmentException { + int index = findStudentIndex(student); + if (index == -1) { + throw new EnrolmentException( + student.getName() + " is not enrolled in '" + courseName + "'" + ); + } + return studentProgress[index]; + } + + private int findStudentIndex(Student student) { + for (int i = 0; i < enrolledCount; i++) { + if (students[i].getUserId().equals(student.getUserId())) { + return i; + } + } + return -1; + } + + protected String progressBar(int progress) { + long filled = Math.round(progress / 10.0); + int empty = (int)(10 - filled); + + StringBuilder bar = new StringBuilder(); + for (int i = 0; i < filled; i++) bar.append("█"); + for (int i = 0; i < empty; i++) bar.append("░"); + return bar.toString(); + } + + public abstract String getCourseType(); + public abstract String getScheduleInfo(); + + @Override + public String toString() { + return String.format("[%s] %s (%s) | Enrolled: %d/%d", + courseId, + courseName, + getCourseType(), + enrolledCount, + maxStudents + ); + } +} + diff --git a/task-1/com/hyfacademy/model/LiveCohortCourse.java b/task-1/com/hyfacademy/model/LiveCohortCourse.java new file mode 100644 index 0000000..5f31856 --- /dev/null +++ b/task-1/com/hyfacademy/model/LiveCohortCourse.java @@ -0,0 +1,72 @@ +package com.hyfacademy.model; + +import com.hyfacademy.exception.EnrolmentException; +import com.hyfacademy.service.Reportable; + +public class LiveCohortCourse extends Course implements Reportable { + + private String startDate; + private String endDate; + private Mentor mentor; + + public LiveCohortCourse(String courseName, int maxStudents, String startDate, String endDate) { + super(courseName, maxStudents); + this.startDate = startDate; + this.endDate = endDate; + } + + @Override + public String getCourseType() { + return "Live Cohort"; + } + + @Override + public String getScheduleInfo() { + String mentorName = (mentor != null ? mentor.getName() : "No mentor assigned"); + return startDate + " to " + endDate + " | Mentor: " + mentorName; + } + + public void assignMentor(Mentor mentor) throws EnrolmentException { + this.mentor = mentor; + mentor.assignToCourse(this); + } + + @Override + public String generateReport() { + + System.out.println("══════════════════════════════════════════"); + System.out.println(" COURSE REPORT — Live Cohort"); + System.out.println("══════════════════════════════════════════"); + System.out.println(" ID : " + getCourseId()); + System.out.println(" Name : " + getCourseName()); + System.out.println(" Schedule : " + startDate + " to " + endDate); + System.out.println(" Mentor : " + (mentor != null ? mentor.getName() : "None")); + System.out.println(" Capacity : " + capacityStatus()); + System.out.println("──────────────────────────────────────────"); + System.out.println(" STUDENT PROGRESS"); + System.out.println("──────────────────────────────────────────"); + + int total = 0; + + for (int i = 0; i < getEnrolledCount(); i++) { + Student s = students[i]; + int progress = studentProgress[i]; + total += progress; + + System.out.printf(" %-8s %-20s %4d%% %s%n", + s.getUserId(), + s.getName(), + progress, + progressBar(progress) + ); + } + + System.out.println("──────────────────────────────────────────"); + + int avg = (getEnrolledCount() == 0) ? 0 : total / getEnrolledCount(); + System.out.println(" Avg Progress : " + avg + "%"); + System.out.println("══════════════════════════════════════════"); + + return ""; + } +} diff --git a/task-1/com/hyfacademy/model/Mentor.java b/task-1/com/hyfacademy/model/Mentor.java new file mode 100644 index 0000000..4970859 --- /dev/null +++ b/task-1/com/hyfacademy/model/Mentor.java @@ -0,0 +1,46 @@ +package com.hyfacademy.model; + +import com.hyfacademy.exception.EnrolmentException; + +public class Mentor extends User { + + private static int counter = 1; + + private String expertise; + private Course[] assignedCourses = new Course[3]; + private int courseCount = 0; + + public Mentor(String name, String email, String expertise) { + super(name, email, String.format("COA-%03d", counter++)); + this.expertise = expertise; + } + + @Override + public String getRole() { + return "MENTOR"; + } + + public void assignToCourse(Course course) throws EnrolmentException { + if (courseCount >= 3) { + throw new EnrolmentException( + getName() + " cannot be assigned to more than 3 courses" + ); + } + + assignedCourses[courseCount++] = course; + } + + public Course[] getAssignedCourses() { + return assignedCourses; + } + + public int getCourseCount() { + return courseCount; + } + + public String getExpertise() { + return expertise; + } +} + + diff --git a/task-1/com/hyfacademy/model/SelfPacedCourse.java b/task-1/com/hyfacademy/model/SelfPacedCourse.java new file mode 100644 index 0000000..5179717 --- /dev/null +++ b/task-1/com/hyfacademy/model/SelfPacedCourse.java @@ -0,0 +1,64 @@ +package com.hyfacademy.model; + +import com.hyfacademy.service.Reportable; + +public class SelfPacedCourse extends Course implements Reportable { + + private int estimatedHours; + + public SelfPacedCourse(String courseName, int maxStudents, int estimatedHours) { + super(courseName, maxStudents); + this.estimatedHours = estimatedHours; + } + + @Override + public String getCourseType() { + return "Self-Paced"; + } + + @Override + public String getScheduleInfo() { + return "Estimated: " + estimatedHours + " hours — complete at your own pace"; + } + + @Override + public String generateReport() { + System.out.println("══════════════════════════════════════════"); + System.out.println(" COURSE REPORT — Self-Paced"); + System.out.println("══════════════════════════════════════════"); + System.out.println(" ID : " + getCourseId()); + System.out.println(" Name : " + getCourseName()); + System.out.println(" Capacity : " + capacityStatus()); + System.out.println(" Est. Hours : " + estimatedHours); + System.out.println("──────────────────────────────────────────"); + System.out.println(" STUDENT PROGRESS"); + System.out.println("──────────────────────────────────────────"); + + for (int i = 0; i < getEnrolledCount(); i++) { + Student s = students[i]; + int progress = studentProgress[i]; + + System.out.printf(" %-8s %-20s %4d%% %s%n", + s.getUserId(), + s.getName(), + progress, + progressBar(progress) + ); + } + + System.out.println("──────────────────────────────────────────"); + + int total = 0; + for (int i = 0; i < getEnrolledCount(); i++) { + total += studentProgress[i]; + } + + int avg = (getEnrolledCount() == 0) ? 0 : total / getEnrolledCount(); + System.out.println(" Avg Progress : " + avg + "%"); + + System.out.println("══════════════════════════════════════════"); + + return " "; + } +} + diff --git a/task-1/com/hyfacademy/model/Student.java b/task-1/com/hyfacademy/model/Student.java new file mode 100644 index 0000000..bd7f9bd --- /dev/null +++ b/task-1/com/hyfacademy/model/Student.java @@ -0,0 +1,64 @@ +package com.hyfacademy.model; + +import com.hyfacademy.exception.AlreadyEnrolledException; +import com.hyfacademy.exception.EnrolmentException; + +public class Student extends User { + + private static int counter = 1; + + private Course[] enrolledCourses = new Course[5]; + private int courseCount = 0; + + public Student(String name, String email) { + super(name, email, String.format("STU-%03d", counter++)); + } + + @Override + public String getRole() { + return "STUDENT"; + } + + public void enrol(Course course) throws EnrolmentException { + + // Student limit + if (courseCount >= 5) { + throw new EnrolmentException( + getName() + " cannot enrol in more than 5 courses" + ); + } + + // Already enrolled? + for (int i = 0; i < courseCount; i++) { + if (enrolledCourses[i].getCourseId().equals(course.getCourseId())) { + throw new AlreadyEnrolledException( + getName(), + course.getCourseName() + ); + } + } + + enrolledCourses[courseCount++] = course; + } + + public Course[] getCourses() { + return enrolledCourses; + } + + public int getCourseCount() { + return courseCount; + } + + public int getProgress(String courseName) throws EnrolmentException { + for (int i = 0; i < courseCount; i++) { + if (enrolledCourses[i].getCourseName().equalsIgnoreCase(courseName)) { + return enrolledCourses[i].getStudentProgress(this); + } + } + + throw new EnrolmentException( + getName() + " is not enrolled in course '" + courseName + "'" + ); + } +} + diff --git a/task-1/com/hyfacademy/model/User.java b/task-1/com/hyfacademy/model/User.java new file mode 100644 index 0000000..c667081 --- /dev/null +++ b/task-1/com/hyfacademy/model/User.java @@ -0,0 +1,36 @@ +package com.hyfacademy.model; + +public abstract class User { + private static int userCounter = 1; + + protected String name; + protected String email; + protected String userId; + + public User(String name, String email, String userId) { + this.name = name; + this.email = email; + this.userId = userId; + } + + public String getName() { + return name; + } + public String getEmail() { + return email; + } + public String getUserId() { + return userId; + } + + public abstract String getRole(); + + public String getSummary() { + return "[" + getRole() + "] " + name + " | " + email; + } + + @Override + public String toString() { + return getSummary(); + } +} diff --git a/task-1/com/hyfacademy/service/Enrollable.java b/task-1/com/hyfacademy/service/Enrollable.java new file mode 100644 index 0000000..20f27ab --- /dev/null +++ b/task-1/com/hyfacademy/service/Enrollable.java @@ -0,0 +1,16 @@ +package com.hyfacademy.service; + +import com.hyfacademy.model.Student; + +public interface Enrollable { + + void enrol(Student student); + boolean isFull(); + int getEnrolledCount(); + int getMaxStudents(); + + default String capacityStatus() { + return getEnrolledCount() + "/" + getMaxStudents() + + (isFull() ? " — FULL" : " — Open"); + } +} diff --git a/task-1/com/hyfacademy/service/PlatformService.java b/task-1/com/hyfacademy/service/PlatformService.java new file mode 100644 index 0000000..abb5f2d --- /dev/null +++ b/task-1/com/hyfacademy/service/PlatformService.java @@ -0,0 +1,309 @@ +package com.hyfacademy.service; + +import com.hyfacademy.exception.*; +import com.hyfacademy.model.*; + +import java.util.InputMismatchException; +import java.util.Scanner; + +public class PlatformService { + + private Course[] courses = new Course[10]; + private int courseCount = 0; + + private Student[] students = new Student[20]; + private int studentCount = 0; + + private Mentor[] mentors = new Mentor[5]; + private int mentorCount = 0; + + private Scanner scanner = new Scanner(System.in); + + + public void run() { + boolean running = true; + + while (running) { + printMenu(); + + System.out.print("Choose an option: "); + int choice = -1; + + try { + choice = scanner.nextInt(); + } catch (InputMismatchException e) { + System.out.println("Error: Please enter a valid number."); + } finally { + scanner.nextLine(); // clear buffer + } + + switch (choice) { + case 1 -> addCourse(); + case 2 -> addStudent(); + case 3 -> addMentor(); + case 4 -> enrolStudent(); + case 5 -> updateProgress(); + case 6 -> viewAllCourses(); + case 7 -> viewCourseReport(); + case 8 -> viewAllStudents(); + case 9 -> { + System.out.println("Exiting... Goodbye!"); + running = false; + } + default -> System.out.println("Error: Invalid option. Try again."); + } + + System.out.println(); + } + } + + private void printMenu() { + System.out.println("╔══════════════════════════════════════════╗"); + System.out.println("║ HYF ACADEMY COURSE PLATFORM ║"); + System.out.println("╚══════════════════════════════════════════╝"); + System.out.println(" 1. Add course"); + System.out.println(" 2. Add student"); + System.out.println(" 3. Add mentor"); + System.out.println(" 4. Enrol student in course"); + System.out.println(" 5. Update student progress"); + System.out.println(" 6. View all courses"); + System.out.println(" 7. View course report"); + System.out.println(" 8. View all students"); + System.out.println(" 9. Exit"); + System.out.println("══════════════════════════════════════════"); + } + + private void addCourse() { + if (courseCount >= courses.length) { + System.out.println("Error: Cannot add more courses (limit reached)."); + return; + } + + System.out.print("Course type (1 = Self-Paced, 2 = Live Cohort): "); + int type; + + try { + type = scanner.nextInt(); + } catch (InputMismatchException e) { + System.out.println("Error: Invalid type."); + scanner.nextLine(); + return; + } + scanner.nextLine(); + + System.out.print("Course name: "); + String name = scanner.nextLine(); + + System.out.print("Max students: "); + int max; + try { + max = scanner.nextInt(); + } catch (InputMismatchException e) { + System.out.println("Error: Max students must be a number."); + scanner.nextLine(); + return; + } + scanner.nextLine(); + + if (type == 1) { + System.out.print("Estimated hours: "); + int hours; + try { + hours = scanner.nextInt(); + } catch (InputMismatchException e) { + System.out.println("Error: Estimated hours must be a number."); + scanner.nextLine(); + return; + } + scanner.nextLine(); + + courses[courseCount++] = new SelfPacedCourse(name, max, hours); + + } else if (type == 2) { + System.out.print("Start date (YYYY-MM-DD): "); + String start = scanner.nextLine(); + System.out.print("End date (YYYY-MM-DD): "); + String end = scanner.nextLine(); + + courses[courseCount++] = new LiveCohortCourse(name, max, start, end); + + } else { + System.out.println("Error: Unknown course type."); + return; + } + + System.out.println("Course added."); + } + + + private void addStudent() { + if (studentCount >= students.length) { + System.out.println("Error: Cannot add more students (limit reached)."); + return; + } + + System.out.print("Student name: "); + String name = scanner.nextLine(); + System.out.print("Student email: "); + String email = scanner.nextLine(); + + Student s = new Student(name, email); + students[studentCount++] = s; + + System.out.println("Student created with ID: " + s.getUserId()); + } + + + private void addMentor() { + if (mentorCount >= mentors.length) { + System.out.println("Error: Cannot add more mentors (limit reached)."); + return; + } + + System.out.print("Mentor name: "); + String name = scanner.nextLine(); + System.out.print("Mentor email: "); + String email = scanner.nextLine(); + System.out.print("Expertise: "); + String expertise = scanner.nextLine(); + + Mentor m = new Mentor(name, email, expertise); + mentors[mentorCount++] = m; + + System.out.println("Mentor created with ID: " + m.getUserId()); + } + + + private Student findStudentById(String id) { + for (int i = 0; i < studentCount; i++) { + if (students[i].getUserId().equalsIgnoreCase(id)) { + return students[i]; + } + } + return null; + } + + private Course findCourseById(String id) { + for (int i = 0; i < courseCount; i++) { + if (courses[i].getCourseId().equalsIgnoreCase(id)) { + return courses[i]; + } + } + return null; + } + + private void enrolStudent() { + System.out.print("Student ID: "); + String studentId = scanner.nextLine(); + System.out.print("Course ID: "); + String courseId = scanner.nextLine(); + + Student s = findStudentById(studentId); + Course c = findCourseById(courseId); + + if (s == null) { + System.out.println("Error: Student not found."); + return; + } + if (c == null) { + System.out.println("Error: Course not found."); + return; + } + + try { + c.enrol(s); + System.out.println("Student enrolled successfully."); + } catch (CourseFullException | AlreadyEnrolledException e) { + System.out.println("Error " + e.getMessage()); + } catch (EnrolmentException e) { + System.out.println("Error " + e.getMessage()); + } + } + + private void updateProgress() { + System.out.print("Student ID: "); + String studentId = scanner.nextLine(); + System.out.print("Course ID: "); + String courseId = scanner.nextLine(); + + Student s = findStudentById(studentId); + Course c = findCourseById(courseId); + + if (s == null) { + System.out.println("Error: Student not found."); + return; + } + if (c == null) { + System.out.println("Error: Course not found."); + return; + } + + System.out.print("Progress (0–100): "); + int progress; + + try { + progress = scanner.nextInt(); + } catch (InputMismatchException e) { + System.out.println("Progress must be a number."); + scanner.nextLine(); + return; + } finally { + scanner.nextLine(); + } + + try { + c.updateProgress(s, progress); + System.out.println("Progress updated."); + } catch (InvalidProgressException e) { + System.out.println("Error: " + e.getMessage()); + } catch (EnrolmentException e) { + System.out.println("Error: " + e.getMessage()); + } + } + + private void viewAllCourses() { + System.out.println("══════════════════════════════════════════════════════════════════"); + System.out.println(" ID NAME TYPE CAPACITY"); + System.out.println("══════════════════════════════════════════════════════════════════"); + + for (int i = 0; i < courseCount; i++) { + System.out.println(courses[i]); // polymorphic toString() + } + + System.out.println("══════════════════════════════════════════════════════════════════"); + } + + private void viewCourseReport() { + System.out.print("Course ID: "); + String courseId = scanner.nextLine(); + + Course c = findCourseById(courseId); + if (c == null) { + System.out.println(" Course not found."); + return; + } + + if (c instanceof Reportable r) { + r.generateReport(); + } else { + System.out.println("This course does not support reporting."); + } + } + + private void viewAllStudents() { + System.out.println("══════════════════════════════════════════"); + System.out.println(" STUDENTS"); + System.out.println("══════════════════════════════════════════"); + + for (int i = 0; i < studentCount; i++) { + Student s = students[i]; + System.out.printf(" %-8s %-20s Courses: %d%n", + s.getUserId(), + s.getName(), + s.getCourseCount()); + } + + System.out.println("══════════════════════════════════════════"); + } +} + diff --git a/task-1/com/hyfacademy/service/Reportable.java b/task-1/com/hyfacademy/service/Reportable.java new file mode 100644 index 0000000..36f5b22 --- /dev/null +++ b/task-1/com/hyfacademy/service/Reportable.java @@ -0,0 +1,7 @@ +package com.hyfacademy.service; + +public interface Reportable { + + String generateReport(); + +}