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

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross 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: 5440
 Wage Rate: -0.00
 Hours: 0.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross 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: 5440
 Wage Rate: -0.00
 Hours: 0.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross 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: 5440
 Wage Rate: -0.00
 Hours: 0.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross 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: 5440
 Wage Rate: -0.00
 Hours: 0.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $-0.00