Nền tảng học trực tuyến
Thuộc phần Lập trình hướng đối tượng trong hành trình Python của Coddy. Bài học 61 trên 64.
Thử thách
KhóTrong thử thách này, bạn sẽ triển khai một hệ thống nền tảng học trực tuyến theo hướng đối tượng.
course.py- Triển khai lớpCoursestudent.py- Triển khai lớpStudentinstructor.py- Triển khai lớpInstructor
Tự làm thử
from course import Course
from student import Student
from instructor import Instructor
# Xử lý test case
test_case = input()
if test_case == "course_creation":
# Kiểm tra việc tạo các khóa học có sức chứa khác nhau
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":
# Kiểm tra quá trình ghi danh của học viên
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")
# Ghi danh học viên
result1 = student1.enroll(course)
result2 = student2.enroll(course)
result3 = student3.enroll(course) # Sẽ thất bại vì khóa học đã đủ chỗ
print(f"Enrollment 1 result: {result1}")
print(f"Enrollment 2 result: {result2}")
print(f"Enrollment 3 result: {result3}")
# Thử ghi danh cùng một học viên hai lần
result4 = student1.enroll(course)
print(f"Re-enrollment result: {result4}")
# Kiểm tra trạng thái của khóa học và học viên
print(f"Course has {len(course.students)} students")
print(f"Student 1 has {len(student1.enrolled_courses)} enrolled courses")
elif test_case == "student_unenrollment":
# Kiểm tra quá trình hủy ghi danh của học viên
course = Course("Python 101", "Introduction to Python")
student = Student("Alice", "alice@example.com")
# Ghi danh rồi hủy ghi danh
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")
# Thử hủy ghi danh lần nữa
result = student.unenroll(course)
print(f"Second unenrollment result: {result}")
elif test_case == "course_completion":
# Kiểm tra quá trình hoàn thành khóa học
course = Course("Python 101", "Introduction to Python")
student = Student("Alice", "alice@example.com")
# Ghi danh và hoàn thành khóa học
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")
# Kiểm tra trạng thái khóa học
print(f"Course has {len(course.students)} active students")
print(f"Course has {len(course.completed_students)} completed students")
elif test_case == "instructor_assignment":
# Kiểm thử việc phân công giảng viên
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":
# Kiểm thử việc quản lý sức chứa của khóa học
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")
# Ghi danh đủ số học viên cho khóa học
student1.enroll(course)
student2.enroll(course)
result = student3.enroll(course) # Thao tác này sẽ thất bại
print(f"Enrolling to full course: {result}")
# Tạo chỗ trống bằng cách hủy ghi danh
student1.unenroll(course)
result = student3.enroll(course) # Thao tác này giờ sẽ thành công
print(f"Enrolling after unenrollment: {result}")
# Kiểm tra trạng thái cuối cùng
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":
# Kiểm thử cách toàn bộ hệ thống phối hợp hoạt động
# Tạo các khóa học
python_course = Course("Python 101", "Introduction to Python", 3)
data_course = Course("Data Science", "Advanced data analysis", 2)
# Tạo các học viên
alice = Student("Alice", "alice@example.com")
bob = Student("Bob", "bob@example.com")
charlie = Student("Charlie", "charlie@example.com")
# Tạo giảng viên
dr_smith = Instructor("Dr. Smith", "smith@example.edu", "Computer Science")
# Phân công giảng viên
dr_smith.assign_to_course(python_course)
dr_smith.assign_to_course(data_course)
# Ghi danh học viên
alice.enroll(python_course)
bob.enroll(python_course)
charlie.enroll(python_course)
alice.enroll(data_course)
bob.enroll(data_course) # Thao tác này khiến data_course đạt sức chứa tối đa
# Hoàn thành một khóa học
alice.complete_course(python_course)
# In ra trạng thái hệ thống
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":
# Kiểm tra các trường hợp đặc biệt
course = Course("Small Course", "Test edge cases", 1)
student1 = Student("Alice", "alice@example.com")
student2 = Student("Bob", "bob@example.com")
# Kiểm tra việc ghi danh khi khóa học đạt đúng sức chứa tối đa
result1 = student1.enroll(course)
result2 = student2.enroll(course) # Thao tác này sẽ thất bại
print(f"Enrollment at capacity: {result1}")
print(f"Enrollment beyond capacity: {result2}")
# Kiểm tra việc hoàn thành khóa học mà học viên chưa ghi danh
other_course = Course("Other Course", "Not enrolled")
result3 = student1.complete_course(other_course)
print(f"Complete non-enrolled course: {result3}")
# Kiểm tra việc hủy ghi danh khỏi khóa học mà học viên chưa ghi danh
result4 = student2.unenroll(course)
print(f"Unenroll from non-enrolled course: {result4}")
# Kiểm tra việc phân công lại giảng viên
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":
# Kiểm tra giới hạn sức chứa
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":
# Kiểm tra học viên ghi danh nhiều khóa học
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++")
# Ghi danh vào tất cả khóa học
student.enroll(course1)
student.enroll(course2)
student.enroll(course3)
print(f"Enrolled in {len(student.enrolled_courses)} courses")
# Hoàn thành một số khóa học
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")
# Kiểm tra các khóa học cụ thể
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":
# Kiểm tra giảng viên phụ trách nhiều khóa học
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++")
# Phân công giảng viên cho tất cả khóa họ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}")
# Kiểm tra giảng viên đã được phân công đúng cho từng khóa học
for i, course in enumerate([course1, course2, course3]):
print(f"Course {i+1} instructor: {course.instructor.name}")Tất cả bài học trong Lập trình hướng đối tượng
1Nền tảng về lập trình hướng đối tượng
Tệp bên ngoàiGiới thiệu về lập trình hướng đối tượngLớp và đối tượngTham số selfPhương thứcThuộc tínhPhương thức khởi tạo (__init__)Ôn tập: máy tính đơn giản4Kế thừa
Kế thừa cơ bảnHàm super()Ghi đè phương thứcKế thừa đa lớpThứ tự phân giải phương thứcÔn tập: hệ thống phân cấp nhân viên7Phương thức đặc biệt
Giới thiệu về phương thức ma thuậtNạp chồng toán tửPhương thức ma thuật của vùng chứaÔn tập: danh sách tùy chỉnh13Thử thách cuối cùng
Nền tảng học trực tuyếnHệ thống ngân hàngPhát triển nhân vật trong trò chơiDịch vụ cho thuê phương tiện2Decorator
Giới thiệu về decoratorDecorator thuộc tínhDecorator phương thức tĩnhDecorator phương thức lớp5Tính đa hình
Xem lại cách ghi đè phương thứcKiểu gõ vịtLớp và phương thức trừu tượngThiết kế giao diệnÔn tập: máy tính hình học8Các khái niệm OOP nâng cao
Kết hợp và kế thừaMixinPhương thức tĩnh và phương thức lớpDecorator lớpTrình quản lý ngữ cảnhTự luyện tập: Trình biên dịch Python online