E-Learning-Plattform
Teil des Abschnitts Object Oriented Programming der Python-Journey von Coddy — Lektion 61 von 64.
Aufgabe
SchwerIn dieser Herausforderung wirst du ein objektorientiertes E-Learning-Plattform-System implementieren.
course.py- Implementiere dieCourse-Klassestudent.py- Implementiere dieStudent-Klasseinstructor.py- Implementiere dieInstructor-Klasse
Spickzettel
Diese Lektion konzentriert sich auf die Implementierung eines objektorientierten E-Learning-Plattform-Systems mit drei Hauptklassen:
- Course-Klasse - Repräsentiert Bildungskurse auf der Plattform
- Student-Klasse - Repräsentiert Lernende, die sich für Kurse einschreiben können
- Instructor-Klasse - Repräsentiert Lehrkräfte, die Kurse erstellen und verwalten können
Das System demonstriert Prinzipien der objektorientierten Programmierung, indem es reale Entitäten als Klassen mit eigenen Eigenschaften und Methoden modelliert.
Probier es selbst
from course import Course
from student import Student
from instructor import Instructor
# Testfall-Handler
test_case = input()
if test_case == "course_creation":
# Teste das Erstellen von Kursen mit unterschiedlichen Kapazitäten
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":
# Teste den Einschreibungsprozess für Studenten
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")
# Studenten einschreiben
result1 = student1.enroll(course)
result2 = student2.enroll(course)
result3 = student3.enroll(course) # Sollte fehlschlagen (Kapazität)
print(f"Enrollment 1 result: {result1}")
print(f"Enrollment 2 result: {result2}")
print(f"Enrollment 3 result: {result3}")
# Versuche, denselben Studenten zweimal einzuschreiben
result4 = student1.enroll(course)
print(f"Re-enrollment result: {result4}")
# Überprüfe Kurs- und Studentenstatus
print(f"Course has {len(course.students)} students")
print(f"Student 1 has {len(student1.enrolled_courses)} enrolled courses")
elif test_case == "student_unenrollment":
# Teste den Abmeldungsprozess für Studenten
course = Course("Python 101", "Introduction to Python")
student = Student("Alice", "alice@example.com")
# Einschreiben und dann abmelden
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")
# Versuche, die Abmeldung erneut durchzuführen
result = student.unenroll(course)
print(f"Second unenrollment result: {result}")
elif test_case == "course_completion":
# Teste den Prozess des Kursabschlusses
course = Course("Python 101", "Introduction to Python")
student = Student("Alice", "alice@example.com")
# Einschreiben und abschließen
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")
# Kursstatus überprüfen
print(f"Course has {len(course.students)} active students")
print(f"Course has {len(course.completed_students)} completed students")
elif test_case == "instructor_assignment":
# Teste die Zuweisung von Dozenten
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":
# Teste die Verwaltung der Kurskapazität
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")
# Kurs füllen
student1.enroll(course)
student2.enroll(course)
result = student3.enroll(course) # Sollte fehlschlagen
print(f"Enrolling to full course: {result}")
# Platz schaffen durch Abmeldung
student1.unenroll(course)
result = student3.enroll(course) # Sollte jetzt erfolgreich sein
print(f"Enrolling after unenrollment: {result}")
# Endzustand überprüfen
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":
# Teste das gesamte System im Zusammenspiel
# Kurse erstellen
python_course = Course("Python 101", "Introduction to Python", 3)
data_course = Course("Data Science", "Advanced data analysis", 2)
# Studenten erstellen
alice = Student("Alice", "alice@example.com")
bob = Student("Bob", "bob@example.com")
charlie = Student("Charlie", "charlie@example.com")
# Dozent erstellen
dr_smith = Instructor("Dr. Smith", "smith@example.edu", "Computer Science")
# Dozent zuweisen
dr_smith.assign_to_course(python_course)
dr_smith.assign_to_course(data_course)
# Studenten einschreiben
alice.enroll(python_course)
bob.enroll(python_course)
charlie.enroll(python_course)
alice.enroll(data_course)
bob.enroll(data_course) # Dies füllt data_course
# Einen Kurs abschließen
alice.complete_course(python_course)
# Systemstatus ausgeben
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":
# Teste Grenzfälle
course = Course("Small Course", "Test edge cases", 1)
student1 = Student("Alice", "alice@example.com")
student2 = Student("Bob", "bob@example.com")
# Teste Einschreibung bei exakter Kapazitätsgrenze
result1 = student1.enroll(course)
result2 = student2.enroll(course) # Sollte fehlschlagen
print(f"Enrollment at capacity: {result1}")
print(f"Enrollment beyond capacity: {result2}")
# Teste Abschluss eines Kurses, in dem der Student nicht eingeschrieben ist
other_course = Course("Other Course", "Not enrolled")
result3 = student1.complete_course(other_course)
print(f"Complete non-enrolled course: {result3}")
# Teste Abmeldung von einem Kurs, in dem der Student nicht eingeschrieben ist
result4 = student2.unenroll(course)
print(f"Unenroll from non-enrolled course: {result4}")
# Teste die Neuzuweisung eines Dozenten
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":
# Teste Kapazitätsgrenzen
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":
# Teste Student mit mehreren Kursen
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++")
# In alle Kurse einschreiben
student.enroll(course1)
student.enroll(course2)
student.enroll(course3)
print(f"Enrolled in {len(student.enrolled_courses)} courses")
# Einige Kurse abschließen
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")
# Spezifische Kurse verifizieren
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":
# Teste Dozent mit mehreren Kursen
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++")
# Allen Kursen zuweisen
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}")
# Überprüfe, ob der Dozent jedem Kurs korrekt zugewiesen wurde
for i, course in enumerate([course1, course2, course3]):
print(f"Course {i+1} instructor: {course.instructor.name}")Alle Lektionen in Object Oriented Programming
1Grundlagen der OOP
Externe DateienEinführung in die OOPKlassen vs. ObjekteDer self-ParameterMethodenAttributeKonstruktor-Methode (__init__)Zusammenfassung – Einfacher Taschenrechner4Vererbung
Grundlagen der VererbungDie super()-FunktionMethoden überschreibenMehrfachvererbungMethod Resolution OrderZusammenfassung - Mitarbeiter-Hierarchie7Spezielle Methoden
Einführung in Magic MethodsOperator-ÜberladungMagic Methods für ContainerRückblick - Eigene Liste10Entwurfsmuster Teil 1
Einführung in EntwurfsmusterSingleton-MusterFactory-MusterObserver-MusterStrategy-Muster13Abschlussherausforderungen
E-Learning-PlattformBanksystemSpielcharakter-EntwicklungFahrzeugvermietung2Dekoratoren
Einführung in DekoratorenProperty-DekoratorStatischer Methoden-DekoratorKlassenmethoden-Dekorator5Polymorphismus
Methoden-Überschreiben vertieftDuck TypingAbstrakte Klassen und MethodenInterface-DesignZusammenfassung – Formen-Rechner8Fortgeschrittene OOP-Konzepte
Komposition vs. VererbungMixinsStatische und KlassenmethodenKlassendekoratorenContext Manager3Klasseneigenschaften
Instanz- vs. KlassenvariablenProperty-DekoratorenPrivate AttributeZusammenfassung – Bankkonto-Manager6Kapselung
Public, Protected, Private MemberZugriffsmodifikatorenInformation HidingProperty-Decorators für FortgeschritteneZusammenfassung - Studentenverwaltungssystem12Projekt: Bibliotheksverwaltung
ProjektübersichtBuch- und Benutzerklassen