views:

62

answers:

4

My professor has given me an assignment on Pay roll system in python.

There's one feature of printing Employees:

 All Employees
 Print by Attributes
      Print by Id
      Print by Name
      Print by Designation
 Print by Salary
      Print less than (<)
      Print greater than (>)
      ... all possible conditions
 Print by Department

Now how do I implement these functions ? I don't need any code, I just want some help in implementing these functions.

I have a main program main.py which in turn will call these functions.

For example If I have a function called ById() in print.py then, should I take user input and display the result in the function itself or should I create a dictionary (kind of array) and store the result in the dictionary and then return it?

But if I return a dictionary there will be iteration of records in two places first for storing result in ById() function and second iteration in main.py for displaying result.

And if I add user input and print result in the function itself then the function becomes less reusable.

What should I do ? Can some one point me in a right direction ?

Edit 1:

Note : We are not done with Object Oriented Programming yet : (

Thanks.

+1  A: 

I think you should go the Object Oriented Route.

There can be a class employe-which will contain all the attributes of the employes.

Also some functions like set_salary(),promote() etc..

There could be a class company at the top level,

Which would contain class department which inturn would contain a list object that contains employee objects for that department.

I think something like this should work for you.

anijhaw
Our professor is going to teach oop in python in the next session, so how should I go with the implementation ? I am sorry I forgot to mention it.
Searock
+1  A: 

Regarding your question about function responsibility... Try to follow the Single Responsibility Principle (SRP). This indicates that you should not have a get_employees_and_print() function but instead do print_employees(get_employees()). This allows you to better reason about the execution of your program, allows better testability, and lets you reuse code because you can print_employee(get_by_name()), print_employee(get_by_id()), and not have to worry about re-implementing the printing bits for each. The print function can just take a list of employee objects and print them in whatever ways are needful.

Daenyth
@Daenyth Thanks a lot
Searock
+1  A: 

Start by thinking about what kind of data structures you want.

What would be a good data structure to represent a single Employee?

What would be a good data structure to represent all the Employees?

Hint: Since you're not using OOP, your primary choices will probably be lists or dicts, or nested combinations thereof.

Once you've settled upon a good data structure, writing the functions to pull information from that data structure will follow naturally.

unutbu
+1  A: 

Write a generic print function that can print an array of dicts.

Then have all your get_emp_by_id(), get_emp_by_name(), get_emp_sal_less_than(), etc, return array of dicts. Just call your custom print function with them.

For example,

get_emp_by_name()

returns,

[{id : 1, name : "mark", desg : "dev"}
 {id : 2, ......}
 ....
]

while get_emp_sal_less_than() could return

 [{id : 1, name : "mark", desg : "dev", sal : "99999"}
  {id : 2, ......}
     ....
 ]

So, your print should be able to interpret the keys of the dict as columns and print likewise.

Sujoy
Thanks for the advice
Searock