Skip to content
12 changes: 9 additions & 3 deletions task-1/com.hyfacademy/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.hyfacademy;

import com.hyfacademy.service.PlatformService;

public class Main {
static void main(String[] args) {
System.out.println("Hello World!");

public static void main(String[] args) {

PlatformService platformService = new PlatformService();

platformService.run();
}
}
}
19 changes: 18 additions & 1 deletion task-1/com.hyfacademy/exception/AlreadyEnrolledException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
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(studentName + " is already enrolled in '" + courseName + "'");
this.studentName = studentName;
this.courseName = courseName;

}

public String getStudentName() {
return studentName;
}

public String getCourseName() {
return courseName;
}
}
19 changes: 18 additions & 1 deletion task-1/com.hyfacademy/exception/CourseFullException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
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("Course '" + courseName + "' is full (max: " + maxCapacity + " students)");
this.courseName = courseName;
this.maxCapacity = maxCapacity;

}

public String getCourseName () {
return courseName;
}

public int getMaxCapacity () {
return maxCapacity;
}
}
8 changes: 6 additions & 2 deletions task-1/com.hyfacademy/exception/EnrolmentException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package com.hyfacademy.exception;

public class EnrolmentException {
}
public class EnrolmentException extends RuntimeException {
public EnrolmentException (String message) {
super(message);
}
}

13 changes: 12 additions & 1 deletion task-1/com.hyfacademy/exception/InvalidProgressException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
package com.hyfacademy.exception;

public class InvalidProgressException {
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;
}
}
115 changes: 113 additions & 2 deletions task-1/com.hyfacademy/model/Course.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,115 @@
package com.hyfacademy.model;

public class Course {
}
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 final String courseName;
private final String courseId;
private final int maxStudents;
private int enrolledCount;
private final int[] studentProgress;
private final Student[] students;
private static int counter = 1;

private static String generateId() {
return String.format("CRS-%03d", counter++);
}

public Course(String courseName, int maxStudents) {
this.courseName = courseName;
this.maxStudents = maxStudents;
this.courseId = generateId();
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 int[] getStudentProgress() {
return studentProgress;
}

public Student[] getStudents() {
return students;
}

public void enrol(Student student) {
if (enrolledCount >= maxStudents) {
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++;
}

public void updateProgress(Student student, int progress) {
if (progress < 0 || progress > 100) {
throw new InvalidProgressException(progress);
}

for (int i = 0; i < enrolledCount; i++) {
if (students[i].getUserId().equals(student.getUserId())) {
studentProgress[i] = progress;
return;
}
}

throw new EnrolmentException(student.getName() + " is not enrolled in '" + courseName + "'");
}

public int getStudentProgress(Student student) {
for (int i = 0; i < enrolledCount; i++) {
if (students[i].getUserId().equals(student.getUserId())) {
return studentProgress[i];
}
}

throw new EnrolmentException(student.getName() + " is not enrolled in '" + courseName + "'");
}

public boolean isFull() {
return enrolledCount >= maxStudents;
}

public abstract String getCourseType();

public abstract String getScheduleInfo();

@Override
public String toString() {
return "[" + courseId + "] "
+ courseName
+ " ("
+ getCourseType()
+ ") | Enrolled: "
+ enrolledCount
+ "/"
+ maxStudents;
}
}
130 changes: 128 additions & 2 deletions task-1/com.hyfacademy/model/LiveCohortCourse.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,130 @@
package com.hyfacademy.model;
import com.hyfacademy.service.Reportable;

public class LiveCohortCourse {
}
public class LiveCohortCourse extends Course implements Reportable {

private final String startDate;
private final String endDate;
private Mentor mentor;

public LiveCohortCourse(
String courseName,
int maxStudents,
String startDate,
String endDate,
Mentor mentor
) {
super(courseName, maxStudents);

this.startDate = startDate;
this.endDate = endDate;

assignMentor(mentor);
}

public String getStartDate() {
return startDate;
}

public String getEndDate() {
return endDate;
}

public Mentor getMentor() {
return mentor;
}

public void assignMentor(Mentor mentor) {
this.mentor = mentor;
mentor.assignToCourse(this);
}

@Override
public String getCourseType() {
return "Live Cohort";
}

@Override
public String getScheduleInfo() {
return startDate
+ " to "
+ endDate
+ " | Mentor: "
+ mentor.getName();
}
@Override
public String generateReport() {

StringBuilder report = new StringBuilder();

report.append("══════════════════════════════════════════\n");
report.append(" COURSE REPORT — Live Cohort\n");
report.append("══════════════════════════════════════════\n");

report.append(" ID : ").append(getCourseId()).append("\n");
report.append(" Name : ").append(getCourseName()).append("\n");

report.append(" Schedule : ")
.append(startDate)
.append(" to ")
.append(endDate)
.append("\n");

report.append(" Mentor : ")
.append(mentor.getName())
.append("\n");

report.append(" Capacity : ")
.append(getEnrolledCount())
.append("/")
.append(getMaxStudents())
.append(isFull() ? " — FULL\n" : " — Open\n");

report.append("──────────────────────────────────────────\n");
report.append(" STUDENT PROGRESS\n");
report.append("──────────────────────────────────────────\n");

int total = 0;

for (int i = 0; i < getEnrolledCount(); i++) {

Student student = getStudents()[i];
int progress = getStudentProgress()[i];

total += progress;

int filled = Math.round(progress / 10.0f);

StringBuilder bar = new StringBuilder();

for (int j = 0; j < 10; j++) {
if (j < filled) {
bar.append("█");
} else {
bar.append("░");
}
}

report.append(String.format(
" %-9s %-20s %3d%% %s%n",
student.getUserId(),
student.getName(),
progress,
bar
));
}

int average = getEnrolledCount() == 0
? 0
: total / getEnrolledCount();

report.append("──────────────────────────────────────────\n");
report.append(" Avg Progress : ")
.append(average)
.append("%\n");

report.append("══════════════════════════════════════════");

return report.toString();
}
}
40 changes: 39 additions & 1 deletion task-1/com.hyfacademy/model/Mentor.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
package com.hyfacademy.model;

public class Mentor {
import com.hyfacademy.exception.EnrolmentException;

public class Mentor extends User {
private final String expertise;
private final Course [] assignedCourses ;
private int courseCount;
private static int counter = 1;

private static String generateId () {
return String.format("COA-%03d", counter++);
}

public Mentor (String name, String email, String expertise) {
super(name,email,generateId());
this.expertise = expertise;
this.assignedCourses = new Course[3];
this.courseCount = 0 ;

}
public void assignToCourse(Course course) {
if (courseCount >= assignedCourses.length)
{
throw new EnrolmentException(getName() + " cannot be assigned to more than 3 courses");
}
assignedCourses[courseCount] = course ;
courseCount ++;
}

public Course[] getAssignedCourses () {
return assignedCourses;
}

public String getExpertise() {
return expertise;
}
@Override
public String getRole() {
return "MENTOR";
}
}
Loading