Advertisement
fahimkamal63

Employee Management System

Apr 27th, 2024
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <limits>
  5.  
  6. using namespace std;
  7.  
  8. struct Employee {
  9.     int id;
  10.     string name;
  11.     string jobTitle;
  12.     double salary;
  13. };
  14.  
  15. vector<Employee> employees;
  16.  
  17. void addNewEmployee() {
  18.     Employee newEmployee;
  19.     cout << "Enter employee ID: ";
  20.     cin >> newEmployee.id;
  21.     cin.ignore(numeric_limits<streamsize>::max(), '\n'); // ignore newline character
  22.     cout << "Enter employee name: ";
  23.     getline(cin, newEmployee.name);
  24.     cout << "Enter job title: ";
  25.     getline(cin, newEmployee.jobTitle);
  26.     cout << "Enter salary: ";
  27.     cin >> newEmployee.salary;
  28.     employees.push_back(newEmployee);
  29.     cout << "Employee added successfully!" << endl;
  30. }
  31.  
  32. void showAllEmployees() {
  33.     cout << "Employee List:" << endl;
  34.     for (int i = 0; i < employees.size(); i++) {
  35.         cout << "ID: " << employees[i].id << ", Name: " << employees[i].name << ", Job Title: " << employees[i].jobTitle << ", Salary: " << employees[i].salary << endl;
  36.     }
  37. }
  38.  
  39. double calculatePayroll() {
  40.     double totalPayroll = 0.0;
  41.     for (int i = 0; i < employees.size(); i++) {
  42.         totalPayroll += employees[i].salary;
  43.     }
  44.     return totalPayroll;
  45. }
  46.  
  47. void generatePayslip() {
  48.     int employeeID;
  49.     cout << "Enter employee ID: ";
  50.     cin >> employeeID;
  51.     for (int i = 0; i < employees.size(); i++) {
  52.         if (employees[i].id == employeeID) {
  53.             cout << "Payslip for Employee " << employees[i].name << endl;
  54.             cout << "Job Title: " << employees[i].jobTitle << endl;
  55.             cout << "Gross Salary: " << employees[i].salary << endl;
  56.             cout << "Net Pay: " << employees[i].salary * 0.8 << endl; // assume 20% tax
  57.             return;
  58.         }
  59.     }
  60.     cout << "Employee not found!" << endl;
  61. }
  62.  
  63. void generatePayrollReport() {
  64.     double totalPayroll = calculatePayroll();
  65.     cout << "Payroll Report:" << endl;
  66.     cout << "Total Payroll: " << totalPayroll << endl;
  67.     cout << "Average Salary: " << totalPayroll / employees.size() << endl;
  68. }
  69.  
  70. int main() {
  71.     int choice;
  72.     while (true) {
  73.         cout << "Employee Management System" << endl;
  74.         cout << "1. Add New Employee" << endl;
  75.         cout << "2. Show All Employees" << endl;
  76.         cout << "3. Calculate Payroll" << endl;
  77.         cout << "4. Generate Payslip" << endl;
  78.         cout << "5. Generate Payroll Report" << endl;
  79.         cout << "6. Exit" << endl;
  80.         cout << "Enter your choice: ";
  81.         cin >> choice;
  82.         switch (choice) {
  83.             case 1:
  84.                 addNewEmployee();
  85.                 break;
  86.             case 2:
  87.                 showAllEmployees();
  88.                 break;
  89.             case 3:
  90.                 cout << "Total Payroll: " << calculatePayroll() << endl;
  91.                 break;
  92.             case 4:
  93.                 generatePayslip();
  94.                 break;
  95.             case 5:
  96.                 generatePayrollReport();
  97.                 break;
  98.             case 6:
  99.                 cout << "Exiting program..." << endl;
  100.                 return 0;
  101.             default:
  102.                 cout << "Invalid choice. Please try again." << endl;
  103.         }
  104.     }
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement