-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathcourse_registration_system.go
113 lines (92 loc) · 2.69 KB
/
course_registration_system.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package courseregistrationsystem
import (
"fmt"
"strings"
"sync"
)
type CourseRegistrationSystem struct {
courses map[string]*Course
students map[int]*Student
registrations []*Registration
mu sync.RWMutex
}
var (
instance *CourseRegistrationSystem
once sync.Once
)
func GetRegistrationSystem() *CourseRegistrationSystem {
once.Do(func() {
instance = &CourseRegistrationSystem{
courses: make(map[string]*Course),
students: make(map[int]*Student),
registrations: make([]*Registration, 0),
}
})
return instance
}
func (rs *CourseRegistrationSystem) AddCourse(course *Course) {
rs.mu.Lock()
defer rs.mu.Unlock()
rs.courses[course.Code] = course
}
func (rs *CourseRegistrationSystem) AddStudent(student *Student) {
rs.mu.Lock()
defer rs.mu.Unlock()
rs.students[student.ID] = student
}
func (rs *CourseRegistrationSystem) SearchCourses(query string) []*Course {
rs.mu.RLock()
defer rs.mu.RUnlock()
var result []*Course
query = strings.ToLower(query)
for _, course := range rs.courses {
if strings.Contains(strings.ToLower(course.Code), query) ||
strings.Contains(strings.ToLower(course.Name), query) {
result = append(result, course)
}
}
return result
}
func (rs *CourseRegistrationSystem) RegisterCourse(student *Student, course *Course) error {
rs.mu.Lock()
defer rs.mu.Unlock()
// Check if course exists
if _, exists := rs.courses[course.Code]; !exists {
return fmt.Errorf("course %s does not exist", course.Code)
}
// Check if student exists
if _, exists := rs.students[student.ID]; !exists {
return fmt.Errorf("student with ID %d does not exist", student.ID)
}
// Check capacity
if course.GetEnrolledStudents() >= course.MaxCapacity {
return fmt.Errorf("course %s is full", course.Code)
}
// Check if student is already registered
for _, c := range student.RegisteredCourses {
if c.Code == course.Code {
return fmt.Errorf("student is already registered for course %s", course.Code)
}
}
// Create registration
registration := NewRegistration(student, course)
rs.registrations = append(rs.registrations, registration)
// Update course and student
course.IncrementEnrolled()
student.AddCourse(course)
rs.notifyObservers(course)
return nil
}
func (rs *CourseRegistrationSystem) GetRegisteredCourses(student *Student) []*Course {
rs.mu.RLock()
defer rs.mu.RUnlock()
if _, exists := rs.students[student.ID]; !exists {
return nil
}
return student.RegisteredCourses
}
func (rs *CourseRegistrationSystem) notifyObservers(course *Course) {
// TODO: Implement observer pattern for notifications
fmt.Printf("Course %s updated: %d/%d students enrolled\n",
course.Code, course.GetEnrolledStudents(), course.MaxCapacity)
}