Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python Programming------ You will be creating a menu-driven application that mai

ID: 3808486 • Letter: P

Question

Python Programming------

You will be creating a menu-driven application that maintains a list of employees.

Create 6 functions and a main program that calls them:

Step 1: Use your FUNCTION called createMenu to allow the main program to show the menu for Employee Maintenance. Reminder – the createMenu function accepts TWO parameters: menuHeading (a string) and optionList (a list of strings) and should NOT PRINT ANYTHING – it simply RETURNS a string.

Step 2: Create FUNCTION “stubs” for each of the four menu items – the stubs should just print a message about what the function will eventually do.   Most of these functions will accept one parameter (the list). The addEmployee function is already done for you, as shown below:

def addEmployee(empList ):
        “””Description: this function will ask the user for employee information (last name)
              and place that name into the empList.
             Precondition: empList is a list of strings
        “””
        print(“ Not implemented yet – will eventually add an employee to the list ”)

Create an addEmployee function – just include one print statement in the function for now

Create a printEmployeeList function – just include one print statement in the function for now

Create a deleteEmployee function – just include one print statement in the function for now

Create a sortEmployeeList function – just include one print statement in the function for now

Step 3: Create the “main” program area, where the application continues to print the menu and
calls your getValidChoice function to obtain a choice from the user.

Based on the user’s choice, call the appropriate function.

Step 4: When the user selects 5 (exit), print a message thanking them for using the application and ask them to press enter to quit.

Employee Maintenance 1) Add a New Employee 2) Print Employee Roster 3) Delete an Employee 4) Sort List by Last Name 5) Exit. Please choose an item from the menu by typing its number

Explanation / Answer


def getValidChoice():
   input = 0;
   while(True):
       print "1. Add new Employee"
       print "2. Print Employee Roster"
       print "3. Delete a Employee"
       print "4. Sort the list by Last name"
       print "5. Quit"
       input = int(raw_input("Enter Your choice:"))
       if(input>0 and input <6):
           break
       else:
           print "Invalid Input"
           continue
   return input
      
class Employee:
   def __init__(self, lname):
       self.lastname = lname
      
   def __lt__(self, other):
return self.lastname < other.lastname
         
   def __repr__(self):
       return self.lastname
         
   def getLastName(self):
       return self.lastname
      
def addEmployee(list):
   lname = raw_input("Enter Last Name:")
   emp = Employee(lname)
   list.append(emp)

def printEmployeeList(list):
   for emp in list:
       print emp
      
def deleteEmployee(list):
   lname = raw_input("Enter Last Name:")
   emptemp = Employee("")
   for emp in list:
       if(emp.getLastName() == lname):
           emptemp = emp
           break;
   list.remove(emptemp)
  
def sortEmployeeList(list):
   list.sort();
   for emp in list:
       print emp
  
def main():
   list = []
   quit = False
   while(quit != True):
       choice = getValidChoice()
       if(choice == 1):
           addEmployee(list)
          
       if(choice == 2):
           printEmployeeList(list)
      
       if(choice == 3):
           deleteEmployee(list)
          
       if(choice == 4):
           sortEmployeeList(list)
          
       if(choice == 5):
           quit = True
           break
      
main()

#endOfFile