markyrocks

hw

May 19th, 2021
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.38 KB | None | 0 0
  1. //description : babbages cabbages is a program to calculate the gross pay, taxes payed and net pay for an employee given their name, hours worked and hourly pay rate.
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <iostream>
  5. #include<string>
  6. #include<iomanip>
  7. #define TAX_RATE .28 // constant to hold current tax rate
  8. #define MAX_REG_HOURS 40.00
  9. #define MIN_HOURS 20.00
  10. #define MAX_HOURS 60.00
  11. #define MIN_HOURLY_RATE 7.25
  12. #define MAX_HOURLY_RATE 44.99
  13. #define SIZE 3
  14.  
  15.  
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. void getEmployeeInfo(std::string& name, double& hours, double& hourlyRate);
  18. double getDeductions();
  19. void getTypeOfHours(double hours, double& regHours, double& otHours);
  20. double calculateGrossPay(double regHours, double otHours, double hourlyRate);
  21. double getSumOfGrossPay(double grossPays[],int num);
  22. double getAveOfGrossPay(double sum,int num);
  23. double calculateTaxes(double gross, double deduction);
  24. double calculateNetPay(double grossPay, double taxes);
  25. //void displayResults(std::ofstream& outfile, std::string name, double regHours,
  26. //    double otHours, double hourlyRate, double grossPay, double netPay, double taxes, double deduction);
  27. //void displaySummery(std::ofstream& outfile, double grossPays[], std::string fullNames[], double sum, double ave, double minGrossPay);
  28. bool getYesOrNo();
  29. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  30.  
  31. // main module to call subsequent modules
  32. int main()
  33. {
  34.    
  35.     int sum = 0,NumOfEmployees=0;
  36.     double regHours{}, otHours{}, deduction{};
  37.     double hours, ave = 0.0;
  38.     double hourlyRate = 0.0;
  39.     double grossPay = 0.0, minGrossPay{}, maxGrossPay{};
  40.     double netPay[SIZE];
  41.     double taxes[SIZE];
  42.     std::string fullNames[SIZE];
  43.     double grossPays[SIZE];
  44.     std::ofstream outfile;
  45.  
  46.     //outfile.open("/Users/tricia/Desktop/temp.txt/payroll.txt ", std::ios::out);
  47.     //if (!outfile.is_open()) {
  48.     //    std::cout << "Unable to open a file!" << std::endl;
  49.     //}
  50.     //else {
  51.     //    std::cout << " success ! " << std::endl;
  52.     //}
  53.  
  54.     for (auto i = 0; i < SIZE; i++)
  55.     {
  56.         getEmployeeInfo(fullNames[i], hours, hourlyRate);
  57.         deduction = getDeductions();
  58.         getTypeOfHours(hours, regHours, otHours);
  59.         grossPays[i] = calculateGrossPay(regHours, otHours, hourlyRate);
  60.         taxes[i] = calculateTaxes(grossPays[i],deduction);
  61.         netPay[i] = calculateNetPay(grossPays[i], taxes[i]);
  62.         NumOfEmployees++;
  63.         if (!getYesOrNo()) { break; }
  64.     }
  65.  
  66.         sum = getSumOfGrossPay(grossPays,NumOfEmployees);
  67.         ave = getAveOfGrossPay(sum,NumOfEmployees);
  68.         minGrossPay = *std::min(std::begin(grossPays), std::begin(grossPays)+NumOfEmployees-1);
  69.         maxGrossPay = *std::max(std::begin(grossPays), std::begin(grossPays) + NumOfEmployees-1);
  70.  
  71.     //    std::cout << minGrossPay << " min\n max " << maxGrossPay;  //this was for testing
  72.  
  73.     //std::cout << "display\n";
  74.     //for (auto i = 0; i < SIZE; i++)
  75.     //    std::cout << grossPays[i] << '\n';
  76.  
  77.     //displayResults(outfile, name, regHours, otHours, hourlyRate, grossPay, netPay, taxes, deduction);
  78.     //displaySummery(outfile, grossPays, fullNames, sum, ave, minGrossPay);
  79.  
  80.  
  81.  
  82.     //std::cout << "============================" << std::endl;
  83.     //std::cout << "  Employee Payroll Summery " << std::endl;
  84.     //std::cout << "============================" << std::endl;
  85.     //std::cout << " Name                  Gross pay" << std::endl;
  86.     //std::cout << "====================  ===========" << std::endl;
  87.     //for (int i = 0; i < SIZE; i++)
  88.     //{
  89.     //    std::cout << std::left << std::setw(20) << fullNames[i] << "   " << std::setw(11) << grossPays[i] << std::endl;
  90.  
  91.  
  92.     //}
  93.     //std::cout << "Total Gross Pay: " << sum << std::endl;
  94.     //std::cout << "Average Gross Pay: " << ave << std::endl;
  95.     //std::cout << "Minimum Gross Pay: " << getMinGrossPay << std::endl;
  96.     //std::cout << "Maximum Gross Pay: " << getMaxGrossPay(grossPays) << std::endl;
  97.  
  98.  
  99.     //outfile.close();
  100. }
  101.  
  102. void getEmployeeInfo(std::string& name, double& hours, double& hourlyRate)
  103. {
  104.     {
  105.         std::string firstName{};
  106.         std::string lastName{};
  107.  
  108.         while (1) {
  109.             char c{};
  110.             std::cout << " please enter employee's first name : " << std::endl;
  111.             std::cin >> firstName;
  112.             std::cout << " please enter employee's last name : " << std::endl;
  113.             std::cin >> lastName;
  114.             name = lastName + "," + firstName;
  115.             std::cout << "is this correct? " << name << " y or n?\n";
  116.             std::cin >> c;
  117.             if (c == 'y') { break; }
  118.         }
  119.  
  120.         std::cout << "enter employee's hours worked : " << std::endl;
  121.         std::cin >> hours;
  122.         while ((hours < MIN_HOURS) || (hours > MAX_HOURS))
  123.         {
  124.             std::cout << "The number of hours worked must be between 20 and 60 hours. " << std::endl;
  125.             std::cin >> hours;
  126.         }
  127.  
  128.         std::cout << "enter emplyee's current  pay rate in dollars per hour : " << std::endl;
  129.         std::cin >> hourlyRate;
  130.         while ((hourlyRate < MIN_HOURLY_RATE) || (hourlyRate > MAX_HOURLY_RATE))
  131.         {
  132.             std::cout << " The hourly pay rate must be between 7.25 and 44.99. " << std::endl;
  133.             std::cin >> hourlyRate;
  134.             break;
  135.         }
  136.  
  137.     }
  138. }
  139.  
  140. double getDeductions()
  141. {
  142.     char garage{}, transit{}, bikeLocker{};
  143.    
  144.     std::cout << " Does employee use parking garage (y/n)? ";
  145.     std::cin >> garage;  
  146.     while ((garage != 'n') && (garage != 'N') && (garage != 'y') && (garage != 'Y'))
  147.     {
  148.         std::cout << "invalid response. please enter Y for yes or N for no :" << std::endl;
  149.         std::cin >> garage;
  150.     }
  151.     if ((garage == 'y') || (garage == 'Y'))
  152.     {
  153.         return 7.50;
  154.     }
  155.     else if ((garage == 'n') || (garage == 'N'))
  156.     {
  157.         std::cout << "Does the employee participate in the transit program (Y/N)?";
  158.         std::cin >> transit;  while ((transit != 'n') && (transit != 'N') && (transit != 'y') && (transit != 'Y'))
  159.         {
  160.             std::cout << "invalid response. please enter Y for yes or N for no :" << std::endl;
  161.             std::cin >> transit;
  162.         }
  163.  
  164.          if ((transit == 'y') || (transit == 'Y'))
  165.             {
  166.                 return 5.00;
  167.           }
  168.           else
  169.             {
  170.                 std::cout << "Does the employee rent a bike locker (Y/N)? ";
  171.                 std::cin >> bikeLocker; while ((bikeLocker != 'n') && (bikeLocker != 'N') && (bikeLocker != 'y') && (bikeLocker != 'Y'))
  172.                 {
  173.                     std::cout << "invalid response. please enter Y for yes or N for no :" << std::endl;
  174.                     std::cin >> bikeLocker;
  175.                 }
  176.                  if ((bikeLocker == 'y') || (bikeLocker == 'Y'))
  177.                     {
  178.                         return 1.00;
  179.                     }
  180.                  else { return 0.0; }
  181.  
  182.             }
  183.  
  184.     }
  185. }
  186.  
  187. void getTypeOfHours(double hours, double& regHours, double& otHours)
  188. {
  189.     if (hours > MAX_REG_HOURS){
  190.         otHours = hours - MAX_REG_HOURS;
  191.         regHours = MAX_REG_HOURS;
  192.     }
  193.     else{
  194.         otHours = 0;
  195.         regHours = hours;
  196.     }
  197. }
  198.  
  199. // module to calculate the gross pay given hours and hourly pay rate
  200.  
  201. double calculateGrossPay(double regHours, double otHours, double hourlyRate)
  202. {
  203. return (regHours * hourlyRate) + (otHours * (hourlyRate * 1.5));
  204. }
  205.  
  206. double getSumOfGrossPay(double grossPays[],int num)
  207. {
  208.     int sum=0;
  209.     for (auto i = 0; i < num; i++) {
  210.         sum += grossPays[i];
  211.    }
  212.     return sum;
  213. }
  214.  
  215. double getAveOfGrossPay(double sum,int num)
  216. {
  217.     return sum / num;
  218. }
  219.  
  220. // module to calculate taxes paid
  221. double calculateTaxes(double gross,double deduction)
  222. {
  223.     return (gross-deduction) * TAX_RATE;
  224. }
  225. // module to use gross pay and taxes to calculate net pay
  226. double calculateNetPay(double grossPay, double taxes)
  227. {
  228.     return grossPay - taxes;
  229. }
  230. //// module to calculate taxes paid
  231. //
  232. //// module to display results in table format.
  233. //void displayResults(std::ofstream& outfile, std::string name, double regHours, double otHours, double hourlyRate, double grossPay, double netPay, double taxes, double deduction)
  234. //{
  235. //
  236. //
  237. //    outfile << "name             Reg Hours  OT hours  hourly Rate  gross pay  deduct  taxes  net Pay   " << std::endl;
  238. //    outfile << "===============  =========  ========  ===========  =========  ======  =====  =======  " << std::endl;
  239. //
  240. //    //for(int i =0; i<SIZE; i++)
  241. //    //{
  242. //    outfile << std::left << std::setw(15) << name << "  ";
  243. //    outfile << std::right << std::setw(9) << regHours << "  ";
  244. //    outfile << std::setw(8) << otHours << "  ";
  245. //    outfile << std::setw(11) << hourlyRate << "  ";
  246. //    outfile << std::setw(9) << grossPay << "  ";
  247. //    outfile << std::fixed << std::setprecision(2) << std::setw(6) << deduction << "  ";
  248. //    outfile << std::setw(5) << taxes << "  ";
  249. //    outfile << std::setw(7) << netPay << std::endl;
  250. //}//}
  251. //void displaySummery(std::ofstream& outfile, double grossPays[], std::string fullNames[], double sum, double ave, double minGrossPay)
  252. //{
  253. //
  254. //
  255. //    outfile << "============================" << std::endl;
  256. //    outfile << "  Employee Payroll Summery " << std::endl;
  257. //    outfile << "============================" << std::endl;
  258. //    outfile << " Name                  Gross pay" << std::endl;
  259. //    outfile << "====================  ===========" << std::endl;
  260. //    outfile << std::left << std::setw(22) << fullNames[0] << "  ";
  261. //    outfile << std::setw(11) << grossPays[0] << "  " << std::endl;
  262. //    outfile << std::setw(22) << fullNames[1] << "  ";
  263. //    outfile << std::setw(11) << grossPays[1] << "  " << std::endl;
  264. //    outfile << std::setw(22) << fullNames[2] << "  ";
  265. //    outfile << std::setw(11) << grossPays[2] << "  " << std::endl;
  266. //    outfile << "Total Gross Pay: " << sum << std::endl;
  267. //    outfile << "Average Gross Pay: " << ave << std::endl;
  268. //    outfile << "Minimum Gross Pay: " << minGrossPay << std::endl;
  269. //
  270. //}
  271. //
  272. bool getYesOrNo()
  273. {
  274.     char another;
  275.     std::cout << "Process another employee (Y/N)? : ";
  276.     std::cin >> another;
  277.  
  278.     if (another == 'Y' || another == 'y') { return true; }
  279.     return false;
  280. }
  281.  
  282.  
Advertisement
Add Comment
Please, Sign In to add comment