So, I have some student class that calculate grades with 40% from exams and 60%. And need to use some design pattern that allows me to use different calculate to calculate with ratio 45% and 55% without changing student class. And in main I have to print score with using calculate 1, then other calculate 2, and then back to calculate one. I believe it might be either Factory Pattern, or adapter pattern but I do not know how to implement them.
class Student(Calculate):
def __init__(self, studentName):
self.__studentName = studentName
self.__homeworks = []
self.__exams = []
@property
def studentName(self):
return self.__studentName
@property
def homeworks(self):
return self.__homeworks
@property
def exams(self):
return self.__exams
@studentName.setter
def studentName(self, studentName):
self.__studentName = studentName
def addHWScore(self, score):
self.__homeworks.append(score)
def addExamScore(self, score):
self.__exams.append(score)
def calculate(self):
sum1 = 0
sum2 = 0
for x in self.__homeworks:
sum1 = sum1 + x
for y in self.__exams:
sum2 = sum2 + y
avg = ((sum1 / len(self.__homeworks)) * 0.4 + (sum2 / len(self.__exams)) * 0.6)
return 'Student: ' + self.__studentName + ', avarage grade: ' + str(avg)
def main():
student1 = Student('John')
student1.addExamScore(100)
student1.addHWScore(80)
student2 = Student('Alex')
student2.addExamScore(90)
student2.addExamScore(95)
student2.addHWScore(85)
student2.addHWScore(85)
print('calculate 1')
print('===========')
print(student1.calculate())
print(student2.calculate())
print()
print('calculate 2')
print('===========')
???
print()
print('calculate 1')
print('===========')
print(student1.calculate())
print(student2.calculate())
main()
I need to somehow to be able put this calculate on calculate 2, and switch back
def calculate(self):
sum1 = 0
sum2 = 0
for x in self.__homeworks:
sum1 = sum1 + x
for y in self.__exams:
sum2 = sum2 + y
avg = ((sum1 / len(self.__homeworks)) * 0.45 + (sum2 / len(self.__exams)) * 0.55)
return 'Student: ' + self.__studentName + ', avarage grade: ' + str(avg)
Aucun commentaire:
Enregistrer un commentaire