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


 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


 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


 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


 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