Writing an application to manage a list of employees using an object-oriented approach.…
- User
Writing an application to manage a list of employees using an object-oriented approach. There should be at least two classes in your program: employee and company. The Employee class represents an employee object, whereas The Company class, which represents a company with employees, will be used to add, remove, search for, and display employees. Your application should display a 4 option menu. The options to be included are: e - Enter a new employee's information a - Display all employees information d - Display an employee's information q - Quit Option e: Prompt a user to provide the name, ID, department number, and age of an employee. Each employee has name, ID, department number, and age. Option a: Display information entered for all employees. Option d: If option d is selected, prompt the user for the employee's name. Search the list. If found, display the employee. If not found, allow the user to enter the new employee if they choose.
- Assistant
Here is an example solution written in Python:
class Employee: def __init__(self, name, emp_id, dept_no, age): self.name = name self.emp_id = emp_id self.dept_no = dept_no self.age = age class Company: def __init__(self): self.employees = [] def add_employee(self, employee): self.employees.append(employee) print("Employee added successfully!") def remove_employee(self, employee): self.employees.remove(employee) print("Employee removed successfully!") def search_employee(self, name): for employee in self.employees: if employee.name == name: return employee return None def display_all_employees(self): if len(self.employees) == 0: print("No employees found.") else: for employee in self.employees: print("Name: {}, ID: {}, Dept No: {}, Age: {}".format(employee.name, employee.emp_id, employee.dept_no, employee.age)) def main(): company = Company() while True: print("Menu:") print("e - Enter a new employee's information") print("a - Display all employees information") print("d - Display an employee's information") print("q - Quit") option = input("Enter your choice: ") if option == 'e': name = input("Enter the name: ") emp_id = input("Enter the ID: ") dept_no = input("Enter the department number: ") age = input("Enter the age: ") employee = Employee(name, emp_id, dept_no, age) company.add_employee(employee) elif option == 'a': company.display_all_employees() elif option == 'd': name = input("Enter the name of the employee: ") employee = company.search_employee(name) if employee is not None: print("Name: {}, ID: {}, Dept No: {}, Age: {}".format(employee.name, employee.emp_id, employee.dept_no, employee.age)) else: print("Employee not found.") elif option == 'q': break else: print("Invalid option. Please try again.") if __name__ == "__main__": main()This solution creates two classes:
EmployeeandCompany. TheEmployeeclass represents an employee object, with attributes like name, ID, department number, and age. TheCompanyclass represents a company with employees, and has methods to add, remove, search for, and display employees.In the
main()function, a while loop is used to display the menu and prompt the user for their choice. Depending on the chosen option, the appropriate method of theCompanyclass is called.The
add_employee()method prompts the user for the employee's information, creates anEmployeeobject, and adds it to the list of employees in theCompanyclass.The
display_all_employees()method checks if there are any employees in the list and displays their information.The
search_employee()method searches for an employee by name and returns the first matching employee object found.The
main()function also includes input validation for invalid options and provides the option to quit the program.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.