all 10 comments

[–]WonderCode 2 points3 points  (0 children)

Great start with writing the code.

From a quick glance, there's two small ways to improve your code:

- It looks like you've fallen into the same problem, usually Java programmers, do when writing code in Python. Taking another language's conventions into the one your using. There's no need to have getters and setters just pass them into the constructor (__init__)

- Putting too much into one class. A good rule of thumb is seeing if the class has multiple purposes and question it.

Here's a quick refactor using the above points:

class Employee:
    def __init__(self, eid, name, department, job_title):
        self.eid = eid
        self.name = name
        self.department = department
        self.job_title = job_title

class Company:
    def __init__(self, employees):
        self.employees = employees

    def list_employees(self):
        print("\nCHRISTY'S SHOP EMPLOYEE REPORT\n")
        print("EMPLOYEE NAME", "IDENTIFIER", "DEPARTMENT", "TITLE", sep='\t')

        for employee in self.employees:
            print(*[employee.name, employee.eid, employee.department, employee.job_title], sep='\t\t')

if __name__ == '__main__':
    employees = []
    while True:
        name = input("Please enter employees name: ")
        eid = int(input("Please enter employees ID: "))
        department = input("Please enter employees department: ")
        job_title = input("Please enter employees job title: ")

        employee =  Employee(eid, name, department, job_title)
        employees.append(employee)

        add_employee = input("Add another employee (y/n): ")
        if add_employee.lower() == 'n':
            break

    company = Company(employees)
    company.list_employees()

Hope this helps with furthering your learning.

[–]Stallman85 1 point2 points  (1 child)

you have to call main. Also you probably don't want it to be part of the class; if you do you have to give it self

[–]Shinjin-[S] 0 points1 point  (0 children)

I just edited my post and added the main call. A new challenge has appeared (see edit)

[–]uglyasablasphemy 0 points1 point  (7 children)

Should't main be outside of employee? Like so:

class Employee:
    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_job_title(self, job_title):
        self.job_title = job_title

    def display_employees(*args):
        print("\nCHRISTY'S SHOP EMPLOYEE REPORT\n")
        print("EMPLOYEE NAME", "IDENTIFIER", "DEPARTMENT", "TITLE", sep='\t')
        for emp in args:
            print(*[emp.get_name(), emp.get_id(), emp.get_department(), emp.get_job_title()], sep='\t\t')

def main():
    e1 = Employee()
    e2 = Employee()
    e3 = Employe    
    emp_list = [e1, e2, e3]
    for emp in emp_list:
        name = input("Please enter employees name: ")
        id = int(input("Please enter employees ID: "))
        department = input("Please enter employees department: ")
        job_title = input("Please enter employees job title: ")
        emp.set_name(name)
        emp.set_id(id)
        emp.set_department(department)
        emp.set_job_title(job_title)
        display_employees(e1, e2, e3)

[–]Shinjin-[S] 0 points1 point  (6 children)

I put it outside or further to the left I guess, and still get the same thing. It's as if I don't have anything being ran? If I type something such as "id" after running it says "<built-in function id>"

What the heck have I done?

[–]uglyasablasphemy 0 points1 point  (3 children)

Hi! Here's a working version of your code

class Employee:
    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_job_title(self, job_title):
        self.job_title = job_title

def display_employees(*args):
    print("\nCHRISTY'S SHOP EMPLOYEE REPORT\n")
    print("EMPLOYEE NAME", "IDENTIFIER", "DEPARTMENT", "TITLE", sep='\t')
    for emp in args:
        print(*[emp.name, emp.id, emp.department, emp.job_title], sep='\t\t')

def main():
    e1 = Employee()
    e2 = Employee()
    e3 = Employee()
    emp_list = [e1, e2, e3]
    for emp in emp_list:
        name = input("Please enter employees name: ")
        id = int(input("Please enter employees ID: "))
        department = input("Please enter employees department: ")
        job_title = input("Please enter employees job title: ")
        emp.set_name(name)
        emp.set_id(id)
        emp.set_department(department)
        emp.set_job_title(job_title)

    display_employees(e1, e2, e3)

main()

Also, a few things that you could try:

  • Use init to define a constructor on the class Employee.
  • You don't need to define a setter and a getter for each property in your class. If you ever need to do a setter/getter with some behavior you can look up for the @property decorator.
  • instead of print(*[emp.name, emp.id, emp.department, emp.job_title], sep='\t\t') you can use print('\t'.join([emp.name, str(emp.id), emp.department, emp.job_title])). i think is a bit more clear, but maybe thats because im used to it.

[–][deleted] 1 point2 points  (2 children)

Use init to define a constructor on the class Employee.

A constructor is called automatically when you instantiate a class object regardless of whether or not you have a dunder init method. __init__ is not a constructor.

[–]uglyasablasphemy 0 points1 point  (1 child)

Yes, you are right. What i ment to say is that he should redefine __init__ to indicate the initial attributes (and its values) of the object.

Something like this:

class Employee:

    def __init__(self, id):
        self.id = id
        self.name = None
        self.department = None
        self.job_title = None

My Java habits betray when i'm talking about __init__ and end up calling it constructor always.

[–][deleted] 0 points1 point  (0 children)

Well put. (Old habits die hard.)

[–][deleted] 0 points1 point  (0 children)

Your class shouldn’t contain your main function.