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 <string> // string functions
  26. #include <cctype>
  27.  
  28. using namespace std;
  29.  
  30. // define constants
  31. #define STD_HOURS 40.0
  32. #define OT_RATE 1.5
  33. #define MA_TAX_RATE 0.05
  34. #define NH_TAX_RATE 0.0
  35. #define VT_TAX_RATE 0.06
  36. #define CA_TAX_RATE 0.07
  37. #define DEFAULT_TAX_RATE 0.08
  38. #define NAME_SIZE 20
  39. #define TAX_STATE_SIZE 3
  40. #define FED_TAX_RATE 0.25
  41. #define FIRST_NAME_SIZE 10
  42. #define LAST_NAME_SIZE 10
  43.  
  44. #define EMP_SIZE 5
  45.  
  46. using std::string;
  47.  
  48. // class Employee
  49. class Employee
  50. {
  51. private:
  52.  
  53. // private data available only to member functions
  54. // all data is initialized to support creating instances that
  55. // are not done via a full constructor with data to be used
  56.  
  57. string firstName = ""; // Employee First Name
  58. string lastName = ""; // Employee Last Name
  59. string taxState = ""; // Employee Tax State
  60. int clockNumber = 0; // Employee Clock Number
  61. float wageRate = 0.0; // Hourly Wage Rate
  62. float hours = 0.0; // Hours worked in a week
  63. float overTimeHrs = 0.0; // Overtime Hours worked
  64. float grossPay = 0.0; // Weekly Gross Pay
  65. float stateTax = 0.0; // State Tax
  66. float fedTax = 0.0; // Fed Tax
  67. float netPay = 0.0; // Net Pay
  68.  
  69. // private functions for call only by an Employee object
  70. // ... these are gernally more complex computations
  71. float calcOverTimeHrs();
  72. float calcGrossPay();
  73. float calcStateTax();
  74. float calcFedTax();
  75. float calcNetPay();
  76.  
  77. // Delcare "getter" function prototypes to retrieve private data
  78. // Note: The inline keyword is not required for the prototypes
  79. string getFirstName();
  80. string getLastName();
  81. string getTaxState();
  82. int getClockNumber();
  83. float getWageRate();
  84. float getHours();
  85. float getOverTimeHrs();
  86. float getGrossPay();
  87. float getStateTax();
  88. float getFedTax();
  89. float getNetPay();
  90.  
  91. // TODO - Declare a "getter" function declaration that will retrieve the employee stateTax
  92.  
  93. // TODO - Declare a "getter" function declaration that will retrieve the employee fedTax
  94.  
  95. // TODO - Declare a "getter" function declaration that will retrieve the employee netPay
  96.  
  97. public:
  98.  
  99. // public member functions that can be called
  100. // to access private data member items
  101.  
  102. // public no argument constructor with defaults
  103. // All Employee class data will be initialized to defaults
  104. Employee() {}
  105.  
  106. // public constructor with arguments passed to it
  107. Employee (string myFirstName, string myLastName, string myTaxState,
  108. int myClockNumber, float myWageRate, float myHours);
  109.  
  110. ~Employee(); // destructor
  111.  
  112. // print out Employee data to the console
  113. void printEmployee();
  114.  
  115.  
  116. }; // End class declarations.
  117.  
  118. // The inlined Employee members follow...
  119.  
  120. // A "getter" function that will retrieve the First Name
  121. inline string Employee::getFirstName() {
  122. return firstName;
  123. }
  124.  
  125. // A "getter" function that will retrieve the employee Last Name
  126. inline string Employee::getLastName() {
  127. return lastName;
  128. }
  129.  
  130. // A "getter" function that will retrieve the employee Tax State
  131. inline string Employee::getTaxState() {
  132. return taxState;
  133. }
  134.  
  135. // A "getter" function that will retrieve the employee Clock Number
  136. inline int Employee::getClockNumber() {
  137. return clockNumber;
  138. }
  139.  
  140. // A "getter" function that will retrieve the employee Wage Rate
  141. inline float Employee::getWageRate() {
  142. return wageRate;
  143. }
  144.  
  145. // A "getter" function that will retrieve the employee Hours Worked
  146. inline float Employee::getHours() {
  147. return hours;
  148. }
  149.  
  150. // A "getter" function that will retrieve the employee Overtime Hours
  151. inline float Employee::getOverTimeHrs() {
  152. return overTimeHrs;
  153. }
  154.  
  155. // A "getter" function that will retrieve the employee Gross Pay
  156. inline float Employee::getGrossPay() {
  157. return grossPay;
  158. }
  159.  
  160. // A "getter" function to retrieve employee state tax
  161. inline float Employee::getStateTax() {
  162. return stateTax;
  163. }
  164.  
  165. // A "getter" function to retrieve employee federal tax
  166. inline float Employee::getFedTax() {
  167. return fedTax;
  168. }
  169.  
  170. // A "getter" function to retrieve employee net pay
  171. inline float Employee::getNetPay() {
  172. return netPay;
  173. }
  174.  
  175. // TODO - Add an inline "getter" function that will retrieve the employee stateTax
  176.  
  177. // TODO - Add an inline "getter" function that will retrieve the employee fedTax
  178.  
  179. // TODO - Add an inline "getter" function that will retrieve the employee netPay
  180.  
  181.  
  182. // private member function to calculate Overtime Hours
  183. float Employee::calcOverTimeHrs ( )
  184. {
  185. // Calculate the overtime hours for the week
  186. if (hours > STD_HOURS)
  187. overTimeHrs = hours - STD_HOURS; // ot hours
  188. else
  189. overTimeHrs = 0; // no ot hours
  190.  
  191. // the calculated overtime hours
  192. return (overTimeHrs);
  193.  
  194. } // calcOverTimeHrs
  195.  
  196. // private member function to calculate Gross Pay
  197. float Employee::calcGrossPay ( )
  198. {
  199. // Process gross pay based on if there is overtime
  200. if (overTimeHrs > 0)
  201. {
  202. // overtime
  203. grossPay = (STD_HOURS * wageRate) // normal pay
  204. + (overTimeHrs * (wageRate * OT_RATE)); // ot pay
  205. }
  206. else // no overtime pay
  207. {
  208. grossPay = wageRate * hours; // normal pay
  209. }
  210.  
  211. // the calculated gross pay
  212. return (grossPay);
  213.  
  214. } // calcGrossPay
  215.  
  216. // private member function to calculate State Tax
  217. float Employee::calcStateTax ()
  218. {
  219.  
  220. float theStateTax; // calculated state tax
  221.  
  222. theStateTax = grossPay; // initialize to gross pay
  223.  
  224. // calculate state tax based on where employee resides
  225.  
  226. if (taxState.compare("MA") == 0)
  227. theStateTax *= MA_TAX_RATE;
  228. else if (taxState.compare("VT") == 0)
  229. theStateTax *= VT_TAX_RATE;
  230. else if (taxState.compare("NH") == 0)
  231. theStateTax *= NH_TAX_RATE;
  232. else if (taxState.compare("CA") == 0)
  233. theStateTax *= CA_TAX_RATE;
  234. else
  235. theStateTax *= DEFAULT_TAX_RATE; // any other state
  236.  
  237. // return the calculated state tax
  238. return (theStateTax);
  239.  
  240. } // calcStateTax
  241.  
  242. // private member function to calculate Federal Tax
  243. float Employee::calcFedTax ()
  244. {
  245.  
  246. float theFedTax; // The calculated Federal Tax
  247.  
  248. // Federal Tax is the same for all regardless of state
  249. theFedTax = grossPay * FED_TAX_RATE;
  250.  
  251. // return the calculated federal tax
  252. return (theFedTax);
  253.  
  254. } // calcFedTax
  255.  
  256. // private member function to calculate Net Pay
  257. float Employee::calcNetPay ()
  258. {
  259.  
  260. float theNetPay; // total take home pay (minus taxes)
  261. float theTotalTaxes; // total taxes owed
  262.  
  263. // calculate the total taxes owed
  264. theTotalTaxes = stateTax + fedTax;
  265.  
  266. // calculate the net pay
  267. theNetPay = grossPay - theTotalTaxes;
  268.  
  269. // return the calculated net pay
  270. return (theNetPay);
  271.  
  272. } // calcNetPay
  273.  
  274.  
  275. // constructor with arguments
  276. Employee::Employee (string myFirstName, string myLastName, string myTaxState,
  277. int myClockNumber, float myWageRate, float myHours)
  278. {
  279. // initialize all member data items
  280. firstName = myFirstName; // input
  281. lastName = myLastName; // input
  282.  
  283. // Convert myTaxState to uppercase if entered in lowercase
  284. if (std::islower(myTaxState[0]))
  285. myTaxState[0] = std::toupper(myTaxState[0]);
  286. if (std::islower(myTaxState[1]))
  287. myTaxState[1] = std::toupper(myTaxState[1]);
  288.  
  289. taxState = myTaxState; // input
  290. clockNumber = myClockNumber; // input
  291. wageRate = myWageRate; // input
  292. hours = myHours; // input
  293. overTimeHrs = calcOverTimeHrs(); // calculated overtime hours
  294. grossPay = calcGrossPay(); // calculated gross pay
  295.  
  296. // Calculate state tax
  297. stateTax = calcStateTax();
  298.  
  299. // Calculate federal tax
  300. fedTax = calcFedTax();
  301.  
  302. // Calculate net pay
  303. netPay = calcNetPay();
  304.  
  305. // TODO - set stateTax as the return from calcStateTax member function
  306.  
  307. // TODO - set fedTax as the return from calcFedTax member function
  308.  
  309. // TODO - set netPay as the return from calcNetPay member function
  310.  
  311. } // Employee constructor
  312.  
  313. // a member function to print out the info in a given Employee object
  314. void Employee::printEmployee() {
  315.  
  316. // Display the entered input on the Employee
  317. cout << endl << endl <<"*** Entered Details are *** " << endl;
  318. cout << endl <<" First Name: "<< getFirstName();
  319. cout << endl <<" Last Name: "<< getLastName();
  320. cout << endl <<" Tax State: "<< getTaxState();
  321. cout << endl <<" Clock Number: "<< getClockNumber();
  322. cout << endl <<" Wage Rate: "<< getWageRate ();
  323. cout << endl <<" Hours: "<< getHours ();
  324.  
  325. // Display the calculated values of the Employee
  326. cout<< endl << endl <<" *** Calculated Values are *** " << endl;
  327.  
  328. // print out overtime hours based on the employee information entered
  329. cout << endl << " Overtime Hours : " << getOverTimeHrs();
  330.  
  331. // print out gross pay based on the employee information entered
  332. cout << endl << " Gross Pay : $" << getGrossPay();
  333.  
  334. // print out employee state tax
  335. cout << endl << " State Tax : $" << getStateTax();
  336.  
  337. // print out employee federal tax
  338. cout << endl << " Federal Tax : $" << getFedTax();
  339.  
  340. // print out employee net pay
  341. cout << endl << " Net Pay : $" << getNetPay();
  342.  
  343. // TODO - Add cout statement with call to stateTax getter function
  344.  
  345. // TODO - Add cout statement with call to fedTax getter function
  346.  
  347. // TODO - Add cout statement with call to netPay getter function
  348.  
  349. // add a new line to separate the next employee
  350. cout <<"\n";
  351.  
  352. } // printEmployee
  353.  
  354. // Employee's destructor
  355. Employee::~Employee()
  356. {
  357. // nothing to print here, but could ...
  358. }
  359.  
  360.  
  361. // main function to start the processing
  362. int main ()
  363. {
  364. // local variables to collect user input
  365.  
  366. string myFirstName; // First Name to input
  367. string myLastName; // Last Name to input
  368. string myTaxState; // Tax State to input
  369. int myClockNumber; // Clock Number to input
  370. float myWageRate; // Wage Rate to input
  371. float myHours; // Hours to input
  372.  
  373. cout << fixed // fix the number of decimal digits
  374. << setprecision(2); // to 2
  375.  
  376. // Array of Objects to store each of our employees
  377. // This calls the default constructor for each array element
  378. Employee e[EMP_SIZE];
  379.  
  380. // prompt for information to read in employee information
  381. for (int i = 0; i < EMP_SIZE; ++i)
  382. {
  383. // Enter Employee Information
  384. cout <<"\n\n Enter Employee First Name: ";
  385. cin>>myFirstName ;
  386. cout <<"\n Enter Employee Last Name: ";
  387. cin>>myLastName ;
  388. cout <<"\n Enter Employee Tax State: ";
  389. cin>>myTaxState ;
  390. cout<<"\n Enter Employee Clock Number: ";
  391. cin>>myClockNumber;
  392. cout <<"\n Enter Employee Hourly Wage Rate: ";
  393. cin>>myWageRate;
  394. cout <<"\n Enter Employee Hours Worked for the Week: ";
  395. cin>>myHours;
  396.  
  397. // Call our constructor to create an employee object
  398. // using the input entered above. The constructor
  399. // will also put into motion the execution of the
  400. // various private member functions which will
  401. // calculate the overtime, gross pay, state tax, federal
  402. // tax, and net pay for the current employee.
  403.  
  404. // The updated object will be returned and placed in the
  405. // the element of our array of objects named "e", using the index
  406. // of the current value of our loop index "i" ... thus: e[i]
  407. e[i] = {myFirstName, myLastName, myTaxState,
  408. myClockNumber, myWageRate, myHours
  409. };
  410.  
  411. // Call the printEmployee public member function to display all
  412. // the inputted and calculated values for the current employee
  413. e[i].printEmployee();
  414.  
  415. } // for
  416.  
  417. return 0;
  418.  
  419. } // main
Success #stdin #stdout 0s 544KB
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: 

*** Entered Details are *** 

 First Name: Connie
 Last Name: Cobol
 Tax State: MA
 Clock Number: 98401
 Wage Rate: 10.60
 Hours: 51.00

 *** Calculated Values are *** 

 Overtime Hours : 11.00
 Gross Pay : $598.90
 State Tax : $29.95
 Federal Tax : $149.73
 Net Pay : $419.23


 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: 

*** Entered Details are *** 

 First Name: Mary
 Last Name: Apl
 Tax State: NH
 Clock Number: 526488
 Wage Rate: 9.75
 Hours: 42.50

 *** Calculated Values are *** 

 Overtime Hours : 2.50
 Gross Pay : $426.56
 State Tax : $0.00
 Federal Tax : $106.64
 Net Pay : $319.92


 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: 

*** Entered Details are *** 

 First Name: Frank
 Last Name: Fortran
 Tax State: VT
 Clock Number: 765349
 Wage Rate: 10.50
 Hours: 37.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $388.50
 State Tax : $23.31
 Federal Tax : $97.12
 Net Pay : $268.07


 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: 

*** Entered Details are *** 

 First Name: Jeff
 Last Name: Ada
 Tax State: NY
 Clock Number: 34645
 Wage Rate: 12.25
 Hours: 45.00

 *** Calculated Values are *** 

 Overtime Hours : 5.00
 Gross Pay : $581.88
 State Tax : $46.55
 Federal Tax : $145.47
 Net Pay : $389.86


 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: 

*** Entered Details are *** 

 First Name: Anton
 Last Name: Pascal
 Tax State: CA
 Clock Number: 127615
 Wage Rate: 8.35
 Hours: 40.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $334.00
 State Tax : $23.38
 Federal Tax : $83.50
 Net Pay : $227.12