Menu
Coddy logo textTech

온라인 학습 플랫폼

Coddy PHP 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 91개 중 88번째.

challenge icon

챌린지

쉬움

E-Learning Platform을 구축해 보겠습니다. 이 플랫폼은 courses, students, instructors를 관리합니다. 이 종합 challenge는 inheritance, interfaces, encapsulation, design patterns를 하나의 일관된 system으로 결합합니다.

코드를 여섯 개의 파일로 구성합니다:

  • User.php: students와 instructors 모두의 기반이 되는 abstract User class를 Create합니다. id (int)와 name (string)에 constructor promotion을 사용하며, both를 public readonly로 지정합니다. 자식 class가 구현해야 하는 abstract method getRole(): string을 포함합니다.
  • Student.php: User 파일을 포함합니다. User를 extending하는 Student class를 Create합니다. Students는 private array에 자신이 enrolled한 courses를 기록하고, 각 course에 대한 progress(완료된 percentage)를 추적합니다. 다음을 구현합니다:
    • getRole(): string: "Student"를 반환합니다.
    • enrollInCourse(Course $course): string: course에 capacity가 있는 경우 student를 enroll하고, success 또는 failure message를 반환합니다.
    • getEnrolledCourses(): array: enrolled된 courses의 array를 반환합니다.
    • updateProgress(string $courseTitle, int $percent): void: course의 progress(0-100)를 설정합니다.
    • getProgress(string $courseTitle): int: progress percentage를 반환하거나, enrolled되지 않은 경우 0을 반환합니다.
  • Instructor.php: User 파일을 포함합니다. User를 extending하는 Instructor class를 Create합니다. Instructors는 courses를 Create하고 관리할 수 있습니다. 다음을 구현합니다:
    • getRole(): string: "Instructor"를 반환합니다.
    • createCourse(string $title, int $capacity): Course: 이 instructor가 assigned된 새로운 Course를 Create하고 반환합니다.
  • Course.php: Instructor 파일을 포함합니다. title (string, public readonly), instructor (Instructor, public readonly), capacity (int, private)에 constructor promotion을 사용하는 Course class를 Create합니다. enrolled된 students는 private array에 기록합니다. 다음을 구현합니다:
    • getCapacity(): int: maximum capacity를 반환합니다.
    • getEnrolledCount(): int: current enrollment count를 반환합니다.
    • hasCapacity(): bool: 더 많은 students가 enroll할 수 있으면 true를 반환합니다.
    • addStudent(Student $student): bool: capacity가 허용하는 경우 student를 추가하고, success status를 반환합니다.
    • getStudents(): array: enrolled된 students의 array를 반환합니다.
  • Platform.php: Course 및 Student 파일을 포함합니다. entire system을 관리하는 Platform class를 Create합니다. courses와 users는 private arrays에 저장합니다. 다음을 구현합니다:
    • addCourse(Course $course): void: platform에 course를 추가합니다.
    • findCourse(string $title): ?Course: title로 course를 찾습니다.
    • enrollStudent(Student $student, string $courseTitle): string: student를 course에 enroll합니다. "Course not found", "Course is full" 또는 "[name] enrolled in [title]"을 반환합니다.
    • getCourseStats(string $title): string: "[title]: [enrolled]/[capacity] students" 또는 "Course not found"를 반환합니다.
  • main.php: Platform 파일을 포함합니다. instructor data (JSON), Create할 courses (JSON), 수행할 operations (JSON)이라는 세 가지 입력을 받습니다.

    Instructor JSON 형식:

    {"id": 1, "name": "Dr. Smith"}

    Courses JSON 형식:

    [{"title": "PHP Basics", "capacity": 2}, {"title": "OOP Mastery", "capacity": 3}]

    Operations JSON 형식:

    [{"type": "enroll", "student_id": 1, "student_name": "Alice", "course": "PHP Basics"}, {"type": "progress", "student_id": 1, "course": "PHP Basics", "percent": 50}, {"type": "stats", "course": "PHP Basics"}]

    platform, instructor 및 모든 courses를 Create합니다(instructor의 createCourse method 사용). courses를 platform에 추가합니다. 그런 다음 각 operation을 처리합니다:

    • "enroll": 새로운 Student를 Create하고 enroll합니다. enrollStudent()의 result를 출력합니다.
    • "progress": student progress를 update하고 "[name] progress in [course]: [percent]%"를 출력합니다.
    • "stats": getCourseStats()의 result를 출력합니다.

    나중에 progress를 update할 수 있도록 created된 students를 ID별로 추적합니다. 각 operation의 result를 새 줄에 출력합니다.

이 challenge는 inheritance가 어떻게 깔끔한 user hierarchy를 만들고, encapsulation이 enrollment data를 보호하며, platform이 courses, students, instructors 간의 relationships를 관리하는 coordinator로 작동하는지를 보여 줍니다.

직접 해보기

<?php

require_once 'Platform.php';

// 입력 읽기
$instructorData = (array)json_decode(trim(fgets(STDIN)), true);
$coursesData = (array)json_decode(trim(fgets(STDIN)), true);
$operations = (array)json_decode(trim(fgets(STDIN)), true);

// TODO: 플랫폼 생성

// TODO: instructorData에서 instructor 생성

// TODO: instructor의 createCourse 메서드를 사용하여 모든 코스 생성
// 그리고 플랫폼에 추가

// TODO: 생성된 학생들을 ID로 추적
$students = [];

// TODO: 각 작업 처리
foreach ($operations as $op) {
    $operation = (array)$op;
    
    // TODO: "enroll" 작업 처리
    // 새 Student를 생성하고 등록
    // enrollStudent()의 결과 출력
    
    // TODO: "progress" 작업 처리
    // 학생 진행 상황 업데이트
    // "[name] progress in [course]: [percent]%"를 출력
    
    // TODO: "stats" 작업 처리
    // getCourseStats()의 결과 출력
}

?>

객체 지향 프로그래밍의 모든 레슨

직접 연습해 보세요: 온라인 PHP 컴파일러