-
Notifications
You must be signed in to change notification settings - Fork 246
/
2.student.mark.oop.py
267 lines (221 loc) · 8.47 KB
/
2.student.mark.oop.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# Objective: OOP-fy 1.student.mark.py
# Students, Courses, StudentMarks should be an object
students_list = []
courses_list = []
marks_list = []
class Object:
def __init__(self, name, id):
self._name = name
self._id = id
# Append the object to the list
def _insert_to_list(self, list):
list.append(self)
return list
# Get the name of the obj
def _get_name(self):
return self._name
# Get the id of the obj
def _get_id(self):
return self._id
# String representation of obj
def __str__(self):
return f"Name: {self._name}, ID: {self._id}"
class Student(Object):
def __init__(self, name, id, dob):
super().__init__(name, id)
self.__student_dob = dob
# Get the d.o.b of the student
def _get_dob(self):
return self.__student_dob
# Compare between two students and return true if specific attributes are identical
def __lt__(self, other_student):
return self._id == other_student._id
# String representation of Student
def __str__(self):
return f"Name: {self._name}, ID: {self._id}, D.O.B: {self.__student_dob}"
class Course(Object):
def __init__(self, name, id):
super().__init__(name, id)
def __str__(self):
return super().__str__()
def __lt__(self, other_course):
return (self._name == other_course._name) or (self._id == other_course._id)
class StudentMark:
def __init__(self, student_id, course_name, mark):
self.__student_id = student_id
self.__course_name = course_name
self.__student_mark = mark
def _get_course_name(self):
return self.__course_name
def _get_student_id(self):
return self.__student_id
def _get_student_mark(self):
return self.__student_mark
def __str__(self):
return f"Student ID: {self.__student_id}, Course: {self.__course_name}, Mark: {self.__student_mark}"
# Input a number of students in an class
def input_student(students_list):
# Ask the user how many students would they like to add
while True:
try:
no_students = int(input("Input number of students to be added in the class: "))
if no_students <= 0:
print("There must be at least one student")
else:
break
except:
print("Must be an integer!")
# Inputting neccessary info for students
for s in range(0, no_students, 1):
while True:
try:
print(f"---Student {s + 1}. ")
student_id = int(input("Student ID: "))
student_name = str(input("Name: "))
student_dob = str(input("D.O.B (DD/MM/YYYY): "))
if student_id <= 0 or student_name == "" or student_dob == "":
print("try again")
else:
# Check if the student had already existed yet
for student_obj in students_list:
if student_obj.get_id() == student_id:
print("Student already exist!")
return
# Create a new Student object and append it to the students_list
new_student = Student(student_name, student_id, student_dob)
students_list.append(new_student)
break
except:
print("Something went wrong, try again.")
# List out all students in the class
def list_student(students_list):
print("---List of students in the class---")
counter = 1
for student_obj in students_list:
print(f"Student {counter}. Name: {student_obj.get_name()}, ID: {student_obj.get_id()}, D.O.B: {student_obj.get_dob()}")
counter = counter + 1
# Input a number of courses
def input_course(course_list):
# Ask the user how many courses they would like to add
while True:
try:
no_courses = int(input("Input number of courses to be added: "))
if no_courses <= 0:
print("There must be at least one course")
else:
break
except:
print("Must be an integer!")
# Inputting neccessary info for the courses
for c in range(0, no_courses, 1):
while True:
try:
print(f"---Course {c + 1}. ")
course_name = str(input("Course Name: "))
course_id = int(input("Course ID: "))
if course_id <= 0 or course_name == "":
print("try again")
else:
# Check if the course exist yet
for course_obj in course_list:
if course_obj.get_name() == course_name or course_obj.get_id() == course_id:
print("Course already exist!")
return
new_course = Course(course_name, course_id)
course_list.append(new_course)
break
except:
print("Something went wrong, try again.")
# List out available courses
def list_course(courses_list):
counter = 1
print("---List of courses available---")
for course_obj in courses_list:
print(f"Course {counter}. {course_obj.get_name()}, ID: {course_obj.get_id()}")
counter = counter + 1
# Select a course, input marks for a student in that courses
def input_mark(students_list, courses_list, marks_list):
student_id = int(input("Student's ID: "))
student_exist = False
course_exist = False
# Check if the student exist
if len(students_list) == 0:
print("Student does not exist!")
return
else:
for student_obj in students_list:
if student_obj.get_id() == student_id:
student_exist = True
course_name = input("Course's name: ")
# Check if the course exist
if len(courses_list) == 0:
print("Course does not exist!")
return
else:
for course_obj in courses_list:
if course_obj.get_name() == course_name:
course_exist = True
# If student or course does not exist, then the function stops
if student_exist == False:
print("Student does not exist!")
return
if course_exist == False:
print("Course does not exist!")
return
# Ask the user the student's mark for that course
student_mark = 0
while True:
try:
student_mark = float(input("Input mark here: "))
if student_mark < 0:
print("Mark must be non-negative")
else:
break
except:
print("Must be a number!")
# The student mark will be an object
student_mark_obj = StudentMark(student_id, course_name, student_mark)
# Add the mark's info into the list
marks_list.append(student_mark_obj)
# List out all mark of a student
def list_mark(marks_list):
try:
search_target = int(input("Enter the student ID: "))
for mark_obj in marks_list:
if mark_obj.get_student_id() == search_target:
print(f"Course: {mark_obj.get_course_name()}, Mark: {mark_obj.get_student_mark()}")
except:
print("Something went wrong")
# Execute the code
# This code will loop until terminated
if __name__ == "__main__":
print("------Student Mark management system------")
while True:
print("---Here are the things that you could do---")
print("1. Add new student(s)")
print("2. Add new course(s)")
print("3. Select a course and input mark for student")
print("4. List out all students")
print("5. List out all courses")
print("6. List out all students' mark for each courses")
print("7. Exit")
op = input("What do you want to do? (1-7): ")
# Use this code if you're using a Python version 3.10 or later
match op:
case "1":
input_student(students_list)
case "2":
input_course(courses_list)
case "3":
input_mark(students_list, courses_list, marks_list)
case "4":
list_student(students_list)
case "5":
list_course(courses_list)
case "6":
list_mark(marks_list)
case "7":
exit()
case _:
print("Invalid input")
print("\n")