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

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $-0.00
 State Tax : $-0.00
 Federal Tax : $-0.00
 Net Pay : $0.00


 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: 
 Last Name: 
 Tax State: 
 Clock Number: 5311
 Wage Rate: -0.00
 Hours: 0.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $-0.00
 State Tax : $-0.00
 Federal Tax : $-0.00
 Net Pay : $0.00


 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: 
 Last Name: 
 Tax State: 
 Clock Number: 5311
 Wage Rate: -0.00
 Hours: 0.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $-0.00
 State Tax : $-0.00
 Federal Tax : $-0.00
 Net Pay : $0.00


 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: 
 Last Name: 
 Tax State: 
 Clock Number: 5311
 Wage Rate: -0.00
 Hours: 0.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $-0.00
 State Tax : $-0.00
 Federal Tax : $-0.00
 Net Pay : $0.00


 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: 
 Last Name: 
 Tax State: 
 Clock Number: 5311
 Wage Rate: -0.00
 Hours: 0.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $-0.00
 State Tax : $-0.00
 Federal Tax : $-0.00
 Net Pay : $0.00