Menu
CoddyTech

在线学习平台

本课属于 Coddy Python 学习旅程的“面向对象编程”单元。第 61 课,共 64 课。

challenge icon

挑战

困难

在这个挑战中,你将实现一个面向对象的在线学习平台系统。

  • course.py - 实现 Course 类
  • student.py - 实现 Student 类
  • instructor.py - 实现 Instructor 类

亲手试试

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}")
    
    # 尝试让同一名学生选课两次
    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}")

面向对象编程中的所有课

自己练习: Python 在线编译器