Skip to content
Advertisement

Can I improve this code to a more oriented OOP paradigm?

I am improving this code to a student, here is the image: enter image description here

So I rewrite this using list compreenssion and oriented to a formal class, so the second question is: Can I improve yet more this code?

class aluno():
  def __init__(self):
    # cria uma lista de alunos
    self.id = 0
    self.alunos = {}

  def adiciona_aluno(self, name, info=None):
    # cria um novo aluno com o id incremental
    self.alunos[self.id] = {
    'name':name,
    'matricula':self.id,
    'info': info if info else "Sem informação"
    }
    self.id += 1

  def view(self,id):
    #visualiza um aluno no formato especificado
    aluno = self.alunos[id]
    print(f'({aluno["matricula"]}) Nome:{aluno["name"]} - Info:{aluno["info"]}')

  def view_all(self):
    # itera em todos os alunos
    [self.view(aluno) for aluno in self.alunos]

  def main():
    lista_de_alunos = aluno()
    lista_de_alunos.adiciona_aluno("Franz")
    lista_de_alunos.adiciona_aluno("Pedro", "Aluno exemplar em amatematica")
    lista_de_alunos.view_all()

  if __name__ == "__main__":
    main()

Advertisement

Answer

Yes you can improve your code to be more “Object Oriented”.

It doesn’t make sence that Aluno contains a list, Aluno should represent a single student instance.

Below is a sample code. Note that my sample code can be improved, you can, for instances, add a Person class that stores common attributes to Student and Professor class, like birth date, id, etc.

class Aluno:
    _id = 0

    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age
        self.id = Aluno._id
        Aluno._id += 1

    def __cmp__(self, other):
        return self.id == other.id

    def __str__(self):
        return f"- ID: {self.id}; - Name {self.name}; - Age: {self.age};"


class Professor(Aluno):
    def __init__(self, name: str, age: int, wage: float):
        super().__init__(name=name, age=age)
        self.wage = wage


class Turma:
    def __init__(self, year: int, letter: str, director: Professor, alunos: list = None):
        if alunos is None:
            alunos = []

        self.year = year
        self.letter = letter
        self.director = director
        self.alunos = alunos

    def add_aluno(self, aluno):
        # TODO Check if the aluno alerady exists (or work with a set)
        self.alunos.append(aluno)

    def remove_aluno(self, aluno):
        # TODO Check if the aluno can be removed
        self.alunos.remove(aluno)

    def __str__(self):
        _str = f"{self.year}{self.letter}n" 
               f"* Director:n" 
               f"- {self.director}n" 
               f"* Alunos:n"
        for aluno in self.alunos:
            _str += str(aluno)+"n"
        return _str


if __name__ == "__main__":
    aluno1 = Aluno(name="Foo Bar", age=18)
    aluno2 = Aluno(name="Joe", age=16)

    prof = Professor(name="Dr. Phill", age=54, wage=1256)

    turma = Turma(year=10, letter="C", director=prof, alunos=[aluno1])
    turma.add_aluno(aluno2)

    print(f"ALUNO 1: {aluno1}")
    print(f"ALUNO 2: {aluno2}")

    print(f"PROFESSOR: {prof}")

    print(f"TURMA: {turma}")

    turma.remove_aluno(aluno2)
    print(f"TURMA: {turma}")

Outputs:

ALUNO 1: - ID: 0; - Name Foo Bar; - Age: 18;
ALUNO 2: - ID: 1; - Name Joe; - Age: 16;
PROFESSOR: - ID: 2; - Name Dr. Phill; - Age: 54;
TURMA: 10C
* Director:
- - ID: 2; - Name Dr. Phill; - Age: 54;
* Alunos:
- ID: 0; - Name Foo Bar; - Age: 18;
- ID: 1; - Name Joe; - Age: 16;

TURMA: 10C
* Director:
- - ID: 2; - Name Dr. Phill; - Age: 54;
* Alunos:
- ID: 0; - Name Foo Bar; - Age: 18;

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement