Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

{python} Problem: (With the given instructions below, my code is producing multiple errors. Any help would be appreciated) Human Resources has contracted you to write

{python} Problem: (With the given instructions below, my code is producing multiple errors. Any help would be appreciated)

Human Resources has contracted you to write an employee database program for them. Since the company is small, their database will exist in a text file. They would like to use a CSV file so they can easily view the database in other programs, such as Microsoft Excel.

Write a menu-driven program that supports two functions -- the ability to add an employee and the ability to view all employees. When adding a new employee, you should get the employees name, department, and salary. The salary should be inputted as a floating point datatype.

When the program first starts, it should ask for the location of the database. That database file will then be used by all other functions of the program (e.g. adding employee and viewing employees). Your program should consist of 4 functions:

main(): This is the main function of the program. It should prompt the user for a db file location then continously display the menu and respond to the choice appropriately. For example, if the user chooses menu option 1 then the add_employee() function should be called. This process should continue until the user chooses option 3 (i.e. Exit).

menu(): This function should display the menu to the user and ask for a menu choice until a valid one is inputted. Lastly, it should return the choice as an integer.

add_employee(db): This function should input an employee's name, department, and salary from the user. Using that data, it will then open the db file in append mode and write a new row containing the employees data.

view_employees(db): This function should open the db file in read-only mode and then parse the employee data to match the sample execution below.

Sample Execution

File path to database: employee.db 1. Add Employee 2. View Employees 3. Exit Choice: 1 Employee name: Bob Smith Department: Engineering Salary: 52000.50 1. Add Employee 2. View Employees 3. Exit Choice: 4 Choice: 5 Choice: 1 Employee name: Alice Jones Department: Sales Salary: 34535.21 1. Add Employee 2. View Employees 3. Exit Choice: 2 Employee: Bob Smith Department: Engineering Salary: $52000.50 Employee: Alice Jones Department: Sales Salary: $34535.21 1. Add Employee 2. View Employees 3. Exit Choice: 3

My Code: (that produces EoF-errors when checking for main(), view/add_employee, and menu)

#Importing csv module import csv def add_employee(db_file): ''' Method to add employee details to db_file :param db_file: path to the db file :return: None ''' #Opening db file in append mode with open(db_file, "a") as employee_file: #Opening csv writer file employee_details = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) #Asking user for details employee_name = input("Employee name:") employee_dep = input("Department:") employee_salary = float(input("Salary:")) #Writing to the csv file employee_details.writerow([employee_name, employee_dep, employee_salary])

def view_employees(db_file): ''' Method to view employees details :param db_file: path to the db file :return: None ''' # Opening db file in read mode with open(db_file, "r") as employee_file: # Opening csv reader file csv_reader = csv.reader(employee_file, delimiter=',') #For each employee displaying employee details. for row in csv_reader: print("Employee: " + row[0]) print("Department: " + row[1]) print("Salary: $" + row[2])

def menu(): ''' Method to display the menu and ask for user choice. :return: None ''' #Printing menu print("1. Add Employee") print("2. View Employees") print("3. Exit") #Ask the user for choice userChoice = int(input("Choice: ")) #Keepon asking until user enters valid input while (userChoice not in [1, 2, 3]): userChoice = int(input("Choice: ")) return userChoice

def main(): ''' Main method :return: None ''' #Ask user for db file path. db_file = input("File path to database: ") #Get user choice from menu method userChoice = menu() #Trigger respective method based on userchoice. while (userChoice != 3): if userChoice == 1: add_employee(db_file) if userChoice == 2: view_employees(db_file) if userChoice == 3: exit userChoice = menu()

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Differentiate f and find the domain of f. f (x) = ln(x 2 - 2x)

Answered: 1 week ago

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago