fork download
  1. //********************************************************
  2. //
  3. // Assignment 11 - Object Oriented Design
  4. //
  5. // Name: <replace with your name>
  6. //
  7. // Class: C Programming, <replace with Semester and Year>
  8. //
  9. // Date: <replace with the current date>
  10. //
  11. // Description: An object oriented program design using
  12. // C++ that will process our existing set of employees.
  13. // It utilizes a class called Employee and generates an
  14. // array of objects that are used to store, calculate,
  15. // and print out a simple report of inputted and calculated
  16. // values.
  17. //
  18. //
  19. // Object Oriented Design (using C++)
  20. //
  21. //********************************************************
  22.  
  23. #include <iomanip> // std::setprecision, std::setw
  24. #include <iostream> // std::cout, std::fixed
  25. #include <fstream> // file I/O
  26. #include <string> // string functions
  27.  
  28. // define constants
  29. #define EMP_SIZE 5
  30. #define STD_HOURS 40.0
  31. #define OT_RATE 1.5
  32. #define MA_TAX_RATE 0.05
  33. #define NH_TAX_RATE 0.0
  34. #define VT_TAX_RATE 0.06
  35. #define CA_TAX_RATE 0.07
  36. #define DEFAULT_TAX_RATE 0.08
  37. #define NAME_SIZE 20
  38. #define TAX_STATE_SIZE 3
  39. #define FED_TAX_RATE 0.25
  40. #define FIRST_NAME_SIZE 10
  41. #define LAST_NAME_SIZE 10
  42.  
  43. using namespace std;
  44.  
  45. // class Employee
  46. class Employee
  47. {
  48. private:
  49.  
  50. // private data available only to member functions
  51.  
  52. string firstName; // Employee First Name
  53. string lastName; // Employee Last Name
  54. string taxState; // Employee Tax State
  55. int clockNumber; // Employee Clock Number
  56. float wageRate; // Hourly Wage Rate
  57. float hours; // Hours worked in a week
  58. float overTimeHrs; // Overtime Hours worked
  59. float grossPay; // Weekly Gross Pay
  60. float stateTax; // State Tax
  61. float fedTax; // Fed Tax
  62. float netPay; // Net Pay
  63.  
  64. // private member function to calculate Overtime Hours
  65. float calcOverTimeHrs ( )
  66. {
  67. // Calculate the overtime hours for the week
  68. if (hours > STD_HOURS)
  69. overTimeHrs = hours - STD_HOURS; // ot hours
  70. else
  71. overTimeHrs = 0; // no ot hours
  72.  
  73. // the calculated overtime hours
  74. return (overTimeHrs);
  75.  
  76. } // calcOverTimeHrs
  77.  
  78. // private member function to calculate Gross Pay
  79. float calcGrossPay ( )
  80. {
  81. // Process gross pay based on if there is overtime
  82. if (overTimeHrs > 0)
  83. {
  84. // overtime
  85. grossPay = (STD_HOURS * wageRate) // normal pay
  86. + (overTimeHrs * (wageRate * OT_RATE)); // ot pay
  87. }
  88. else // no overtime pay
  89. {
  90. grossPay = wageRate * hours; // normal pay
  91. }
  92.  
  93. // the calculated gross pay
  94. return (grossPay);
  95.  
  96. } // calcGrossPay
  97.  
  98. // private member function to calculate State Tax
  99. float calcStateTax ()
  100. {
  101.  
  102. float theStateTax; // calculated state tax
  103.  
  104. theStateTax = grossPay; // initialize to gross pay
  105.  
  106. // calculate state tax based on where employee resides
  107.  
  108. if (taxState.compare("MA") == 0)
  109. theStateTax *= MA_TAX_RATE;
  110. else if (taxState.compare("VT") == 0)
  111. theStateTax *= VT_TAX_RATE;
  112. else if (taxState.compare("NH") == 0)
  113. theStateTax *= NH_TAX_RATE;
  114. else if (taxState.compare("CA") == 0)
  115. theStateTax *= CA_TAX_RATE;
  116. else
  117. theStateTax *= DEFAULT_TAX_RATE; // any other state
  118.  
  119. // return the calculated state tax
  120. return (theStateTax);
  121.  
  122. } // calcStateTax
  123.  
  124. // private member function to calculate Federal Tax
  125. float calcFedTax ()
  126. {
  127.  
  128. float theFedTax; // The calculated Federal Tax
  129.  
  130. // Federal Tax is the same for all regardless of state
  131. theFedTax = grossPay * FED_TAX_RATE;
  132.  
  133. // return the calculated federal tax
  134. return (theFedTax);
  135.  
  136. } // calcFedTax
  137.  
  138. // private member function to calculate Net Pay
  139. float calcNetPay ()
  140. {
  141.  
  142. float theNetPay; // total take home pay (minus taxes)
  143. float theTotalTaxes; // total taxes owed
  144.  
  145. // calculate the total taxes owed
  146. theTotalTaxes = stateTax + fedTax;
  147.  
  148. // calculate the net pay
  149. theNetPay = grossPay - theTotalTaxes;
  150.  
  151. // return the calculated net pay
  152. return (theNetPay);
  153.  
  154. } // calcNetPay
  155.  
  156. public:
  157.  
  158. // public member functions that can be called
  159. // to access private data member items
  160.  
  161. // public no argument constructor with defaults
  162. Employee() {firstName=""; lastName=""; taxState="";
  163. clockNumber=0; wageRate=0; hours=0;}
  164.  
  165. // public constructor with arguments passed to it
  166. Employee (string myFirstName, string myLastName, string myTaxState,
  167. int myClockNumber, float myWageRate, float myHours);
  168.  
  169. ~Employee(); // destructor
  170.  
  171. // public getter function prototypes to retrieve private data
  172. string getFirstName();
  173. string getLastName();
  174. string getTaxState();
  175. int getClockNumber();
  176. float getWageRate();
  177. float getHours();
  178. float getOverTimeHrs();
  179. float getGrossPay();
  180.  
  181. // TODO - Add public getter function prototype to retrieve stateTax
  182.  
  183. // TODO - Add public getter function prototype to retrieve fedTax
  184.  
  185. // TODO - Add public getter function prototype to retrieve netPay
  186.  
  187. // member function prototype to print an Employee object
  188. void printEmployee(ofstream &outputFile, Employee e); // passes an object
  189.  
  190. }; // class Employee
  191.  
  192. // constructor with arguments
  193. Employee::Employee (string myFirstName, string myLastName, string myTaxState,
  194. int myClockNumber, float myWageRate, float myHours)
  195. {
  196. // initialize all member data items
  197. firstName = myFirstName; // input
  198. lastName = myLastName; // input
  199.  
  200. // Convert myTaxState to uppercase if entered in lowercase
  201. if (std::islower(myTaxState[0]))
  202. myTaxState[0] = std::toupper(myTaxState[0]);
  203. if (std::islower(myTaxState[1]))
  204. myTaxState[1] = std::toupper(myTaxState[1]);
  205.  
  206. taxState = myTaxState; // input
  207. clockNumber = myClockNumber; // input
  208. wageRate = myWageRate; // input
  209. hours = myHours; // input
  210. overTimeHrs = calcOverTimeHrs(); // calculated overtime hours
  211. grossPay = calcGrossPay(); // calculated gross pay
  212.  
  213. // TODO - set stateTax as the return from calcStateTax member function
  214.  
  215. // TODO - set fedTax as the return from calcFedTax member function
  216.  
  217. // TODO - set netPay as the return from calcNetPay member function
  218.  
  219. } // Employee constructor
  220.  
  221. // Employee's destructor
  222. Employee::~Employee()
  223. {
  224. // nothing to print here, but could ...
  225. }
  226.  
  227. // A "getter" function that will retrieve the First Name
  228. string Employee::getFirstName() {
  229. return firstName;
  230. }
  231.  
  232. // A "getter" function that will retrieve the employee Last Name
  233. string Employee::getLastName() {
  234. return lastName;
  235. }
  236.  
  237. // A "getter" function that will retrieve the employee Tax State
  238. string Employee::getTaxState() {
  239. return taxState;
  240. }
  241.  
  242. // A "getter" function that will retrieve the employee Clock Number
  243. int Employee::getClockNumber() {
  244. return clockNumber;
  245. }
  246.  
  247. // A "getter" function that will retrieve the employee Wage Rate
  248. float Employee::getWageRate() {
  249. return wageRate;
  250. }
  251.  
  252. // A "getter" function that will retrieve the employee Hours Worked
  253. float Employee::getHours() {
  254. return hours;
  255. }
  256.  
  257. // A "getter" function that will retrieve the employee Overtime Hours
  258. float Employee::getOverTimeHrs() {
  259. return overTimeHrs;
  260. }
  261.  
  262. // A "getter" function that will retrieve the employee Gross Pay
  263. float Employee::getGrossPay() {
  264. return grossPay;
  265. }
  266.  
  267. // TODO - Add a "getter" function that will retrieve the employee stateTax
  268.  
  269. // TODO - Add a "getter" function that will retrieve the employee fedTax
  270.  
  271. // TODO - Add a "getter" function that will retrieve the employee netPay
  272.  
  273.  
  274. // a member function to print out the info in a given Employee object
  275. void Employee::printEmployee(ofstream &outputFile, Employee e) {
  276.  
  277. // Display the entered input on the Employee
  278. outputFile <<"\n\n *** Entered Details are *** \n";
  279. outputFile <<"\n First Name: "<< e.getFirstName();
  280. outputFile <<"\n Last Name: "<< e.getLastName();
  281. outputFile <<"\n Tax State: "<< e.getTaxState();
  282. outputFile <<"\n Clock Number: "<< e.getClockNumber();
  283. outputFile <<"\n Wage Rate: "<< e.getWageRate ();
  284. outputFile <<"\n Hours: "<< e.getHours ();
  285.  
  286. // Display the calculated values of the Employee
  287. outputFile <<"\n\n *** Calculated Values are *** \n";
  288.  
  289. // print out overtime hours based on the employee information entered
  290. outputFile <<"\n Overtime Hours : " << e.getOverTimeHrs();
  291.  
  292. // print out gross pay based on the employee information entered
  293. outputFile <<"\n Gross Pay : $" << e.getGrossPay();
  294.  
  295. // TODO - Add cout statement with call to stateTax getter function
  296.  
  297. // TODO - Add cout statement with call to fedTax getter function
  298.  
  299. // TODO - Add cout statement with call to netPay getter function
  300.  
  301. // add a new line to separate the next employee
  302. outputFile <<"\n";
  303.  
  304. } // printEmployee
  305.  
  306. // main function to start the processing
  307. int main ()
  308. {
  309. // local variables to collect user input
  310.  
  311. string myFirstName; // First Name to input
  312. string myLastName; // Last Name to input
  313. string myTaxState; // Tax State to input
  314. int myClockNumber; // Clock Number to input
  315. float myWageRate; // Wage Rate to input
  316. float myHours; // Hours to input
  317.  
  318. cout << fixed // fix the number of decimal digits
  319. << setprecision(2); // to 2
  320.  
  321. // Array of Objects to store each of our employees
  322. Employee e[EMP_SIZE];
  323.  
  324. // set up printing to a file
  325. ofstream outputFile;
  326. outputFile.open("assignment11_output.txt");
  327.  
  328. // prompt for information to read in employee information
  329. for (int i = 0; i < EMP_SIZE; ++i)
  330. {
  331. // Enter Employee Information
  332. cout <<"\n\n Enter Employee First Name: ";
  333. cin>>myFirstName ;
  334. cout <<"\n Enter Employee Last Name: ";
  335. cin>>myLastName ;
  336. cout <<"\n Enter Employee Tax State: ";
  337. cin>>myTaxState ;
  338. cout<<"\n Enter Employee Clock Number: ";
  339. cin>>myClockNumber;
  340. cout <<"\n Enter Employee Hourly Wage Rate: ";
  341. cin>>myWageRate;
  342. cout <<"\n Enter Employee Hours Worked for the Week: ";
  343. cin>>myHours;
  344.  
  345. // Call our constructor to create an employee object
  346. // using the input entered above. The constructor
  347. // will also put into motion the execution of the
  348. // various private member functions which will
  349. // calculate the overtime, gross pay, state tax, federal
  350. // tax, and net pay for the current employee.
  351.  
  352. // The updated object will be returned and placed in the
  353. // the element of our array of objects named "e", using the index
  354. // of the current value of our loop index "i" ... thus: e[i]
  355. e[i] = {myFirstName, myLastName, myTaxState,
  356. myClockNumber, myWageRate, myHours};
  357.  
  358. // Call the printEmployee public member function to display all
  359. // the inputted and calculated values for the current employee
  360. // to the specified output file
  361. e[i].printEmployee(outputFile, e[i]);
  362.  
  363. } // for
  364.  
  365. return 0;
  366.  
  367. } // main
Success #stdin #stdout 0s 5304KB
stdin
Connie
Cobol
MA
98401
10.60
51.0
Mary
Apl
NH
526488
9.75
42.5
Frank
Fortran
VT
765349
10.50
37.0
Jeff
Ada
NY
34645
12.25
45
Anton
Pascal
CA
127615
8.35
40.0
stdout

 Enter Employee First Name: 
 Enter Employee Last Name: 
 Enter Employee Tax State: 
 Enter Employee Clock Number: 
 Enter Employee Hourly Wage Rate: 
 Enter Employee Hours Worked for the Week: 

 Enter Employee First Name: 
 Enter Employee Last Name: 
 Enter Employee Tax State: 
 Enter Employee Clock Number: 
 Enter Employee Hourly Wage Rate: 
 Enter Employee Hours Worked for the Week: 

 Enter Employee First Name: 
 Enter Employee Last Name: 
 Enter Employee Tax State: 
 Enter Employee Clock Number: 
 Enter Employee Hourly Wage Rate: 
 Enter Employee Hours Worked for the Week: 

 Enter Employee First Name: 
 Enter Employee Last Name: 
 Enter Employee Tax State: 
 Enter Employee Clock Number: 
 Enter Employee Hourly Wage Rate: 
 Enter Employee Hours Worked for the Week: 

 Enter Employee First Name: 
 Enter Employee Last Name: 
 Enter Employee Tax State: 
 Enter Employee Clock Number: 
 Enter Employee Hourly Wage Rate: 
 Enter Employee Hours Worked for the Week: