1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#include <iostream> #include <vector> #include <iomanip> using namespace std; // Employee class to store employee details and salary calculation class Employee { public: Employee(const string& name, double hourlyRate, int hoursWorked) : name(name), hourlyRate(hourlyRate), hoursWorked(hoursWorked) {} double calculateSalary() const { return hourlyRate * hoursWorked; } void display() const { cout << "Name: " << name << endl << "Hourly Rate: $" << fixed << setprecision(2) << hourlyRate << endl << "Hours Worked: " << hoursWorked << endl << "Salary: $" << fixed << setprecision(2) << calculateSalary() << endl << "--------------------------" << endl; } private: string name; double hourlyRate; int hoursWorked; }; // Function to add an employee void addEmployee(vector<Employee>& employees) { string name; double hourlyRate; int hoursWorked; cout << "Enter employee name: "; cin.ignore(); getline(cin, name); cout << "Enter hourly rate: $"; cin >> hourlyRate; cout << "Enter hours worked: "; cin >> hoursWorked; employees.emplace_back(name, hourlyRate, hoursWorked); cout << "Employee added successfully!" << endl; } // Function to display all employees' payroll void displayPayroll(const vector<Employee>& employees) { cout << "Employee Payroll:" << endl; for (const auto& employee : employees) { employee.display(); } } int main() { vector<Employee> employees; int choice; do { cout << "Employee Payroll System Menu:" << endl; cout << "1. Add Employee" << endl; cout << "2. Display Payroll" << endl; cout << "3. Exit" << endl; cout << "Enter your choice: "; cin >> choice; switch (choice) { case 1: addEmployee(employees); break; case 2: displayPayroll(employees); break; case 3: cout << "Exiting the system." << endl; break; default: cout << "Invalid choice. Please try again." << endl; } } while (choice != 3); return 0; } |
Explanation:
- Employee Class:
- The
Employee
class stores information about an employee, including their name, hourly rate, and hours worked. - The
calculateSalary
method computes the employee’s salary based on their hourly rate and hours worked. - The
display
method prints the employee’s details and calculated salary.
- The
- Add Employee (
addEmployee
):- This function prompts the user to input an employee’s name, hourly rate, and hours worked.
- It creates a new
Employee
object and adds it to theemployees
vector.
- Display Payroll (
displayPayroll
):- This function iterates through the vector of employees and calls the
display
method for each one, showing their details and calculated salary.
- This function iterates through the vector of employees and calls the
- Main Function (
main
):- The main function provides a simple text-based menu to interact with the payroll system.
- It uses a loop to continuously display the menu and process user choices until the user decides to exit.
- The menu options include adding an employee, displaying payroll information, and exiting the program.
Possible Enhancements:
- Data Persistence: Implement functionality to save employee data to a file and load it when the program starts.
- Input Validation: Add error checking and validation for user inputs to handle incorrect or invalid data.
- Advanced Payroll Calculations: Include support for overtime pay, deductions, and bonuses.
- User Interface: Develop a graphical user interface (GUI) using a library like Qt or SFML for a more user-friendly experience.