Platforma e-learningowa
Część sekcji Programowanie obiektowe ścieżki Python w Coddy. Lekcja 61 z 64.
Wyzwanie
TrudnyW tym wyzwaniu zaimplementujesz obiektowy system platformy e-learningowej.
course.py— zaimplementuj klasęCoursestudent.py— zaimplementuj klasęStudentinstructor.py— zaimplementuj klasęInstructor
Spróbuj swoich sił
from course import Course
from student import Student
from instructor import Instructor
# Obsługa przypadków testowych
test_case = input()
if test_case == "course_creation":
# Test tworzenia kursów o różnych limitach miejsc
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":
# Test procesu zapisywania studentów na kurs
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")
# Zapisz studentów na kurs
result1 = student1.enroll(course)
result2 = student2.enroll(course)
result3 = student3.enroll(course) # Powinno się nie udać (limit miejsc)
print(f"Enrollment 1 result: {result1}")
print(f"Enrollment 2 result: {result2}")
print(f"Enrollment 3 result: {result3}")
# Spróbuj zapisać tego samego studenta dwa razy
result4 = student1.enroll(course)
print(f"Re-enrollment result: {result4}")
# Sprawdź stan kursu i studenta
print(f"Course has {len(course.students)} students")
print(f"Student 1 has {len(student1.enrolled_courses)} enrolled courses")
elif test_case == "student_unenrollment":
# Test procesu wypisywania studenta z kursu
course = Course("Python 101", "Introduction to Python")
student = Student("Alice", "alice@example.com")
# Zapisz studenta na kurs, a następnie go wypisz
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")
# Spróbuj wypisać studenta ponownie
result = student.unenroll(course)
print(f"Second unenrollment result: {result}")
elif test_case == "course_completion":
# Test procesu ukończenia kursu
course = Course("Python 101", "Introduction to Python")
student = Student("Alice", "alice@example.com")
# Zapisz studenta na kurs i go ukończ
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")
# Sprawdź stan kursu
print(f"Course has {len(course.students)} active students")
print(f"Course has {len(course.completed_students)} completed students")
elif test_case == "instructor_assignment":
# Przetestuj przypisanie instruktora
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":
# Przetestuj zarządzanie limitem miejsc na kursie
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")
# Zapełnij kurs
student1.enroll(course)
student2.enroll(course)
result = student3.enroll(course) # Powinno się nie powieść
print(f"Enrolling to full course: {result}")
# Zwolnij miejsce, wypisując studenta
student1.unenroll(course)
result = student3.enroll(course) # Teraz powinno się udać
print(f"Enrolling after unenrollment: {result}")
# Sprawdź stan końcowy
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":
# Przetestuj współdziałanie całego systemu
# Utwórz kursy
python_course = Course("Python 101", "Introduction to Python", 3)
data_course = Course("Data Science", "Advanced data analysis", 2)
# Utwórz studentów
alice = Student("Alice", "alice@example.com")
bob = Student("Bob", "bob@example.com")
charlie = Student("Charlie", "charlie@example.com")
# Utwórz instruktora
dr_smith = Instructor("Dr. Smith", "smith@example.edu", "Computer Science")
# Przypisz instruktora
dr_smith.assign_to_course(python_course)
dr_smith.assign_to_course(data_course)
# Zapisz studentów na kurs
alice.enroll(python_course)
bob.enroll(python_course)
charlie.enroll(python_course)
alice.enroll(data_course)
bob.enroll(data_course) # To zapełnia data_course
# Ukończ kurs
alice.complete_course(python_course)
# Wyświetl stan systemu
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":
# Przetestuj przypadki brzegowe
course = Course("Small Course", "Test edge cases", 1)
student1 = Student("Alice", "alice@example.com")
student2 = Student("Bob", "bob@example.com")
# Przetestuj zapisy przy dokładnym osiągnięciu limitu miejsc
result1 = student1.enroll(course)
result2 = student2.enroll(course) # Powinno się nie powieść
print(f"Enrollment at capacity: {result1}")
print(f"Enrollment beyond capacity: {result2}")
# Przetestuj ukończenie kursu, na który student nie jest zapisany
other_course = Course("Other Course", "Not enrolled")
result3 = student1.complete_course(other_course)
print(f"Complete non-enrolled course: {result3}")
# Przetestuj wypisanie się z kursu, na który student nie jest zapisany
result4 = student2.unenroll(course)
print(f"Unenroll from non-enrolled course: {result4}")
# Przetestuj ponowne przypisanie instruktora
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":
# Przetestuj limity miejsc
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":
# Przetestuj studenta zapisanego na wiele kursów
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++")
# Zapisz się na wszystkie kursy
student.enroll(course1)
student.enroll(course2)
student.enroll(course3)
print(f"Enrolled in {len(student.enrolled_courses)} courses")
# Ukończ niektóre kursy
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")
# Sprawdź konkretne kursy
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":
# Przetestuj instruktora prowadzącego wiele kursów
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++")
# Przypisz instruktora do wszystkich kursów
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}")
# Sprawdź, czy instruktor jest prawidłowo przypisany do każdego kursu
for i, course in enumerate([course1, course2, course3]):
print(f"Course {i+1} instructor: {course.instructor.name}")Wszystkie lekcje w sekcji Programowanie obiektowe
1Podstawy programowania obiektowego
Pliki zewnętrzneWprowadzenie do programowania obiektowegoKlasy a obiektyParametr selfMetodyAtrybutyMetoda konstruktora (__init__)Powtórzenie – prosty kalkulator4Dziedziczenie
Podstawy dziedziczeniaFunkcja super()Nadpisywanie metodDziedziczenie wielokrotneKolejność rozwiązywania metodPodsumowanie – hierarchia pracowników7Metody specjalne
Wprowadzenie do metod magicznychPrzeciążanie operatorówMagiczne metody kontenerówPowtórka – własna lista10Wzorce projektowe, część 1
Wprowadzenie do wzorców projektowychWzorzec SingletonWzorzec FactoryWzorzec ObserverWzorzec Strategy13Wyzwania końcowe
Platforma e-learningowaSystem bankowyTworzenie postaci do gryUsługa wynajmu pojazdów2Dekoratory
Wprowadzenie do dekoratorówDekorator propertyDekorator metody statycznejDekorator metody klasowej5Polimorfizm
Ponowne omówienie nadpisywania metodTypowanie kaczeKlasy i metody abstrakcyjneProjektowanie interfejsówPodsumowanie — kalkulator kształtów8Zaawansowane koncepcje programowania obiektowego
Kompozycja a dziedziczenieMiksyMetody statyczne i klasoweDekoratory klasMenedżery kontekstu11Wzorce projektowe, część 2
Wzorzec PolecenieWzorzec AdapterWzorzec DekoratorWzorzec Metoda szablonowaWzorzec StanWzorzec Kompozyt3Właściwości klas
Zmienne instancji a zmienne klasoweDekoratory właściwościAtrybuty prywatnePowtórzenie — menedżer kont bankowych6Hermetyzacja
Public, Protected, Private — pamięćModyfikatory dostępuUkrywanie informacjiDekoratory właściwości — zaawansowanePodsumowanie — system ewidencji uczniów12Projekt: System zarządzania biblioteką
Przegląd projektuKlasy Book i UserSystem wypożyczeńFunkcja wyszukiwaniaInterfejs administratoraTestowanie i integracjaPoćwicz samodzielnie: Kompilator Python online