-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.py
More file actions
178 lines (151 loc) · 6.31 KB
/
Copy pathStudent.py
File metadata and controls
178 lines (151 loc) · 6.31 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""
File: Student.py
Description: This module defines the Student class, representing a university student who can
enrol in, drop, and complete courses. It tracks the student's personal information and course
enrolment history.
Author: Anush Shirantha De Costa
ID: 110454712
Username: deyay064
This is my own work as defined by the University's Academic Misconduct Policy.
"""
class Student:
"""
Represents a university student who can enrol in and complete courses.
Tracks personal details and course history.
"""
def __init__(self, student_id, first_name, last_name, dob):
"""
Initializes a Student instance with personal details and empty course lists.
:param student_id: Unique identifier for the student (e.g., "S123456")
:param first_name: Student's given name
:param last_name: Student's family name
:param dob: Date of birth as a datetime.date object
"""
self.__student_id = student_id
self.__first_name = first_name
self.__last_name = last_name
self.__dob = dob
self.__enrolled_courses = []
self.__completed_courses = []
def enrol(self, course):
"""
Enrols the student in a course if they are not already enrolled or have not completed it.
:param course: Course object to be added to the enrolled courses list
:return: None, print message
"""
if course in self.__enrolled_courses:
print(f"{self.get_full_name()} is already enrolled in this course.")
return
elif course in self.__completed_courses:
print(f"{self.get_full_name()} has already completed this course.")
return
else:
self.__enrolled_courses.append(course)
course.enrol_student(self)
course_name = course.get_course_name()
term = course.get_term()
print(f"{self.get_full_name()} has successfully enrolled in {course_name} ({term}).")
def drop(self, course):
"""
Removes a course from the student's enrolled courses list if currently enrolled.
Prevents dropping completed or non-enrolled courses.
:param course: Course object to remove from the enrolled list
:return: None, print message
"""
if course in self.__enrolled_courses:
self.__enrolled_courses.remove(course)
course.drop_student(self)
course_name = course.get_course_name()
term = course.get_term()
print(f"{self.get_full_name()} has successfully dropped {course_name} ({term}).")
return
elif course in self.__completed_courses:
course_name = course.get_course_name()
term = course.get_term()
print(f"Cannot drop {course_name} ({term}) "
f"because {self.get_full_name()} has already completed this course.")
return
else:
course_name = course.get_course_name()
term = course.get_term()
print(f"Cannot drop {course_name} ({term}) because {self.get_full_name()} is not enrolled in this course.")
def complete_course(self, course):
"""
Marks a course as completed if the student is currently enrolled in it.
Moves the course from the enrolled list to the completed list.
Note: This method assumes completion has been externally verified (e.g. passed course).
:param course: Course object to be marked as completed
:return: None
"""
if course in self.__enrolled_courses:
self.__enrolled_courses.remove(course)
self.__completed_courses.append(course)
course_name = course.get_course_name()
term = course.get_term()
print(f"{course_name} ({term}) has been marked as completed.")
else:
course_name = course.get_course_name()
term = course.get_term()
print(f"Cannot complete {course_name} ({term}) "
f"because {self.get_full_name()} is not enrolled in this course.")
def list_courses(self):
"""
Displays the student's enrolled and completed courses.
Prints two lists with appropriate headings.
:return: None
"""
print("Enrolled Courses:")
if not self.__enrolled_courses:
print("None")
else:
for course in self.__enrolled_courses:
print(f"- {course.get_course_name()} ({course.get_term()})")
print("\nCompleted Courses:")
if not self.__completed_courses:
print("None")
else:
for course in self.__completed_courses:
print(f"- {course.get_course_name()} ({course.get_term()})")
def completed_program(self):
"""
Placeholder method for checking if the student has completed their program.
:return: False (default for now)
"""
return False
def get_full_name(self):
"""
Returns the student's full name.
:return: First name + Last name as a single string
"""
return f"{self.__first_name} {self.__last_name}"
def get_student_id(self):
"""
Returns the student's unique ID.
:return: Student ID as a string
"""
return self.__student_id
def get_enrolled_courses(self):
"""
Returns a list of courses the student is currently enrolled in.
:return: List of enrolled Course objects
"""
return self.__enrolled_courses
def get_completed_courses(self):
"""
Returns a list of courses the student has completed.
:return: List of completed Course objects
"""
return self.__completed_courses
def __str__(self):
"""
Returns a string summary of the student's ID, full name, and enrolled courses.
:return: Formatted string with student info
"""
full_name = f"{self.get_full_name()} (ID: {self.__student_id})"
courses = self.get_enrolled_courses()
if not courses:
course_block = "Enrolled Courses: None"
else:
lines = [f"- {c.get_course_name()} ({c.get_term()})" for c in courses]
course_block = "Enrolled Courses:\n" + "\n".join(lines)
return f"{full_name}\n{course_block}"