Vorrven
Vorrven

Reputation: 53

Calculations in class Student

I have created two classes: Person and Student in different modules. Here is my class Person:

import datetime


class Person:

    def __init__(self, name, surname, patronymic, birthdate):
        self.name = name
        self.surname = surname
        self.patronymic = patronymic
        self.birthdate = birthdate

    def age(self):#this function calculates age of person
        today = datetime.date.today()
        age = today.year - self.birthdate.year

        if today < datetime.date(today.year, self.birthdate.month, self.birthdate.day):
            age -= 1

        return age

Here is my class Student:

from ClassPerson import Person


class Student(Person):
    number_of_group = eval(input("\nInput number of group: "))
    summa = 0
    amount_of_students = 0
    overall_age = 0

    def __init__(self, name, surname, patronymic, birthdate, faculty, group, scholarship):
        Person.__init__(self, name, surname, patronymic, birthdate)
        self.faculty = faculty
        self.group = group
        self.scholarship = scholarship
        if Student.number_of_group == self.group:
            Student.summa += self.scholarship
            Student.overall_age += Student.age(self)
            Student.amount_of_students += 1

    @property
        def scholarship(self):
            return self.__scholarship

    @scholarship.setter
    def scholarship(self, new_s):
        if new_s < 1300:
            self.__scholarship = 1300
        elif new_s > 4000:
            self.__scholarship = 4000
        else:
            self.__scholarship = new_s

I have one simple problem: I need to calculate for specific group overall sum of scholarships and middle age of students of this group. I do calculations in def __init__. But i also had property and setter to change the amount of scholarship due to conditions. So for example we have 3 students:

student1 = Student(
    "Joe",
    "Hapfy",
    "Canes",
    datetime.date(1992, 3, 12),
    "Philosophy faculty",
    441,
    4300
)

student2 = Student(
    "Jane",
    "Mers",
    "Rodrigo",
    datetime.date(1998, 4, 29),
    "Historical faculty",
    441,
    2700
)

student3 = Student(
    "Pavlo",
    "Hornov",
    "Andriyovich",
    datetime.date(1997, 7, 22),
    "Mathematics faculty",
    171,
    1300
)

I want to change student1 scholarship. For example:

student1.scholarship = 1500
print(student1.scholarship)

But the changes are not saved, cause i do calculations in dev __init__. For example, I input number of group as 441.

result = Student.overall_age/Student.amount_of_students
print("Total sum of scholarships: %d" % Student.summa)

The sum of scholarships will be 4300+2700, but due to setter 4300 will be changed to 4000 and sum will be 6700. But now my student1 scholarship is 1500 and i want to receive result 1500+2700=4200. How can i do such calculations after changes of scholarship? Should I use method or something like that instead of calculations in dev __init__?

Upvotes: 0

Views: 342

Answers (1)

Barmar
Barmar

Reputation: 782148

The property setter needs to update Student.summa when necessary.

Since the setter needs to read the old value, we can't use it before we initialize the internal __scholarship attribute. So the __init__() method needs to assign directly to the internal attribute, rather than using the setter with self.scholarship.

from ClassPerson import Person


class Student(Person):
    number_of_group = eval(input("\nInput number of group: "))
    summa = 0
    amount_of_students = 0
    overall_age = 0

    def __init__(self, name, surname, patronymic, birthdate, faculty, group, scholarship):
        Person.__init__(self, name, surname, patronymic, birthdate)
        self.faculty = faculty
        self.group = group
        self.__scholarship = scholarship
        if Student.number_of_group == self.group:
            Student.summa += self.scholarship
            Student.overall_age += Student.age(self)
            Student.amount_of_students += 1

    @property
    def scholarship(self):
        return self.__scholarship

    @scholarship.setter
    def scholarship(self, new_s):
        old_s = self.__scholarship
        if new_s < 1300:
            self.__scholarship = 1300
        elif new_s > 4000:
            self.__scholarship = 4000
        else:
            self.__scholarship = new_s

        # Adjust Student.summa by the change in scholarship
        if self.group == Student.number_of_group:
            Student.summa += self.__scholarship - old_s

Upvotes: 1

Related Questions