diff --git a/task-1/com.hyfacademy/Main.java b/task-1/com.hyfacademy/Main.java index d97363c..aeefc27 100644 --- a/task-1/com.hyfacademy/Main.java +++ b/task-1/com.hyfacademy/Main.java @@ -1,7 +1,10 @@ package com.hyfacademy; +import com.hyfacademy.service.*; + public class Main { - static void main(String[] args) { - System.out.println("Hello World!"); + public static void main(String[] args) { + PlatformService service = new PlatformService(); + service.run(); } } diff --git a/task-1/com.hyfacademy/exception/AlreadyEnrolledException.java b/task-1/com.hyfacademy/exception/AlreadyEnrolledException.java index a82837e..0e3bfe3 100644 --- a/task-1/com.hyfacademy/exception/AlreadyEnrolledException.java +++ b/task-1/com.hyfacademy/exception/AlreadyEnrolledException.java @@ -1,4 +1,14 @@ package com.hyfacademy.exception; -public class AlreadyEnrolledException { +public class AlreadyEnrolledException extends EnrolmentException { + private final String studentName; + private final String courseName; + + public AlreadyEnrolledException(String studentName, String courseName){ + super(String.format("%s is already enrolled in '%s'", studentName, 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 index 11a76f1..0296137 100644 --- a/task-1/com.hyfacademy/exception/CourseFullException.java +++ b/task-1/com.hyfacademy/exception/CourseFullException.java @@ -1,4 +1,17 @@ package com.hyfacademy.exception; -public class CourseFullException { +public class CourseFullException extends EnrolmentException { + private final String courseName; + private final int maxCapacity; + + public CourseFullException(String courseName, int maxCapacity) { + super(String.format("Course '%s' is full (max: %d students)", courseName, maxCapacity)); + 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 index 8454c32..4b922dd 100644 --- a/task-1/com.hyfacademy/exception/EnrolmentException.java +++ b/task-1/com.hyfacademy/exception/EnrolmentException.java @@ -1,4 +1,7 @@ package com.hyfacademy.exception; -public class EnrolmentException { +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 index c0c0037..4384255 100644 --- a/task-1/com.hyfacademy/exception/InvalidProgressException.java +++ b/task-1/com.hyfacademy/exception/InvalidProgressException.java @@ -1,4 +1,13 @@ package com.hyfacademy.exception; -public class InvalidProgressException { +public class InvalidProgressException extends EnrolmentException { + private final int attemptedValue; + + public InvalidProgressException(int attemptedValue){ + super(String.format("Progress must be between 0 and 100, but received: %d", attemptedValue)); + this.attemptedValue = attemptedValue; + } + + // Getter + public int getAttemptedValue() {return attemptedValue;} } diff --git a/task-1/com.hyfacademy/model/Course.java b/task-1/com.hyfacademy/model/Course.java index e80513e..698989f 100644 --- a/task-1/com.hyfacademy/model/Course.java +++ b/task-1/com.hyfacademy/model/Course.java @@ -1,4 +1,101 @@ package com.hyfacademy.model; +import com.hyfacademy.exception.*; +import com.hyfacademy.service.Enrollable; +import com.hyfacademy.service.Reportable; + +public abstract class Course implements Enrollable, Reportable { + private String courseName; + private String courseId; + private int maxStudents; + private int enrolledCount; + + // parallel array + private int[] studentProgress; + private Student[] enrolledStudents; + + public static int totalCourses = 0; + public Course (String courseName, int maxStudents){ + totalCourses++; + this.courseId = "CRS-" + String.format("%03d", totalCourses); + this.courseName = courseName; + this.maxStudents = maxStudents; + this.enrolledCount = 0; + this.studentProgress = new int[maxStudents]; + this.enrolledStudents = new Student[maxStudents]; + } + + // getters + public String getCourseName() {return courseName;} + public String getCourseId() {return courseId;} + // getters for enrolled students and progress + public Student[] getEnrolledStudents() {return enrolledStudents;} + public int[] getStudentProgress() {return studentProgress;} + + @Override + public int getMaxStudents() {return maxStudents;} + @Override + public int getEnrolledCount() {return enrolledCount;} + + + + // help to search student index array + private int findStudentIndex(Student student) { + for (int i = 0; i < enrolledCount; i++) { + if (enrolledStudents[i] != null && enrolledStudents[i].equals(student)) { + return i; + } + } + return -1; + } + + // check if the course is full + @Override + public boolean isFull() { + return enrolledCount >= maxStudents; + } + + // Methods + @Override + public void enrol(Student student) throws CourseFullException, AlreadyEnrolledException { + if (isFull()) { + throw new CourseFullException(this.courseName, this.maxStudents); + } + for (int i = 0; i < enrolledCount; i++) { + if (enrolledStudents[i].equals(student)) { + throw new AlreadyEnrolledException(student.getName(), this.courseName); + } + } + enrolledStudents[enrolledCount] = student; + studentProgress[enrolledCount] = 0; + enrolledCount++; + } + + 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 " + 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 " + student.getName() + " is not enrolled in " + courseName); + } + return studentProgress[index]; + } + + //Abstract Methods + 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); + } -public class Course { } diff --git a/task-1/com.hyfacademy/model/LiveCohortCourse.java b/task-1/com.hyfacademy/model/LiveCohortCourse.java index a69fb63..80b5391 100644 --- a/task-1/com.hyfacademy/model/LiveCohortCourse.java +++ b/task-1/com.hyfacademy/model/LiveCohortCourse.java @@ -1,4 +1,88 @@ package com.hyfacademy.model; +import com.hyfacademy.exception.*; -public class LiveCohortCourse { -} + +public class LiveCohortCourse extends Course { + 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; + } + + // Getters + public String getStartDate() { return startDate; } + public String getEndDate() { return endDate; } + public Mentor getMentor() { return mentor; } + + // Methods + public void assignMentor(Mentor mentor) throws EnrolmentException { + mentor.assignToCourse(this); + this.mentor = mentor; + } + + @Override + public String getCourseType() { + return "Live Cohort"; + } + + @Override + public String getScheduleInfo() { + String mentorName = (mentor != null) ? mentor.getName() : "TBD"; + return String.format("%s to %s | Mentor: %s", startDate, endDate, mentorName); + } + + @Override + public void generateReport() { + Student[] students = getEnrolledStudents(); + int[] progress = getStudentProgress(); + int enrolledCount = getEnrolledCount(); + + // Calculate average progress + int totalProgress = 0; + for (int i = 0; i < enrolledCount; i++) { + totalProgress += progress[i]; + } + int avgProgress = enrolledCount > 0 ? totalProgress / enrolledCount : 0; + + // Determine capacity status + String capacityStatus = enrolledCount >= getMaxStudents() ? "Full" : "Open"; + + System.out.println("══════════════════════════════════════════"); + System.out.println(" COURSE REPORT — Live Cohort"); + System.out.println("══════════════════════════════════════════"); + System.out.printf(" %-12s: %s\n", "ID", getCourseId()); + System.out.printf(" %-12s: %s\n", "Name", getCourseName()); + System.out.printf(" %-12s: %s to %s\n", "Schedule", startDate, endDate); + System.out.printf(" %-12s: %s\n", "Mentor", (mentor != null) ? mentor.getName() : "Not assigned"); + System.out.printf(" %-12s: %d/%d — %s\n", "Capacity", enrolledCount, getMaxStudents(), capacityStatus); + System.out.println("──────────────────────────────────────────"); + System.out.println(" STUDENT PROGRESS"); + System.out.println("──────────────────────────────────────────"); + + for (int i = 0; i < enrolledCount; i++) { + Student student = students[i]; + int studentProgress = progress[i]; + String progressBar = createProgressBar(studentProgress); + System.out.printf(" %-6s%-22s%3d%% %s\n", + student.getUserId(), student.getName(), studentProgress, progressBar); + } + + System.out.println("──────────────────────────────────────────"); + System.out.printf(" Avg Progress : %d%%\n", avgProgress); + System.out.println("══════════════════════════════════════════"); + } + + private String createProgressBar(int progress) { + int filled = progress / 10; + int empty = 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(); + } + +} \ No newline at end of file diff --git a/task-1/com.hyfacademy/model/Mentor.java b/task-1/com.hyfacademy/model/Mentor.java index b8738d5..dd396f4 100644 --- a/task-1/com.hyfacademy/model/Mentor.java +++ b/task-1/com.hyfacademy/model/Mentor.java @@ -1,4 +1,44 @@ package com.hyfacademy.model; +import com.hyfacademy.exception.EnrolmentException; + +public class Mentor extends User{ + private String expertise; + private Course[] assignedCourses; + private int courseCount; + private static int totalMentors = 0; + + public Mentor(String name, String email, String expertise){ + totalMentors++; + super(name, email, String.format("COA-%03d", totalMentors)); + this.expertise = expertise; + this.assignedCourses = new Course[3]; + this.courseCount = 0; + } + @Override + public String getRole(){ + return "MENTOR"; + } + + // Getters + public Course[] getAssignedCourses() {return assignedCourses;} + public String getExpertise() {return expertise;} + public int getCourseCount() {return courseCount;} + + // Methods + public void assignToCourse(Course course) throws EnrolmentException { + // mentor's capacity check + if (courseCount >= 3) { + throw new EnrolmentException("Mentor " + getName() + " is already at maximum course capacity (3)."); + } + // check if the mentor already assigned + for (int i = 0; i < courseCount; i++) { + if (assignedCourses[i].getCourseId().equals(course.getCourseId())) { + throw new EnrolmentException("Mentor is already assigned to " + course.getCourseName()); + } + } + assignedCourses[courseCount] = course; + courseCount++; + } + -public class Mentor { } diff --git a/task-1/com.hyfacademy/model/SelfPacedCourse.java b/task-1/com.hyfacademy/model/SelfPacedCourse.java index 055d64a..67bf556 100644 --- a/task-1/com.hyfacademy/model/SelfPacedCourse.java +++ b/task-1/com.hyfacademy/model/SelfPacedCourse.java @@ -1,4 +1,69 @@ package com.hyfacademy.model; -public class SelfPacedCourse { +public class SelfPacedCourse extends Course { + 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 String.format("Estimated: %d hours — complete at your own pace", estimatedHours); + } + + @Override + public void generateReport() { + Student[] students = getEnrolledStudents(); + int[] progress = getStudentProgress(); + int enrolledCount = getEnrolledCount(); + + // Calculate average progress + int totalProgress = 0; + for (int i = 0; i < enrolledCount; i++) { + totalProgress += progress[i]; + } + int avgProgress = enrolledCount > 0 ? totalProgress / enrolledCount : 0; + + // Determine capacity status + String capacityStatus = enrolledCount >= getMaxStudents() ? "Full" : "Open"; + + System.out.println("══════════════════════════════════════════"); + System.out.println(" COURSE REPORT — Self-Paced"); + System.out.println("══════════════════════════════════════════"); + System.out.printf(" %-12s: %s\n", "ID", getCourseId()); + System.out.printf(" %-12s: %s\n", "Name", getCourseName()); + System.out.printf(" %-12s: %d/%d — %s\n", "Capacity", enrolledCount, getMaxStudents(), capacityStatus); + System.out.printf(" %-12s: %d\n", "Est. Hours", estimatedHours); + System.out.println("──────────────────────────────────────────"); + System.out.println(" STUDENT PROGRESS"); + System.out.println("──────────────────────────────────────────"); + + for (int i = 0; i < enrolledCount; i++) { + Student student = students[i]; + int studentProgress = progress[i]; + String progressBar = createProgressBar(studentProgress); + System.out.printf(" %-6s%-22s%3d%% %s\n", + student.getUserId(), student.getName(), studentProgress, progressBar); + } + + System.out.println("──────────────────────────────────────────"); + System.out.printf(" Avg Progress : %d%%\n", avgProgress); + System.out.println("══════════════════════════════════════════"); + } + + private String createProgressBar(int progress) { + int filled = progress / 10; + int empty = 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(); + } } diff --git a/task-1/com.hyfacademy/model/Student.java b/task-1/com.hyfacademy/model/Student.java index 9f34d88..e51ccdb 100644 --- a/task-1/com.hyfacademy/model/Student.java +++ b/task-1/com.hyfacademy/model/Student.java @@ -1,4 +1,79 @@ package com.hyfacademy.model; +import com.hyfacademy.util.GradeUtils; +import com.hyfacademy.exception.*; + +public class Student extends User { + private static int totalStudents = 0; + private int[] grades; + private int courseCount; + private Course[] enrolledCourses; + + public Student (String name, String email){ + totalStudents++; + super(name, email, String.format("STU-%03d", totalStudents)); + this.grades = new int[5]; + this.enrolledCourses = new Course[5]; + this.courseCount = 0; + } + + @Override + public String getRole(){ + return "STUDENT"; + } + + // Getters + public int[] getGrades() {return grades;} + public static int getTotalStudents() {return totalStudents;} + 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("Not enrolled in course: " + courseName); + } + + //Setter + public void setGrade(int moduleIndex, int grade) { + if (moduleIndex < 0 || moduleIndex > 4) { + System.out.println("Error: moduleIndex must be between 0 and 4."); + return; + } + if (grade < 0 || grade > 100) { + System.out.println("Error: grade must be between 0 and 100."); + return; + } + grades[moduleIndex] = grade; + } + + //methods + public void enrol(Course course) throws AlreadyEnrolledException, EnrolmentException, CourseFullException { + // Checking if the student enrolment limit is reached + if (courseCount >= 5) { + throw new EnrolmentException("Student enrollment limit reached (Max: 5 courses)."); + } + // Checking if student is already enrolled in this course + for (int i = 0; i < courseCount; i++) { + if (enrolledCourses[i].getCourseId().equals(course.getCourseId())) { + throw new AlreadyEnrolledException(this.getName(), course.getCourseName()); + } + } + course.enrol(this); + enrolledCourses[courseCount] = course; + courseCount++; + } + + + @Override + public String toString() { + double average = GradeUtils.calculateAverage(grades); + return String.format("[%s] %s — ", + getUserId(), + getName()); +// average, +// GradeUtils.isPassing(average) ? "PASS" : "FAIL"); + } -public class Student { } diff --git a/task-1/com.hyfacademy/model/User.java b/task-1/com.hyfacademy/model/User.java index 43cba56..8aedf41 100644 --- a/task-1/com.hyfacademy/model/User.java +++ b/task-1/com.hyfacademy/model/User.java @@ -1,4 +1,30 @@ package com.hyfacademy.model; -public class User { +public abstract class User { + String name; + String email; + String userId; + + public User(String name, String email, String userId){ + this.name = name; + this.email = email; + this.userId = userId; + } + // Getters + public String getName() {return name;} + public String getEmail() {return email;} + public String getUserId() {return userId;} + + // Abstract Method + public abstract String getRole(); + + // Concrete method + public String getSummary() { + return "[" + getRole() + "] " + name + " | " + email; // ex: "[STUDENT] Alice | alice@mail.com" + } + + @Override + public String toString() { + return getSummary(); + } } diff --git a/task-1/com.hyfacademy/service/Enrollable.java b/task-1/com.hyfacademy/service/Enrollable.java index a8151b7..3f838ad 100644 --- a/task-1/com.hyfacademy/service/Enrollable.java +++ b/task-1/com.hyfacademy/service/Enrollable.java @@ -1,4 +1,14 @@ package com.hyfacademy.service; +import com.hyfacademy.model.Student; -public class Enrollable { +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/GradeService.java b/task-1/com.hyfacademy/service/GradeService.java new file mode 100644 index 0000000..170da58 --- /dev/null +++ b/task-1/com.hyfacademy/service/GradeService.java @@ -0,0 +1,166 @@ +package com.hyfacademy.service; + +import com.hyfacademy.model.Student; +import com.hyfacademy.util.GradeUtils; + +import java.util.Scanner; + +public class GradeService { + private static final int MAX_STUDENTS = 20; + + private final Student[] students; + private int studentCount; + private final Scanner scanner; + + public GradeService() { + this.students = new Student[MAX_STUDENTS]; + this.scanner = new Scanner(System.in); + } + + public void addStudent() { + if (studentCount >= MAX_STUDENTS) { + System.out.println("Student limit reached."); + return; + } + + System.out.print("Enter student name: "); + String name = scanner.nextLine().trim(); + System.out.print("Enter student email: "); + String email = scanner.nextLine().trim(); + students[studentCount] = new Student(name, email); + studentCount++; + System.out.println("Added."); + } + + public void enterGrades() { + System.out.print("Enter student ID: "); + Student student = findStudentById(scanner.nextLine().trim()); + if (student == null) { + System.out.println("Student not found."); + return; + } + + for (int i = 0; i < GradeUtils.MODULE_COUNT; i++) { + System.out.print("Enter grade for " + GradeUtils.MODULE_NAMES[i] + ": "); + student.setGrade(i, readInt()); + } + } + + public void viewAllStudents() { + if (studentCount == 0) { + System.out.println("No students yet."); + return; + } + + String border = "══════════════════════════════════════════════════════════════"; + + System.out.println(); + System.out.println(border); + System.out.printf(" %-10s %-20s %-9s %-7s %-6s%n", "ID", "NAME", "AVERAGE", "GRADE", "STATUS"); + System.out.println(border); + + int passing = 0; + for (int i = 0; i < studentCount; i++) { + Student student = students[i]; + double average = GradeUtils.calculateAverage(student.getGrades()); + if (GradeUtils.isPassing(average)) { + passing++; + } + System.out.printf(" %-10s %-20s %-9.2f %-7s %-6s%n", + student.getUserId(), + student.getName(), + average, + GradeUtils.getLetterGrade(average), + GradeUtils.isPassing(average) ? "PASS" : "FAIL"); + } + + int failing = studentCount - passing; + System.out.println(border); + System.out.println(" Total students: " + studentCount + " Passing: " + passing + " Failing: " + failing); + System.out.println(border); + } + + public void viewStudentReport() { + System.out.print("Enter student ID: "); + Student student = findStudentById(scanner.nextLine().trim()); + if (student == null) { + System.out.println("Student not found."); + return; + } + + double average = GradeUtils.calculateAverage(student.getGrades()); + String separator = "══════════════════════════════════════"; + String dashedLine = "──────────────────────────────────────"; + + System.out.println(); + System.out.println(separator); + System.out.println(" STUDENT REPORT"); + System.out.println(separator); + System.out.println(" ID : " + student.getUserId()); + System.out.println(" Name : " + student.getName()); + System.out.println(dashedLine); + System.out.println(" MODULE GRADES"); + System.out.println(dashedLine); + + int[] grades = student.getGrades(); + for (int i = 0; i < GradeUtils.MODULE_COUNT; i++) { + int grade = grades[i]; + String moduleName = GradeUtils.MODULE_NAMES[i]; + String status = GradeUtils.isModulePassing(grade) ? "PASS" : "FAIL"; + System.out.printf(" %-25s : %3d %s%n", moduleName, grade, status); + } + + System.out.println(dashedLine); + System.out.printf(" Average : %.2f%n", average); + System.out.println(" Grade : " + GradeUtils.getLetterGrade(average)); + String statusSymbol = GradeUtils.isPassing(average) ? "✓" : "✗"; + System.out.println(" Status : " + statusSymbol + " " + (GradeUtils.isPassing(average) ? "PASS" : "FAIL")); + System.out.println(separator); + } + + public void run() { + while (true) { + System.out.println(); + System.out.println("╔══════════════════════════════════════╗"); + System.out.println("║ HYF ACADEMY — GRADE MGR ║"); + System.out.println("╚══════════════════════════════════════╝"); + System.out.println(" 1. Add student"); + System.out.println(" 2. Enter grades"); + System.out.println(" 3. View all students"); + System.out.println(" 4. View student report"); + System.out.println(" 5. Exit"); + System.out.println("══════════════════════════════════════"); + System.out.print("Choose an option: "); + + + switch (readInt()) { + case 1 -> addStudent(); + case 2 -> enterGrades(); + case 3 -> viewAllStudents(); + case 4 -> viewStudentReport(); + case 5 -> { return; } + default -> System.out.println("Invalid option."); + } + } + } + + private Student findStudentById(String id) { + for (int i = 0; i < studentCount; i++) { + if (students[i].getUserId().equalsIgnoreCase(id)) { + return students[i]; + } + } + return null; + } + + private int readInt() { + while (!scanner.hasNextInt()) { + scanner.nextLine(); + System.out.print("Enter a valid number: "); + } + + int value = scanner.nextInt(); + scanner.nextLine(); + return value; + } +} diff --git a/task-1/com.hyfacademy/service/PlatformService.java b/task-1/com.hyfacademy/service/PlatformService.java index 4248597..f7463c2 100644 --- a/task-1/com.hyfacademy/service/PlatformService.java +++ b/task-1/com.hyfacademy/service/PlatformService.java @@ -1,4 +1,222 @@ package com.hyfacademy.service; +import com.hyfacademy.model.*; +import com.hyfacademy.exception.*; +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() { + while (true) { + printMainMenu(); + String choice = scanner.nextLine(); + try { + switch (choice) { + case "1": addCourse(); break; + case "2": addStudent(); break; + case "3": addMentor(); break; + case "4": enrolStudent(); break; + case "5": updateProgress(); break; + case "6": viewAllCourses(); break; + case "7": viewCourseReport(); break; + case "8": viewAllStudents(); break; + case "9": System.out.println("Exiting..."); return; + default: System.out.println("Invalid option. Try again."); + } + } catch (Exception e) { + System.out.println("An unexpected error occurred: " + e.getMessage()); + } + } + } + private void addCourse() { + if (courseCount >= 10) { + System.out.println("Error: Maximum course limit reached."); + return; + } + System.out.print("Enter type (1 for Self-Paced, 2 for Live Cohort): "); + String type = scanner.nextLine(); + System.out.print("Course Name: "); + String name = scanner.nextLine(); + System.out.print("Max Students: "); + int max = Integer.parseInt(scanner.nextLine()); + + if (type.equals("1")) { + System.out.print("Estimated Hours: "); + int hours = Integer.parseInt(scanner.nextLine()); + courses[courseCount++] = new SelfPacedCourse(name, max, hours); + } else { + System.out.print("Start Date (YYYY-MM-DD): "); + String start = scanner.nextLine(); + System.out.print("End Date (YYYY-MM-DD): "); + String end = scanner.nextLine(); + LiveCohortCourse course = new LiveCohortCourse(name, max, start, end); + courses[courseCount++] = course; + Mentor mentor = selectMentor(); + if (mentor == null) { + System.out.println("Error: No valid mentor selected."); + return; + } + try { + course.assignMentor(mentor); + } catch (EnrolmentException e) { + System.out.println("Error: " + e.getMessage()); + return; + } + } + System.out.println("Course added successfully."); + } + + private void addStudent() { + if (studentCount >= 20) { + System.out.println("Error: Student limit reached."); + return; + } + System.out.print("Name: "); + String name = scanner.nextLine(); + System.out.print("Email: "); + String email = scanner.nextLine(); + students[studentCount++] = new Student(name, email); + System.out.println("Student registered."); + } + + private void addMentor() { + if (mentorCount >= 5) { + System.out.println("Error: Mentor limit reached."); + return; + } + System.out.print("Name: "); + String name = scanner.nextLine(); + System.out.print("Email: "); + String email = scanner.nextLine(); + System.out.print("Expertise: "); + String exp = scanner.nextLine(); + mentors[mentorCount++] = new Mentor(name, email, exp); + System.out.println("Mentor registered."); + } + + private void enrolStudent() { + System.out.print("Enter Student ID: "); + String sId = scanner.nextLine(); + System.out.print("Enter Course ID: "); + String cId = scanner.nextLine(); + + Student student = findStudent(sId); + Course course = findCourse(cId); + + if (student == null || course == null) { + System.out.println("Error: Invalid Student or Course ID."); + return; + } + + try { + student.enrol(course); + System.out.println("Enrollment successful!"); + } catch (CourseFullException e) { + System.out.println("Enrollment Failed: " + e.getMessage()); + } catch (EnrolmentException e) { + System.out.println("Enrollment Failed: " + e.getMessage()); + } + } + + private void updateProgress() { + try { + System.out.print("Student ID: "); + String sId = scanner.nextLine(); + System.out.print("Course ID: "); + String cId = scanner.nextLine(); + System.out.print("Progress (0-100): "); + int progress = Integer.parseInt(scanner.nextLine()); + + Student student = findStudent(sId); + Course course = findCourse(cId); + + if (student != null && course != null) { + course.updateProgress(student, progress); + System.out.println("Progress updated."); + } else { + System.out.println("Invalid IDs provided."); + } + } catch (NumberFormatException e) { + System.out.println("Error: Please enter a valid number for progress."); + } catch (EnrolmentException e) { + System.out.println("Update Failed: " + e.getMessage()); + } finally { + System.out.println("Progress update operation completed."); + } + } + + private void viewAllCourses() { + System.out.println("\n══════════════════════════════════════════════════════════════════"); + System.out.printf(" %-10s%-26s%-15s%s\n", "ID", "NAME", "TYPE", "CAPACITY"); + System.out.println("══════════════════════════════════════════════════════════════════"); + for (int i = 0; i < courseCount; i++) { + Course c = courses[i]; + System.out.printf(" %-10s%-26s%-15s%s\n", + c.getCourseId(), c.getCourseName(), c.getCourseType(), c.capacityStatus()); + } + System.out.println("══════════════════════════════════════════════════════════════════\n"); + } + + // --- Helper Finders --- + private Student findStudent(String id) { + for (int i = 0; i < studentCount; i++) { + if (students[i].getUserId().equalsIgnoreCase(id)) return students[i]; + } + return null; + } + + private Course findCourse(String id) { + for (int i = 0; i < courseCount; i++) { + if (courses[i].getCourseId().equalsIgnoreCase(id)) return courses[i]; + } + return null; + } + + private Mentor selectMentor() { + System.out.print("Enter Mentor ID: "); + String mentorId = scanner.nextLine(); + for (int i = 0; i < mentorCount; i++) { + if (mentors[i].getUserId().equalsIgnoreCase(mentorId)) { + return mentors[i]; + } + } + return null; + } + + private void printMainMenu() { + System.out.println("\n╔══════════════════════════════════════════╗"); + System.out.println("║ HYF ACADEMY COURSE PLATFORM ║"); + System.out.println("╚══════════════════════════════════════════╝"); + System.out.println(" 1. Add course\n 2. Add student\n 3. Add mentor\n 4. Enrol student in course"); + System.out.println(" 5. Update student progress\n 6. View all courses\n 7. View course report"); + System.out.println(" 8. View all students\n 9. Exit"); + System.out.println("══════════════════════════════════════════"); + System.out.print("Choose an option: "); + } + + private void viewAllStudents() { + for (int i = 0; i < studentCount; i++) { + System.out.println(students[i].toString() + " | Courses: " + students[i].getCourseCount() + courses[i].getCourseName()); + } + } + + private void viewCourseReport() { + System.out.print("Enter Course ID: "); + String id = scanner.nextLine(); + Course course = findCourse(id); + + if (course instanceof Reportable) { + ((Reportable) course).generateReport(); + } else { + System.out.println("This course does not support reporting or wasn't found."); + } + } + } diff --git a/task-1/com.hyfacademy/service/Reportable.java b/task-1/com.hyfacademy/service/Reportable.java index 0c8ae05..1e4e5f1 100644 --- a/task-1/com.hyfacademy/service/Reportable.java +++ b/task-1/com.hyfacademy/service/Reportable.java @@ -1,4 +1,5 @@ package com.hyfacademy.service; -public class Reportable { +public interface Reportable { + void generateReport(); } diff --git a/task-1/com.hyfacademy/util/GradeUtils.java b/task-1/com.hyfacademy/util/GradeUtils.java new file mode 100644 index 0000000..cacbb69 --- /dev/null +++ b/task-1/com.hyfacademy/util/GradeUtils.java @@ -0,0 +1,57 @@ +package com.hyfacademy.util; + +public final class GradeUtils { + public static final int MODULE_PASS_MARK = 55; + public static final double TRACK_PASS_AVERAGE = 60.0; + public static final int MODULE_COUNT = 5; + public static final String[] MODULE_NAMES = { + "Java Basics", + "Control Flow", + "OOP Fundamentals", + "Arrays & Collections", + "Input & Output" + }; + + private GradeUtils() { + } + + public static double calculateAverage(int[] grades) { + if (grades == null || grades.length == 0) { + return 0.0; + } + int total = 0; + for (int grade : grades) { + total += grade; + } + return (double) total / grades.length; + } + + public static boolean isPassing(double average) { + return average >= TRACK_PASS_AVERAGE; + } + + public static boolean isModulePassing(int grade) { + return grade >= MODULE_PASS_MARK; + } + + public static String getLetterGrade(double average) { + if (average >= 90.0) { + return "A"; + } + if (average >= 80.0) { + return "B"; + } + if (average >= 70.0) { + return "C"; + } + if (average >= 60.0) { + return "D"; + } + return "F"; + } + +// %3d to print integer with 3 characters width, right-aligned + public static String formatGrade(int grade) { + return String.format("%3d", grade); + } +}