forked from Tonguesten36/pp2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.student.mark.oop.math.py
485 lines (416 loc) · 16.6 KB
/
3.student.mark.oop.math.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import math
import json
import subprocess
import numpy as np
class Student:
#init
def __init__(self, student_id, student_name, dob, gpa):
self.__id = student_id
self.__name = student_name
self.__dob = dob
self.__gpa = gpa
#setters (only use when needed)
'''
def set_id(self, student_id):
self.__id = student_id
def set_name(self, student_name):
self.__name = student_name
def set_dob(self, dob):
self.__dob = dob
'''
def set_gpa(self, gpa):
self.__gpa = gpa
#getters
def get_id(self):
return self.__id
def get_name(self):
return self.__name
def get_dob(self):
return self.__dob
def get_gpa(self):
return self.__gpa
class Course:
#init
def __init__(self, course_id, course_name, course_credit, course_mark_mid_portion, course_mark_final_portion):
self.__id = course_id
self.__name = course_name
self.__credit = course_credit
self.__mark_mid_portion = course_mark_mid_portion
self.__mark_final_portion = course_mark_final_portion
#setters (only use when needed)
'''
def set_id(self, course_id):
self.__id = course_id
def set_name(self, course_name):
self.__name = course_name
def set_credit(self, course_credit):
self.__credit = course_credit
def set_mark_mid_portion(self, course_mark_mid_portion):
self.__mark_mid_portion = course_mark_mid_portion
def set_mark_final_portion(self, course_mark_final_portion):
self.__mark_final_portion = course_mark_final_portion
'''
#getters
def get_id(self):
return self.__id
def get_name(self):
return self.__name
def get_credit(self):
return self.__credit
def get_mark_mid_portion(self):
return self.__mark_mid_portion
def get_mark_final_portion(self):
return self.__mark_final_portion
class Mark:
#init
def __init__(self, student_id, course_id, mark_mid, mark_final):
self.__student_id = student_id
self.__course_id = course_id
self.__mark_mid = mark_mid
self.__mark_final = mark_final
#setters (only use when needed)
def set_student_id(self, student_id):
self.__student_id = student_id
def set_course_id(self, course_id):
self.__course_id = course_id
def set_mark_mid(self, mark_mid):
self.__mark_mid = mark_mid
def set_mark_final(self, mark_final):
self.__mark_final = mark_final
#getters
def get_student_id(self):
return self.__student_id
def get_course_id(self):
return self.__course_id
def get_mark_mid(self):
return self.__mark_mid
def get_mark_final(self):
return self.__mark_final
class IOFuncs:
#init
def __init__(self, student_list, course_list, mark_list):
self.__student_list = student_list
self.__course_list = course_list
self.__mark_list = mark_list
#setters (only use when needed)
'''
def set_student_list(self, student_list):
self.__student_list = student_list
def set_course_list(self, course_list):
self.__course_list = course_list
def set_mark_list(self, mark_list):
self.__mark_list = mark_list
'''
#getters
def get_student_list(self):
return self.__student_list
def get_course_list(self):
return self.__course_list
def get_mark_list(self):
return self.__mark_list
#methods
def input_student(self):
student_id = str(input("Enter student id: "))
student_name = str(input("Enter student name: "))
dob = str(input("Enter date of birth: "))
gpa = str("N/A")
student = Student(student_id, student_name, dob, gpa)
self.__student_list.append(student)
def input_course(self):
course_id = str(input("Enter course id: "))
course_name = str(input("Enter course name: "))
course_credit = int(input("Enter course credit: "))
course_mark_mid_portion = float(input("Enter course mark mid portion: "))
course_mark_final_portion = float(input("Enter course mark final portion: "))
course = Course(course_id, course_name, course_credit, course_mark_mid_portion, course_mark_final_portion)
self.__course_list.append(course)
def input_mark(self):
student_id = str(input("Enter student id: "))
for i in self.__student_list:
if student_id == i.get_id():
student_id = i.get_id()
break
course_id = str(input("Enter course id: "))
for i in self.__course_list:
if course_id == i.get_id():
course_id = i.get_id()
break
mark_mid = float(input("Enter mark mid: "))
mark_final = float(input("Enter mark final: "))
mark = Mark(student_id, course_id, mark_mid, mark_final)
self.__mark_list.append(mark)
def output_students_list(self):
for i in self.__student_list:
print("\nID: " + str(i.get_id())
+ "\nName: " + str(i.get_name())
+ "\nDoB: " + str(i.get_dob())
+ "\nGPA: " + str(i.get_gpa())
)
def output_courses_list(self):
for i in self.__course_list:
print("\nID: " + str(i.get_id())
+ "\nName: " + str(i.get_name())
+ "\nDoB: " + str(i.get_credit())
+ "\nMid_%: " + str(i.get_mark_mid_portion())
+ "\nFinal_%: " + str(i.get_mark_final_portion())
)
def output_marks_list(self):
for i in self.__mark_list:
print("\nStudent ID: " + str(i.get_student_id())
+ "\nCourse ID: " + str(i.get_course_id())
+ "\nMid: " + str(i.get_mark_mid())
+ "\nFinal: " + str(i.get_mark_final())
)
def output_student(self, student_id):
for i in self.__student_list:
if i.get_id() == student_id:
print("\nID: " + str(i.get_id())
+ "\nName: " + str(i.get_name())
+ "\nDoB: " + str(i.get_dob())
+ "\nGPA: " + str(i.get_gpa())
)
def output_course(self, course_id):
for i in self.__course_list:
if i.get_id() == course_id:
print("\nID: " + str(i.get_id())
+ "\nName: " + str(i.get_name())
+ "\nDoB: " + str(i.get_credit())
+ "\nMid_%: " + str(i.get_mark_mid_portion())
+ "\nFinal_%: " + str(i.get_mark_final_portion())
)
def output_mark(self, student_id, course_id):
for i in self.__mark_list:
if i.get_course_id() == course_id and i.get_student_id() == student_id:
print("\nCourse ID: " + str(i.get_course_id())
+ "\nStudent ID: " + str(i.get_student_id())
+ "\nMid: " + str(i.get_mark_mid())
+ "\nFinal: " + str(i.get_mark_final())
)
def List2File(self, filename, lst): #write to json file
with open(filename, "w") as file:
json.dump(lst, file, default=lambda o: o.__dict__, indent=4)
def File2List(self, filename): #read from json file
with open(filename, 'r') as file:
data = json.load(file)
return data
class StudentManagement:
#init
def __init__(self):
self.student_list = []
self.course_list = []
self.mark_list = []
self.io_funcs = IOFuncs(self.student_list, self.course_list, self.mark_list)
#methods
def load_data(self):
filename1 = "students_data.dt"
filename2 = "courses_data.dt"
filename3 = "marks_data.dt"
self.student_list.clear()
self.course_list.clear()
self.mark_list.clear()
data1 = self.io_funcs.File2List(filename1)
for i in data1:
student = Student(i["_Student__id"],
i["_Student__name"],
i["_Student__dob"],
i["_Student__gpa"])
self.student_list.append(student)
data2 = self.io_funcs.File2List(filename2)
for i in data2:
course = Course(i["_Course__id"],
i["_Course__name"],
i["_Course__credit"],
i["_Course__mark_mid_portion"],
i["_Course__mark_final_portion"])
self.course_list.append(course)
data3 = self.io_funcs.File2List(filename3)
for i in data3:
mark = Mark(i["_Mark__student_id"],
i["_Mark__course_id"],
i["_Mark__mark_mid"],
i["_Mark__mark_final"])
self.mark_list.append(mark)
def export_data(self):
filename1 = "students_data.dt"
filename2 = "courses_data.dt"
filename3 = "marks_data.dt"
self.io_funcs.List2File(filename1, self.student_list)
self.io_funcs.List2File(filename2, self.course_list)
self.io_funcs.List2File(filename3, self.mark_list)
def add_student(self):
input_no_student = int(input("Enter number of students: "))
for i in range(input_no_student):
self.io_funcs.input_student()
def add_course(self):
input_no_course = int(input("Enter number of courses: "))
for i in range(input_no_course):
self.io_funcs.input_course()
def add_mark(self):
input_no_mark = int(input("Enter number of marks: "))
for i in range(input_no_mark):
self.io_funcs.input_mark()
def output_students_list(self):
self.io_funcs.output_students_list()
def output_courses_list(self):
self.io_funcs.output_courses_list()
def output_marks_list(self):
self.io_funcs.output_marks_list()
def output_student(self):
input_student_id = input("Enter student id: ")
self.io_funcs.output_student(input_student_id)
def output_course(self):
input_course_id = input("Enter course id: ")
self.io_funcs.output_course(input_course_id)
def output_mark(self):
input_student_id = input("Enter student id: ")
input_course_id = input("Enter course id: ")
self.io_funcs.output_mark(input_student_id, input_course_id)
def gpa_calculator(self):
max_point = float(20)
max_credit = 0
for credit in self.course_list:
max_credit += int(credit.get_credit())
for student in self.student_list:
gpa_sum = 0
credits_sum = 0
for course in self.course_list:
for mark in self.mark_list:
if (course.get_id() == mark.get_course_id()
and mark.get_student_id() == student.get_id()):
mark_mid = ((float(mark.get_mark_mid()) / max_point)
* ((float(course.get_mark_mid_portion()) / 100) * max_point))
# floor mark_mid to 1 decimal places
mark_mid = math.floor(mark_mid * 10) / 10
mark_final = ((float(mark.get_mark_final()) / max_point)
* ((float(course.get_mark_final_portion()) / 100) * max_point))
# floor mark_final to 1 decimal places
mark_final = math.floor(mark_final * 10) / 10
mark_full = mark_mid + mark_final
# floor gpa to 1 decimal places
mark_full = math.floor(mark_full * 10) / 10
gpa_sum += mark_full * float(course.get_credit())
credits_sum += int(course.get_credit())
if credits_sum < max_credit:
gpa = "N/A"
else:
gpa = gpa_sum / credits_sum
# floor gpa to 1 decimal places
gpa = math.floor(gpa * 10) / 10
student.set_gpa(gpa)
def gpa_ranking_Low2High(self):
student_dtype = np.dtype([
('id', np.str_, 16),
('name', np.str_, 16),
('dob', np.str_, 10),
('__gpa', np.float32)
])
tmp_gpa = []
for student in self.student_list:
tmp_gpa.append(student)
for i in tmp_gpa:
if i.get_gpa() == "N/A":
i.set_gpa(np.nan)
# Create a list of tuples from the student objects
gpa_list = [(s.get_id(), s.get_name(), s.get_dob(), s.get_gpa()) for s in tmp_gpa]
# Convert the list to a structured numpy array
gpa_arr = np.array(gpa_list, dtype=student_dtype)
# Sort the array by GPA
gpa_arr = np.sort(gpa_arr, order='__gpa')
# Print the sorted array
for s in gpa_arr:
print(s)
def gpa_ranking_High2Low(self):
# sort by numpy
student_dtype = np.dtype([
('id', np.str_, 16),
('name', np.str_, 16),
('dob', np.str_, 10),
('__gpa', np.float32)
])
tmp_gpa = []
for student in self.student_list:
tmp_gpa.append(student)
for i in tmp_gpa:
if i.get_gpa() == "N/A":
i.set_gpa(np.nan)
# Create a list of tuples from the student objects
gpa_list = [(s.get_id(), s.get_name(), s.get_dob(), s.get_gpa()) for s in tmp_gpa]
# Convert the list to a structured numpy array
gpa_arr = np.array(gpa_list, dtype=student_dtype)
# Sort the array by GPA
gpa_arr = np.sort(gpa_arr, order='__gpa')[::-1]
# Print the sorted array
for s in gpa_arr:
print(s)
def main(self):
while True:
print("\n1. Add student")
print("2. Add course")
print("3. Add mark")
print("4. Output students list")
print("5. Output courses list")
print("6. Output marks list")
print("7. Output student")
print("8. Output course")
print("9. Output mark")
print("10. GPA calculator")
print("11. GPA ranking (Low to High)")
print("12. GPA ranking (High to Low)")
print("13. Load data")
print("14. Export data")
print("15. Exit")
input_option = int(input("Enter option: "))
if input_option == 1:
subprocess.call('clear', shell=True)
self.add_student()
elif input_option == 2:
subprocess.call('clear', shell=True)
self.add_course()
elif input_option == 3:
subprocess.call('clear', shell=True)
self.add_mark()
elif input_option == 4:
subprocess.call('clear', shell=True)
self.output_students_list()
elif input_option == 5:
subprocess.call('clear', shell=True)
self.output_courses_list()
elif input_option == 6:
subprocess.call('clear', shell=True)
self.output_marks_list()
elif input_option == 7:
subprocess.call('clear', shell=True)
self.output_student()
elif input_option == 8:
subprocess.call('clear', shell=True)
self.output_course()
elif input_option == 9:
subprocess.call('clear', shell=True)
self.output_mark()
elif input_option == 10:
subprocess.call('clear', shell=True)
self.gpa_calculator()
print("GPA calculator successfully!")
elif input_option == 11:
subprocess.call('clear', shell=True)
self.gpa_ranking_Low2High()
print("GPA ranking (Low to High) successfully!")
elif input_option == 12:
subprocess.call('clear', shell=True)
self.gpa_ranking_High2Low()
print("GPA ranking (High to Low) successfully!")
elif input_option == 13:
subprocess.call('clear', shell=True)
self.load_data()
print("Load data successfully!")
elif input_option == 14:
subprocess.call('clear', shell=True)
self.export_data()
print("Export data successfully!")
elif input_option == 15:
break
else:
print("Invalid option!")
if __name__ == "__main__":
student_management = StudentManagement()
student_management.main()