Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Page 68-69 Exercise 2
- //Date: 6/19/2019
- //Programmed by: Liam Maiorino
- //Description: This program simulates a application that allows you to keep track
- //of employees with various types of information, allows you to calculate the wages
- //of those employees, search between them, and sort between them
- #include <iostream.h>
- #include "apstring.h"
- #include "apvector.h"
- //Declares Employee as a structure with 4 values declared inside to be used when called
- //upon
- struct Employee
- {
- apstring name;
- double hourlyRate;
- int hoursWorked;
- double wages;
- };
- //Declares the functions used in the program
- int CalculateWages(apvector<Employee> &employeeList, int numEmployees);
- void ReadEmployee(Employee &person);
- void DisplayEmployee(const Employee &person, bool isWagesCalculated);
- bool SearchList(const apvector<Employee> &employeeList, apstring &searchName, int&foundLocation, int numEmployees);
- void SortList(apvector<Employee> &employeeList, int numEmployees);
- bool isError(double input);
- int CalculateWages(apvector<Employee> &employeeList, int numEmployees)
- {
- //Declares totalWages as a integer and makes it equal to 0, this is used
- //to keep track of the total wages in the company
- int totalWages=0;
- //Goes through each employee, calculates their wage, assigns that value to their
- //wage value, and adds their value to the totalWages value.
- for (int i=0; i < numEmployees; i++)
- {
- employeeList[i].wages = employeeList[i].hourlyRate * employeeList[i].hoursWorked;
- totalWages = totalWages + employeeList[i].wages;
- }
- //Returns the total wage value to be outputted
- return totalWages;
- }
- bool isError(double input)
- {
- //If the input is not a double then clear and ignore the input, alert the user of the error
- //and return true. A double is used as a integer value is normally a double but a double
- //is not a integer
- if (!cin)
- {
- cin.clear();
- cin.ignore(10000, '\n');
- cout << "ERROR value not accepted, please check input and try again\n";
- return true;
- }
- //Else if the input is less then 0 then output the error and return true
- else if (input < 0)
- {
- cout << "ERROR value can not be negitive, please try again\n";
- return true;
- }
- //Otherwise return false
- else
- return false;
- }
- void ReadEmployee(Employee &person)
- {
- //Request the user to enter the current employees name
- cout << "Please enter the employee's name: ";
- cin >> person.name;
- //While the error function returns true request the user to enter
- //a value for the employees hourly rate
- do
- {
- cout << "Please enter " << person.name << "'s hourly rate: ";
- cin >> person.hourlyRate;
- } while(isError(person.hourlyRate));
- //While the error function returns true request the user to enter
- //a value for the employees hours worked
- do
- {
- cout << "Please enter " << person.name << "'s hours worked: ";
- cin >> person.hoursWorked;
- } while(isError(person.hoursWorked));
- }
- void DisplayEmployee(const Employee &person, bool isWagesCalculated)
- {
- //Output the desired employees information, including their name, hourlyRate, and hours Worked
- cout << "\nDisplaying Information For Employee " << person.name;
- cout << endl << "---------------------------------------------";
- cout << "\nHourly Rate: $" << person.hourlyRate << "/hr";
- cout << "\nHours Worked: " << person.hoursWorked << "hrs";
- //If the wagesCalculated variable is true then output the wage result, otherwise
- //output that the user must calculate the wages first then try again.
- if (isWagesCalculated)
- cout << "\nWage: $" << person.wages;
- else
- cout << "\nWage: Please Calculate Wages And Try Again";
- }
- bool SearchList(const apvector<Employee> &employeeList, apstring &searchName, int&foundLocation, int numEmployees)
- {
- //Declares found to check if the value has been found or not
- bool found=0;
- //Declares scan as a integer, scan is given a starting value of 0 and is
- //used to be increased by 1 everytime we scan the vector for the value we are looking for
- int scan=0;
- //Clears and ignore previous input in the cin
- cin.clear();
- cin.ignore(10000, '\n');
- //Request the user to enter a value for name they are requesting to search for
- cout << "\nWhat value would you like to search for [VALUES ARE CASE SENSITIVE]: ";
- cin >> searchName;
- cout << endl;
- //While the found boolean equal to not true and the scan variable is less then or equal to
- //the numeber of employees (since once we find the value we dont want to keep searching and
- //once we've gone through the size of the board we dont want to keep searching) check if the
- //current element of the vector is equal to the value we are looking for, if so then set the found
- //boolean to true, the foundLocation variable to the current value of the vector that we are on
- //and return true, otherwise add 1 to the scan variable.
- while (not(found) && scan<numEmployees)
- {
- if (employeeList[scan].name==searchName)
- {
- found = true;
- foundLocation = scan;
- return true;
- }
- else
- scan = scan+1;
- }
- //return false as if the loop ends without returning true then we want to return false
- return false;
- }
- void SortList(apvector<Employee> &employeeList, int numEmployees)
- {
- //temp and shuffle variables are declared as integers. these will be used to hold
- //values for comparison
- int temp;
- int shuffle;
- //loops starting at 1 as we assume the first element is already sorted, it then
- //loops through the vector until it reaches the size of the vector. In this loop
- //it makes the temp value equal to the current loop wage value. Then a loop
- //happens going through values of the board that require being swapped
- for (int loop = 1; loop < numEmployees; loop++)
- {
- temp = employeeList[loop].wages;
- for (shuffle = loop; shuffle > 0 && temp < employeeList[shuffle - 1].wages; shuffle--)
- {
- std::swap(employeeList[shuffle], employeeList[shuffle-1]);
- cout << "\n\nList Successfully Sorted.\n";
- }
- }
- }
- int main()
- {
- //Declares company as a vector with the type of the Employee structure
- apvector<Employee> company;
- //Declares choice, numEmployees, and selectedEmployee as integer with starting
- //values of 0
- int choice=0, numEmployees=0, selectedEmployee=0;
- //Declares foundLocation as a integer, this variable is used to hold the element
- //of the vector that the desired employee is located in
- int foundLocation;
- bool isWagesCalculated=0;
- //While the choice value is no 0 (the quit value) request the user to enter
- //a function and then preform the desired task if applicable
- do
- {
- //Output the possible options
- cout << "Gromit Industries Payroll" << endl;
- cout << "-------------------------" << endl;
- cout << "1. Enter new employee." << endl;
- cout << "2. Display employee details." << endl;
- cout << "3. Calculate wages." << endl;
- cout << "4. Sort employees by wages." << endl;
- cout << "5. Search by name." << endl;
- cout << "0. Quit" << endl << endl;
- cout << "Please make a selection: ";
- //Request the user to enter a option value while the error function returns true
- do
- {
- cin >> choice;
- } while (isError(choice));
- switch(choice)
- {
- case 0:
- break;
- case 1:
- //Adds 1 to the numEmployees value, resizes the vector for the new employee
- //calls on the readEmployee function to allow the user to enter values for the current
- //employee, then when finished output the result
- numEmployees++;
- company.resize(numEmployees);
- ReadEmployee(company[numEmployees-1]);
- cout << "New employee added successfully" << endl;
- //sets isWagesCalculated to 0 as a new employee will be added with a uncalculated
- //wage, this is done to prevent outputting failed results
- isWagesCalculated = 0;
- break;
- case 2:
- //If there have been no employees added (aka numEmployees=0) alert the user
- //of this, otherwise call on the displayEmployee function in relation to the current
- //selected employee
- if (numEmployees==0)
- {
- cout << "\nNo employees detected, please try again";
- }
- else
- {
- DisplayEmployee(company[selectedEmployee], isWagesCalculated);
- }
- break;
- case 3:
- //Output the totals wages by calling on the calculateWages function to return the
- //proper value and sets the isWagesCalculated value to true
- cout << "\nTotal Wages is equal to $" << CalculateWages(company, numEmployees);
- isWagesCalculated = 1;
- break;
- case 4:
- //Calls on the sortlist function to sort the employees by their wages
- SortList(company, numEmployees);
- //Outputs the result of the sort
- cout << "List now sorted based on employee wages";
- break;
- case 5:
- //Declares searchName as a apstring to be used as the variable that the user
- //will input for what name they are searching for
- apstring searchName;
- //if the searchList function returns true then output where the desired employee
- //was found and set the selectedEmployee variable to the location of the desired
- //employee in the vector. otherwise output that the desired employee was not found
- //in the vector
- if (SearchList(company, searchName, foundLocation, numEmployees))
- {
- cout << searchName << " was found at element " << foundLocation << ". Employee now selected";
- selectedEmployee = foundLocation;
- }
- else
- {
- cout << searchName << " was not found in the vector";
- }
- break;
- default:
- //Alert the user that the only available options are 1-5
- cout << endl << "Invalid Option. Between 1 and 5 only" << endl;
- }
- //Output lines for spacing
- cout << endl << endl;
- } while (choice!=0);
- //Return 0 to prevent errors
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement