forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcourse_registration_system.py
48 lines (40 loc) · 1.71 KB
/
course_registration_system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from typing import Dict, List
from course import Course
from student import Student
from registration import Registration
from datetime import datetime
class CourseRegistrationSystem:
instance = None
courses: Dict[str, Course] = {}
students: Dict[int, Student] = {}
registrations: List[Registration] = []
@staticmethod
def get_instance():
if CourseRegistrationSystem.instance is None:
CourseRegistrationSystem.instance = CourseRegistrationSystem()
return CourseRegistrationSystem.instance
def add_course(self, course: Course):
self.courses[course.get_code()] = course
def add_student(self, student: Student):
self.students[student.get_id()] = student
def search_courses(self, query: str) -> List[Course]:
result = []
for course in self.courses.values():
if query in course.get_code() or query in course.get_name():
result.append(course)
return result
def register_course(self, student: Student, course: Course) -> bool:
if course.get_enrolled_students() < course.get_max_capacity():
registration = Registration(student, course, datetime.now())
self.registrations.append(registration)
student.get_registered_courses().append(course)
course.set_enrolled_students(course.get_enrolled_students() + 1)
self._notify_observers(course)
return True
return False
def get_registered_courses(self, student: Student) -> List[Course]:
return student.get_registered_courses()
def _notify_observers(self, course: Course):
# Notify observers (e.g., UI) about the updated course enrollment
# ...
pass