Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions task-1/com.hyfacademy/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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) {
new PlatformService().run();
}
}
}
14 changes: 13 additions & 1 deletion task-1/com.hyfacademy/exception/AlreadyEnrolledException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
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 enroled in '%s'", studentName,courseName));
this.studentName = studentName;
this.courseName = courseName;
}

public String getStudentName(){return studentName;}
public String getCourseName(){return courseName;}

}
13 changes: 12 additions & 1 deletion task-1/com.hyfacademy/exception/CourseFullException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
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;}
}
5 changes: 4 additions & 1 deletion task-1/com.hyfacademy/exception/EnrolmentException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package com.hyfacademy.exception;

public class EnrolmentException {
public class EnrolmentException extends RuntimeException{
public EnrolmentException(String message){
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
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;
}
public int getAttemptedValue(){ return attemptedValue;}
}
74 changes: 73 additions & 1 deletion task-1/com.hyfacademy/model/Course.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,76 @@
package com.hyfacademy.model;

public class Course {
import com.hyfacademy.exception.*;
import com.hyfacademy.service.Enrollable;

public abstract class Course implements Enrollable {
private static int idCounter = 1;

private final String courseName;
private final String courseId;
private final int maxStudents;
protected int enrolledCount;
protected Student[] enrolledStudents;
protected int[] studentProgress;

public Course(String courseName, int maxStudents){
this.courseName = courseName;
this.maxStudents = maxStudents;
this.courseId = String.format("CRS-%03d", idCounter++);
this.enrolledStudents = new Student[maxStudents];
this.studentProgress = new int[maxStudents];
}

public String getCourseName() { return courseName; }
public String getCourseId() { return courseId; }
public int getMaxStudents() { return maxStudents;}
public int getEnrolledCount() { return enrolledCount;}
public boolean isFull() { return enrolledCount >= maxStudents;}

@Override
public void enrol(Student student){
if (isFull())
throw new CourseFullException(courseName, maxStudents);
if (findStudentIndex(student) >= 0)
throw new AlreadyEnrolledException(student.getName(), courseName);
enrolledStudents[enrolledCount] = student;
studentProgress[enrolledCount] = 0;
enrolledCount++;
}

public void updateProgress(Student student, int progress){
if (progress <0 || progress >100)
throw new InvalidProgressException(progress);
int idx = findStudentIndex(student);
if (idx <0)
throw new EnrolmentException(
String.format("Student %s is not enrolled in %s", student.getName(), courseName)
);
studentProgress[idx] = progress;
}

public int getStudentProgress(Student student){
int idx = findStudentIndex(student);
if (idx <0)
throw new EnrolmentException(
String.format("Student %s is not enroled in %s", student.getName(), courseName)
);
return studentProgress[idx];
}

private int findStudentIndex(Student student){
for (int i = 0; i < enrolledCount; i++){
if (enrolledStudents[i].getUserId().equals(student.getUserId())) return i;
}
return -1;
}

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);
}
}
75 changes: 74 additions & 1 deletion task-1/com.hyfacademy/model/LiveCohortCourse.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,77 @@
package com.hyfacademy.model;

public class LiveCohortCourse {
import com.hyfacademy.service.Reportable;

public class LiveCohortCourse extends Course implements Reportable {
private final String startDate;
private final String endDate;
private Mentor mentor;

public LiveCohortCourse(String name, int maxStudents, String startDate, String endDate){
super(name, maxStudents);
this.startDate = startDate;
this.endDate = endDate;
}

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(){
String name = mentor != null? mentor.getName() : "TBD";
return String.format("%s to %s | Mentor: %s", startDate, endDate, name);

}

@Override
public String generateReport(){
String eq = "══════════════════════════════════════════";
String bq = "──────────────────────────────────────────";
String dq = "══════════════════════════════════════════";
StringBuilder sb = new StringBuilder();
sb.append(eq).append("\n");
sb.append("COURSE REPORT - Live Cohort\n");
sb.append(eq).append("\n");
sb.append(String.format("%-12s: %s%n", "ID", getCourseId()));
sb.append(String.format("%-12s: %s%n", "Name", getCourseName()));
sb.append(String.format("%-12s: %s%n", "Capacity", capacityStatus()));
sb.append(String.format("%-12s: %s%n", "Schedule", getScheduleInfo()));
sb.append(dq).append("\n");
sb.append(" STUDENT PROGRESS\n");
sb.append(dq).append("\n");
int total = 0;
for (int i = 0; i < enrolledCount; i++){
int pct = studentProgress[i];
total += pct;
sb.append(String.format(" %-9s %-20s %3d%% %s%n",
enrolledStudents[i].getUserId(),
enrolledStudents[i].getName(),
pct,
progressBar(pct)

));
}
sb.append(dq).append("\n");
int avg = enrolledCount > 0 ? total / enrolledCount : 0;
sb.append(String.format(" Avg Progress : %d%%%n", avg));
sb.append(eq);
return sb.toString();
}

private static String progressBar(int percent){
int filled = Math.round(percent / 10.0f);
StringBuilder bar = new StringBuilder();
for (int i = 0; i < 10; i++) bar.append(i < filled ? '█' : '░');
return bar.toString();
}

}
28 changes: 27 additions & 1 deletion task-1/com.hyfacademy/model/Mentor.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
package com.hyfacademy.model;

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

public class Mentor extends User{
private static int idCounter = 1;

private final String expertise;
private final Course[] assignedCourses = new Course[3];
private int courseCount;

public Mentor(String name, String email, String expertise){
super(name, email, String.format("COA-%03d", idCounter++));
this.expertise = expertise;
}

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

public void assignToCourse(Course course){
if (courseCount == assignedCourses.length)
throw new EnrolmentException(
getName() + " is already assigned to the maximum number of courses"
);
assignedCourses[courseCount++] = course;
}

public Course[] getAssignedCourses(){ return assignedCourses;}
public String getExpertise() { return expertise;}
}
62 changes: 61 additions & 1 deletion task-1/com.hyfacademy/model/SelfPacedCourse.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
package com.hyfacademy.model;

public class SelfPacedCourse {
import com.hyfacademy.service.Reportable;

public class SelfPacedCourse extends Course implements Reportable {
private final int estimateHours;

public SelfPacedCourse(String name, int maxStudents, int estimateHours){
super(name, maxStudents);
this.estimateHours = estimateHours;
}

public int getEstimatedHours(){ return estimateHours;}

@Override
public String getCourseType(){ return "Self-Paced";}

@Override
public String getScheduleInfo(){
return String.format("Estimated: %d hours - complete at your own pace", estimateHours);
}

@Override
public String generateReport(){
String eq = "══════════════════════════════════════════";
String bq = "──────────────────────────────────────────";
String dq = "══════════════════════════════════════════";
StringBuilder sb = new StringBuilder();
sb.append(eq).append("\n");
sb.append("COURSE REPORT - Self-Paced\n");
sb.append(eq).append("\n");
sb.append(String.format("%-12s: %s%n", "ID", getCourseId()));
sb.append(String.format("%-12s: %s%n", "Name", getCourseName()));
sb.append(String.format("%-12s: %s%n", "Capacity", capacityStatus()));
sb.append(String.format("%-12s: %s%n", "Est. Hours", getEstimatedHours()));
sb.append(dq).append("\n");
sb.append(" STUDENT PROGRESS\n");
sb.append(dq).append("\n");
int total = 0;
for (int i = 0; i < enrolledCount; i++){
int pct = studentProgress[i];
total += pct;
sb.append(String.format(" %-9s %-20s %3d%% %s%n",
enrolledStudents[i].getUserId(),
enrolledStudents[i].getName(),
pct,
progressBar(pct)

));
}
sb.append(dq).append("\n");
int avg = enrolledCount > 0 ? total / enrolledCount : 0;
sb.append(String.format(" Avg Progress : %d%%%n", avg));
sb.append(eq);
return sb.toString();
}

private static String progressBar(int percent){
int filled = Math.round(percent / 10.0f);
StringBuilder bar = new StringBuilder();
for (int i = 0; i < 10; i++) bar.append(i < filled ? '█' : '░');
return bar.toString();
}
}
36 changes: 35 additions & 1 deletion task-1/com.hyfacademy/model/Student.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
package com.hyfacademy.model;

public class Student {
import com.hyfacademy.exception.*;

public class Student extends User {
private static int idCounter = 1;

private final Course[] enrolledCourses = new Course[5];
private int courseCount;

public Student(String name, String email){
super(name, email, String.format("STU-%03d", idCounter++));
}

@Override
public String getRole(){return "STUDENT";}

public void enrol(Course course){
if (courseCount == enrolledCourses.length)
throw new EnrolmentException(getName() + " cannot enrol in more than 5 courses");
for (int i =0; i < courseCount; i++){
if (enrolledCourses[i].getCourseId().equals(course.getCourseId()))
throw new AlreadyEnrolledException(getName(), course.getCourseName());
}
course.enrol(this);
enrolledCourses[courseCount++] = course;
}

public int getProgress(String courseName){
for(int i = 0; i < courseCount; i++){
if (enrolledCourses[i].getCourseName().equals(courseName))
return enrolledCourses[i].getStudentProgress(this);
}
throw new EnrolmentException(String.format("%s is not enrolled in '%s'", getName(), courseName));
}
public Course[] getCourses() {return enrolledCourses;}
public int getCourseCount() {return courseCount;}
}
24 changes: 23 additions & 1 deletion task-1/com.hyfacademy/model/User.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
package com.hyfacademy.model;

public class User {
public abstract class User {
private final String name;
private final String email;
private final 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 String.format("[%s] %s | %s", getRole(), name, email);
}

@Override
public String toString(){return getSummary();}
}
Loading