Menu
Coddy logo textTech

Eラーニングプラットフォーム

CoddyのPythonジャーニー「Object Oriented Programming」セクションの一部 — レッスン 61/64。

challenge icon

チャレンジ

難しい

このチャレンジでは、オブジェクト指向のeラーニングプラットフォームシステムを実装します。

  • course.py - Courseクラスを実装します
  • student.py - Studentクラスを実装します
  • instructor.py - Instructorクラスを実装します

チートシート

このレッスンでは、3つの主要なクラスを持つオブジェクト指向のeラーニングプラットフォームシステムの構築に焦点を当てます。

  • Course class - プラットフォーム内の教育コースを表します
  • Student class - コースに登録できる学習者を表します
  • Instructor class - コースを作成および管理できる講師を表します

このシステムは、現実世界のエンティティを独自のプロパティとメソッドを持つクラスとしてモデリングすることで、オブジェクト指向プログラミングの原則を実証します。

自分で試してみよう

from course import Course
from student import Student
from instructor import Instructor

# テストケースのハンドラー
test_case = input()

if test_case == "course_creation":
    # 異なる定員でのコース作成をテスト
    course1 = Course("Python 101", "Introduction to Python", 20)
    course2 = Course("Data Science", "Advanced data analysis techniques")
    
    print(f"Course 1: {course1.title}, Max Capacity: {course1.max_capacity}")
    print(f"Course 2: {course2.title}, Max Capacity: {course2.max_capacity}")
    print(f"Initial student count: {len(course1.students)}")

elif test_case == "student_enrollment":
    # 受講登録プロセスのテスト
    course = Course("Python 101", "Introduction to Python", 2)
    student1 = Student("Alice", "alice@example.com")
    student2 = Student("Bob", "bob@example.com")
    student3 = Student("Charlie", "charlie@example.com")
    
    # 学生を登録
    result1 = student1.enroll(course)
    result2 = student2.enroll(course)
    result3 = student3.enroll(course)  # 失敗するはず(定員オーバー)
    
    print(f"Enrollment 1 result: {result1}")
    print(f"Enrollment 2 result: {result2}")
    print(f"Enrollment 3 result: {result3}")
    
    # 同じ学生を2回登録してみる
    result4 = student1.enroll(course)
    print(f"Re-enrollment result: {result4}")
    
    # コースと学生の状態を確認
    print(f"Course has {len(course.students)} students")
    print(f"Student 1 has {len(student1.enrolled_courses)} enrolled courses")

elif test_case == "student_unenrollment":
    # 受講キャンセルプロセスのテスト
    course = Course("Python 101", "Introduction to Python")
    student = Student("Alice", "alice@example.com")
    
    # 登録してからキャンセル
    student.enroll(course)
    print(f"Before unenroll: Course has {len(course.students)} students")
    print(f"Before unenroll: Student has {len(student.enrolled_courses)} courses")
    
    result = student.unenroll(course)
    print(f"Unenrollment result: {result}")
    
    print(f"After unenroll: Course has {len(course.students)} students")
    print(f"After unenroll: Student has {len(student.enrolled_courses)} courses")
    
    # 再度キャンセルを試みる
    result = student.unenroll(course)
    print(f"Second unenrollment result: {result}")

elif test_case == "course_completion":
    # コース修了プロセスのテスト
    course = Course("Python 101", "Introduction to Python")
    student = Student("Alice", "alice@example.com")
    
    # 登録して修了
    student.enroll(course)
    print(f"Before completion: Enrolled in {len(student.enrolled_courses)} courses")
    print(f"Before completion: Completed {len(student.completed_courses)} courses")
    
    result = student.complete_course(course)
    print(f"Completion result: {result}")
    
    print(f"After completion: Enrolled in {len(student.enrolled_courses)} courses")
    print(f"After completion: Completed {len(student.completed_courses)} courses")
    
    # コースの状態を確認
    print(f"Course has {len(course.students)} active students")
    print(f"Course has {len(course.completed_students)} completed students")

elif test_case == "instructor_assignment":
    # 講師の割り当てをテスト
    course = Course("Python 101", "Introduction to Python")
    instructor = Instructor("Dr. Smith", "smith@example.edu", "Computer Science")
    
    print(f"Before assignment: Instructor has {len(instructor.courses)} courses")
    print(f"Before assignment: Course instructor is {course.instructor}")
    
    result = instructor.assign_to_course(course)
    print(f"Assignment result: {result}")
    
    print(f"After assignment: Instructor has {len(instructor.courses)} courses")
    print(f"After assignment: Course instructor is {course.instructor.name}")

elif test_case == "course_management":
    # コース定員管理のテスト
    course = Course("Limited Course", "Test capacity", 2)
    student1 = Student("Alice", "alice@example.com")
    student2 = Student("Bob", "bob@example.com")
    student3 = Student("Charlie", "charlie@example.com")
    
    # コースを満員にする
    student1.enroll(course)
    student2.enroll(course)
    result = student3.enroll(course)  # 失敗するはず
    print(f"Enrolling to full course: {result}")
    
    # キャンセルによって空きを作る
    student1.unenroll(course)
    result = student3.enroll(course)  # 今度は成功するはず
    print(f"Enrolling after unenrollment: {result}")
    
    # 最終状態を確認
    print(f"Final students in course: {len(course.students)}")
    print(f"Students are: {[s.name for s in course.students]}")

elif test_case == "integrated_test":
    # システム全体の連携をテスト
    # コースを作成
    python_course = Course("Python 101", "Introduction to Python", 3)
    data_course = Course("Data Science", "Advanced data analysis", 2)
    
    # 学生を作成
    alice = Student("Alice", "alice@example.com")
    bob = Student("Bob", "bob@example.com")
    charlie = Student("Charlie", "charlie@example.com")
    
    # 講師を作成
    dr_smith = Instructor("Dr. Smith", "smith@example.edu", "Computer Science")
    
    # 講師を割り当て
    dr_smith.assign_to_course(python_course)
    dr_smith.assign_to_course(data_course)
    
    # 学生を登録
    alice.enroll(python_course)
    bob.enroll(python_course)
    charlie.enroll(python_course)
    
    alice.enroll(data_course)
    bob.enroll(data_course)  # これで data_course が満員になる
    
    # コースを修了
    alice.complete_course(python_course)
    
    # システムの状態を出力
    print("System State:")
    print(f"Python course has {len(python_course.students)} active students and {len(python_course.completed_students)} completed")
    print(f"Data course has {len(data_course.students)} active students")
    print(f"Dr. Smith teaches {len(dr_smith.courses)} courses")
    print(f"Alice is enrolled in {len(alice.enrolled_courses)} courses and completed {len(alice.completed_courses)}")
    print(f"Charlie is enrolled in {len(charlie.enrolled_courses)} courses")

elif test_case == "edge_cases":
    # エッジケースのテスト
    course = Course("Small Course", "Test edge cases", 1)
    student1 = Student("Alice", "alice@example.com")
    student2 = Student("Bob", "bob@example.com")
    
    # 定員ちょうどでの登録をテスト
    result1 = student1.enroll(course)
    result2 = student2.enroll(course)  # 失敗するはず
    print(f"Enrollment at capacity: {result1}")
    print(f"Enrollment beyond capacity: {result2}")
    
    # 学生が登録していないコースの修了をテスト
    other_course = Course("Other Course", "Not enrolled")
    result3 = student1.complete_course(other_course)
    print(f"Complete non-enrolled course: {result3}")
    
    # 学生が登録していないコースのキャンセルをテスト
    result4 = student2.unenroll(course)
    print(f"Unenroll from non-enrolled course: {result4}")
    
    # 講師の再割り当てをテスト
    instructor1 = Instructor("Dr. Smith", "smith@example.edu", "Python")
    instructor2 = Instructor("Dr. Jones", "jones@example.edu", "Java")
    
    instructor1.assign_to_course(course)
    print(f"First instructor: {course.instructor.name}")
    
    instructor2.assign_to_course(course)
    print(f"After reassignment: {course.instructor.name}")

elif test_case == "capacity_limits":
    # 定員制限のテスト
    zero_capacity = Course("Empty Course", "No students allowed", 0)
    student = Student("Alice", "alice@example.com")
    
    result1 = student.enroll(zero_capacity)
    print(f"Enroll in zero-capacity course: {result1}")
    
    large_capacity = Course("Huge Course", "Many students", 1000)
    result2 = student.enroll(large_capacity)
    print(f"Enroll in large-capacity course: {result2}")
    print(f"Large course has capacity for {large_capacity.max_capacity} students")

elif test_case == "multiple_courses":
    # 複数コースを持つ学生のテスト
    student = Student("Alice", "alice@example.com")
    course1 = Course("Python 101", "Intro to Python")
    course2 = Course("Java 101", "Intro to Java")
    course3 = Course("C++ 101", "Intro to C++")
    
    # すべてのコースに登録
    student.enroll(course1)
    student.enroll(course2)
    student.enroll(course3)
    print(f"Enrolled in {len(student.enrolled_courses)} courses")
    
    # いくつかのコースを修了
    student.complete_course(course1)
    student.complete_course(course2)
    print(f"After completion: Enrolled in {len(student.enrolled_courses)} courses")
    print(f"After completion: Completed {len(student.completed_courses)} courses")
    
    # 特定のコースを確認
    enrolled_titles = [c.title for c in student.enrolled_courses]
    completed_titles = [c.title for c in student.completed_courses]
    print(f"Still enrolled in: {enrolled_titles}")
    print(f"Completed: {completed_titles}")

elif test_case == "instructor_workload":
    # 複数コースを持つ講師のテスト
    instructor = Instructor("Dr. Smith", "smith@example.edu", ["Python", "Java", "C++"])
    course1 = Course("Python 101", "Intro to Python")
    course2 = Course("Java 101", "Intro to Java")
    course3 = Course("C++ 101", "Intro to C++")
    
    # すべてのコースに割り当て
    instructor.assign_to_course(course1)
    instructor.assign_to_course(course2)
    instructor.assign_to_course(course3)
    
    print(f"Instructor teaches {len(instructor.courses)} courses")
    course_titles = [c.title for c in instructor.courses]
    print(f"Courses taught: {course_titles}")
    
    # 講師が各コースに正しく割り当てられているか確認
    for i, course in enumerate([course1, course2, course3]):
        print(f"Course {i+1} instructor: {course.instructor.name}")

Object Oriented Programmingのすべてのレッスン