[Python] Employee Classes - Python

Discussão em 'Python' iniciado por Stack, Setembro 3, 2025.

  1. Stack

    Stack Membro Participativo

    Write a class named Employee that holds the following data about an employee in attributes: name, ID number, department, and job title.

    Once you have written the class, write a program that creates three Employee objects to hold the following data:

    Susan Meyers - 47899 - Accounting - Vice President Mark Jones - 39119 - IT - Programmer Joy Rogers - 81774 - Manufacturing - Engineer

    ^ This is the problem I'm stuck on. I believe I have the first part of the code correct, but I don't understand what to do for the second part of the program to create Employee objects.

    Here is my code so far...

    emp.py file

    #create a class named Employee
    class Employee:

    #initialize the attributes
    def __init__(self, name, id, department, title):
    self.__name = name
    self.__id = id
    self.__department = department
    self.__title = title

    #set the attributes
    def set_name(self, name):
    self.__name = name

    def set_id(self, id):
    self.__id = id

    def set_department(self, department):
    self.__department = department

    def set_title(self, title):
    self.__title = title

    #return the attributes
    def get_name(self):
    return self.__name

    def get_id(self):
    return self.__id

    def get_department(self):
    return self.__department

    def get_title(self):
    return self.__title

    #return the objects state as a string

    def __str__(self):
    return 'Name: ' + self.__name + \
    '\nID number: ' + self.__id + \
    '\nDepartment: ' + self.__department + \
    '\nTitle: ' + self.__title


    and here is my main function where the second part is going to go...

    import emp


    def main():
    #Create three employee objects
    emp1 = emp.Employee('name', 'id', 'department', 'title')
    emp2 = emp.Employee('name', 'id', 'department', 'title')
    emp3 = emp.Employee('name', 'id', 'department', 'title')

    #create three Employee objects for each attribute
    emp1.set_name('Susan Meyers')
    emp1.set_id('47899')
    emp1.set_department('Accounting')
    emp1.set_title('Vice President')

    emp2.set_name('Mark Jones')
    emp2.set_id('39119')
    emp2.set_department('IT')
    emp2.set_title('Programmer')

    emp3.set_name('Joy Rogersr')
    emp3.set_id('81774')
    emp3.set_department('Manufacturing')
    emp3.set_title('Engineer')

    print()
    print(emp1)
    print()
    print(emp2)
    print()
    print(emp3)


    main()


    So with this code, I am now getting the correct answer of this:

    Employee 1:
    Name: Susan Meyers
    ID number: 47899
    Department: Accounting
    Title: Vice President

    Employee 2:
    Name: Mark Jones
    ID number: 39119
    Department: IT
    Title: Programmer

    Employee 3:
    Name: Joy Rogers
    ID number: 81774
    Title: Programmer

    Process finished with exit code 0

    Continue reading...

Compartilhe esta Página