Skip to main content

CPT 200: Employee Management System Final Project

import sys
import csv
employeeList = {}
employeeImport = {}
lstMenuOption = ('1', '2', '3', '4', 'Q')
edtMenuOption = ('1', '2', '3', '4', '5', 'Q')

validation = False #initialize validation check to "False"
menuSelection = None
numEmployees = None
num = 1

def cls():
    print('\n'*50)
   
def main_menu():
    totalEmployees = len(employeeList) #Display number of employees in the databse
    print('{:~^79}'.format('~'))
    print('{:~^79}'.format('                              '))
    print('{:~^79}'.format('  Employee Management System  '))
    print('{:~^79}'.format('          James Hardy         '))
    print('{:~^79}'.format('                              '))
    print('{:~^79}'.format('~'))
    print(' ')
    print('{:^79}'.format('There are (%d) employees in the database\n' % totalEmployees))
    print('{:~^79}'.format('~'))
    print(' ')
    print('(1) Add new employee(s)')
    print('(2) View all employees in the database')
    print("(3) Search for employee by SSN")
    print('(Q) Exit\n')
    print('{:~^79}'.format('~'))
    print(' ')
    option = input('Please select an option from the menu: ')
    return option

def edit_menu():
    totalEmployees = len(employeeList) #Display number of employees in the databse
    print('There are (%d) employees in the database\n' % totalEmployees)
    print('{:~^79}'.format('Choose which field to edit'))
    print('(1) Name')
    print('(2) SSN')
    print('(3) Phone Number')
    print("(4) Email")
    print("(5) Salary")
    print('(Q) Exit\n')
    print('{:~^79}'.format('~'))
    option = input('Please select an option from the menu: ')
    return option

def search_by_ssn():
    search = input('Please enter the SSN you want to find: ')
    for empName in employeeList.keys():
        record = employeeList[empName]
        if record[2] == search:
            view_employee(empName)
            global empEdit
            empEdit = empName
            return record

        continue
   
def edit_info(editSelect):
    if editSelect.upper() in edtMenuOption:
        while editSelect.isnumeric() == True:
            if editSelect == '1':
                newName = edit_name()
                return newName
            elif editSelect == '2':
                pass
            elif editSelect == '3':
                pass
            elif editSelect == '4':
                pass
            elif editSelect == '5':
                pass
            elif editSelect.upper() == 'Q':
                break
            elif editSelect.upper() != 'Q':
                print('Invalid option, please try again')
                editSelect = edit_menu()
            break
    else:
        print('Invalid option, please try again')
        editSelect = edit_menu()

def edit_question():
    yesno = input('Would you like to edit the employee record (y/n): ')
    if yesno.lower() == 'y':
        editSelect = edit_menu()
        edit_info(editSelect)
        view_all_employees()
    elif yesno.lower() == 'n':
        pass
    elif (yesno.lower() != ('y' or 'n')) or (yesno.isnumeric() == True):
        print('You have entered an invalid option, please try again')
        edit_question()
   
def edit_name():
    revFName = input("Please enter the employee's first name: ")
    revLName = input("Please enter the employee's last name: ")
    revFullName = revFName + ' ' + revLName
    record[0] = revFName
    record[1] = revLName
    employeeList[revFullName.capitalize()] = employeeList.pop(empEdit)
    return revFullName.capitalize()
       
def valid(option): #Function to validate menu selection
    if (option.isnumeric() == True):
        if option in lstMenuOption:
            return True
    elif (option.upper() == 'Q'):
        global menuSelection
        menuSelection = option.upper()
        return True
    elif (option.isnumeric() == False):
        print('You have not entered a correct option.')
        return False
   
def num_employees():
    numEmployees = (input('Please enter the number of employee(s) you are entering (Q to Quit): '))
    if (numEmployees.isnumeric() == True) and (int(numEmployees) < 0):
        print('You have entered an invalid quantity\n')
    elif (numEmployees.isnumeric() == False):
        if (numEmployees.upper() == 'Q'):
            main_menu()
            print(' ')
        else:
            print('You have entered an invalid quantity')
            return None
    else:
        numEmployees = int(numEmployees)
        return numEmployees
   

def add_employee(): #CPT 200: Functionality 1 part 1
        employeeFName = input("Please Emter the Employee #%d's First Name:" % num)
        employeeLName = input("Please Enter the Employee #%d's Last Name:" % num)
        employeeSSN = input("Please Enter %s %s's SSN (123456789):" % (employeeFName, employeeLName))
        employeePhone = input("Please Enter %s %s's Phone Number:" % (employeeFName, employeeLName))
        employeeEmail = input("Please Enter %s %s's Email Address:" % (employeeFName, employeeLName))
        employeeSalary = input("Please Enter %s %s's Salary:" % (employeeFName, employeeLName))
        employeeFull = employeeFName + ' ' + employeeLName
        employeeInfo = [employeeFName.capitalize(), employeeLName.capitalize(), employeeSSN, employeePhone, employeeEmail, employeeSalary]
       
        if (employeeFull and employeeSSN) not in employeeList.keys():
            with open('Employee_database.csv', 'a+') as employeeCSV:
                employee_writer = csv.writer(employeeCSV)
                employee_writer.writerow(employeeInfo)
        else:
            print('Employee is already in the database, please try again.')
       
        return employeeFull.upper(), employeeInfo

def view_all_employees(): #Function to display all the keys of employeeList, Functionality 1 part 2
    print('{:~^79}'.format('~'))
    print(' ')
    print('The following employees are currently in the database\n')
    for key in employeeList.keys():
        record = employeeList[key]
        print('{:~^79}'.format(' %s ' % key))
        print('SSN: %s' % record[2])
        print('Phone: %s' % record[3])
        print('Email: %s' % record[4])
        print('Salary: $%s' % record[5])
        print('{:~^79}'.format('~'))

def view_employee(empName): #Funtionality 1 part 2.5
    record = employeeList[empName]
    print('{:-^79}'.format(' %s ' % empName))
    print('SSN: %s' % record[2])
    print('Phone: %s' % record[3])
    print('Email: %s' % record[4])
    print('Salary: $%s' % record[5])
    print('{:~^79}'.format('-'))

def import_employees():
    with open('Employee_database.csv', 'r') as csvfile: #Opens the student answer file, and closes once finished
        employee_reader = csv.reader(csvfile)
   
        first_row = True
        for row in employee_reader:
            #Skip the first row with column names
            if first_row:
                first_row = False
                continue
            try:
                fullName = row[0] + ' ' + row[1] #Creates full name for dictionary key
                employeeList[fullName] = row
            except IndexError:
                pass
               
    print('The following employees were imported:\n')
   
    for employee in employeeList.keys():
        print(employee)
    print(' ')
    input('<Press Enter to Continue>')

try:
    import_employees()
except:
    with open('Employee_database.csv', 'w') as employeeCSV:
                employee_writer = csv.writer(employeeCSV)
                csvrow1 = ['FirstName','LastName','SSN','Phone','Email','Salary']
                employee_writer.writerow(csvrow1)
       
try:
    while menuSelection == None:
        menuSelection = main_menu()
        cls()

        if menuSelection.upper() in lstMenuOption: #Menu option selection
            while (menuSelection.isnumeric() == True):
                if menuSelection == '1':
                    while numEmployees == None or numEmployees == 0:
                        numEmployees = num_employees()
               
                    for employee in range(0, numEmployees, 1):
                        employeeFull, employeeInfo = add_employee()
                        employeeList[employeeFull] = employeeInfo
                        numEmployees = numEmployees - 1
                        num = num + 1
                        if (numEmployees > 0):
                            print('Prepare to enter employee #%d' % num)
                    menuSelection = None #Reset check values to default
                    cls()
                    break
                if menuSelection == '2':
                    view_all_employees()
                    input('<Press Enter to Continue>')
                    menuSelection = None #Reset check values to default
                    break
                if menuSelection == '3':
                    record = search_by_ssn()
                    edit_question()
                    menuSelection = None
                    break
                if menuSelection == '4':
                    pass
                    menuSelection = None
                break
            else: #(menuSelection.isnumeric() == False):
                if menuSelection == 'Q':
                    exit()
        else:
            print('You have not selected a valid option. Please try again.')
            menuSelection = None
except ValueError:
    print('You have not selected a valid option. Please try again.')

Comments

Popular posts from this blog

CPT 200: Fundamentals of Programming Languages

    During my quest to obtain a Bachelor of Information Technology from Ashford University, my fourth class was CPT 200: Fundamentals of Programming Languages.  For that class, the programming language that is taught is Python 3.     On the first week of class, we were asked to create code that would ask a user to input several pieces of information about any specific employee.  We were to use the variables: employeeName, employeeSSN, employeePhone, employeeEmail, and employeeSalary.  After the data was inputted, it needed to be printed on the screen.  Below was what I turned in for Functionality 1:     During the second week of class, we were to read two chapters: Chapter 3: Types and Chapter 4: Branching.  These chapters introduced us to the different types of variables that can be used within Python as well as how to use branching in your scripts. For the second functionality, we were instructed to adjust ou...

CPT 307: Starting to understand algorithm selection

As it turns out, there is a specific science to selecting the best algorithm to apply to data within a program.  When I first started my Data Structures & Algorithms class, I was excited to learn about different algorithms, and how to efficiently store and sort data using advanced data structures.  What I learned was that there are a great many different algorithms to both search and sort information stored in arrays.  Some websites, such as geeksforgeeks.org, have entire lists of different algorithms each with differing complexities, and each tailored for a specific use.  The computer science community describes algorithm efficiency using two different measures of complexity.  Time complexity is a function relating the number of actions (n) that will be performed on an array (a[]).  There are many different kinds of actions that an algorithm can perform on an array of data.  Time can mean the number of memory accesses performed, the number of co...

CPT 307: Java Newbie to Newbie

     For our first assignment in CPT 307: Data Structures & Algorithms, we were tasked with installing the Java Development Kit (JDK) and the NetBeans IDE.  Installing the JDK was straightforward and painless.  It was as simple as downloading the installer and following the installation wizard.  NetBeans was a slightly different story.  There were several different packages to download; I chose the package with the most language support.  In hindsight, I probably should have downloaded only the package supporting Java, saving the other packages for when I actually use the other tools.  After completing the NetBeans install, I kept getting an error about “GlassFish” whenever I tried creating a new project.  I attempted to search the forums for a fix but found the NetBeans forums to be extremely confusing, and I could not find a solution to the issue.  So I decided to search the internet for a different IDE to work with.  Wh...