Eラーニングプラットフォーム
CoddyのPHPジャーニー「オブジェクト指向プログラミング」セクションの一部。レッスン 88/91。
チャレンジ
簡単E-Learning Platformを構築して、コース、学生、講師を管理しましょう。この包括的なチャレンジでは、継承、インターフェース、カプセル化、デザインパターンを、まとまりのあるシステムに統合します。
コードを6つのファイルに分けて整理します。
User.php:学生と講師の両方の基底となる abstractUserクラスを Create します。id(int)とname(string)にはコンストラクタプロモーションを使用し、both public readonly とします。子クラスが実装する必要がある abstract メソッドgetRole(): stringを含めます。Student.php:User ファイルを読み込みます。Userを extending するStudentクラスを Create します。学生は、enrolled courses を private array で追跡し、各コースの進捗(完了 percentage)も管理します。以下を実装します。getRole(): string:"Student"を返しますenrollInCourse(Course $course): string:コースに capacity がある場合に学生を enroll し、success または failure のメッセージを返しますgetEnrolledCourses(): array:enrolled courses の array を返しますupdateProgress(string $courseTitle, int $percent): void:コースの progress(0-100)を設定しますgetProgress(string $courseTitle): int:progress percentage を返し、enrolled でない場合は 0 を返します
Instructor.php:User ファイルを読み込みます。Userを extending するInstructorクラスを Create します。講師はコースを Create および管理できます。以下を実装します。getRole(): string:"Instructor"を返しますcreateCourse(string $title, int $capacity): Course:この講師が assigned された新しい Course を Create して返します
Course.php:Instructor ファイルを読み込みます。title(string、public readonly)、instructor(Instructor、public readonly)、capacity(int、private)にコンストラクタプロモーションを使用してCourseクラスを Create します。enrolled students を private array で追跡します。以下を実装します。getCapacity(): int:最大 capacity を返しますgetEnrolledCount(): int:current enrollment count を返しますhasCapacity(): bool:さらに学生が enroll できる場合は true を返しますaddStudent(Student $student): bool:capacity が許す場合に学生を add し、success status を返しますgetStudents(): array:enrolled students の array を返します
Platform.php:Course ファイルと Student ファイルを読み込みます。システム全体を管理するPlatformクラスを Create します。courses と users を private arrays に保存します。以下を実装します。addCourse(Course $course): void:コースを platform に add しますfindCourse(string $title): ?Course:title でコースを Find しますenrollStudent(Student $student, string $courseTitle): string:学生をコースに 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 ファイルを読み込みます。3つの入力を受け取ります:講師データ(JSON)、Create する courses(JSON)、実行する operations(JSON)。講師 JSON の形式:
{"id": 1, "name": "Dr. Smith"}コース 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メソッドを使用)。courses を platform に add します。その後、各 operation を処理します。"enroll":新しい Student を Create して enroll し、enrollStudent()の結果を出力します"progress":学生の progress を更新し、"[name] progress in [course]: [percent]%"を出力します"stats":getCourseStats()の結果を出力します
後で progress を更新できるように、created students を ID で追跡します。各 operation の結果を新しい行に出力します。
このチャレンジでは、継承によって user hierarchy をどのように整理して Create できるか、カプセル化によって enrollment data を保護できるか、そして platform が courses、students、instructors 間の関係を管理する 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からインストラクターを作成する
// TODO: インストラクターの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オンラインコンパイラ