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> // setprecision, setw
  24. #include <iostream> // cout, cin, fixed
  25. #include <string> // string functions
  26. #include <cctype> // islower, toupper
  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. //********************************************************
  47. // Class Employee
  48. //********************************************************
  49.  
  50. class Employee
  51. {
  52. private:
  53.  
  54. // Private data members
  55. string firstName = "";
  56. string lastName = "";
  57. string taxState = "";
  58.  
  59. int clockNumber = 0;
  60.  
  61. float wageRate = 0.0;
  62. float hours = 0.0;
  63. float overTimeHrs = 0.0;
  64. float grossPay = 0.0;
  65. float stateTax = 0.0;
  66. float fedTax = 0.0;
  67. float netPay = 0.0;
  68.  
  69. // Private calculation functions
  70. float calcOverTimeHrs();
  71. float calcGrossPay();
  72. float calcStateTax();
  73. float calcFedTax();
  74. float calcNetPay();
  75.  
  76. // Getter function declarations
  77. string getFirstName();
  78. string getLastName();
  79. string getTaxState();
  80. int getClockNumber();
  81. float getWageRate();
  82. float getHours();
  83. float getOverTimeHrs();
  84. float getGrossPay();
  85. float getStateTax();
  86. float getFedTax();
  87. float getNetPay();
  88.  
  89. public:
  90.  
  91. // Default constructor
  92. Employee() {}
  93.  
  94. // Constructor with arguments
  95. Employee(string myFirstName, string myLastName,
  96. string myTaxState, int myClockNumber,
  97. float myWageRate, float myHours);
  98.  
  99. // Destructor
  100. ~Employee();
  101.  
  102. // Print employee information
  103. void printEmployee();
  104.  
  105. }; // End Employee class
  106.  
  107.  
  108. //********************************************************
  109. // Inline Getter Functions
  110. //********************************************************
  111.  
  112. // Retrieve First Name
  113. inline string Employee::getFirstName()
  114. {
  115. return firstName;
  116. }
  117.  
  118. // Retrieve Last Name
  119. inline string Employee::getLastName()
  120. {
  121. return lastName;
  122. }
  123.  
  124. // Retrieve Tax State
  125. inline string Employee::getTaxState()
  126. {
  127. return taxState;
  128. }
  129.  
  130. // Retrieve Clock Number
  131. inline int Employee::getClockNumber()
  132. {
  133. return clockNumber;
  134. }
  135.  
  136. // Retrieve Wage Rate
  137. inline float Employee::getWageRate()
  138. {
  139. return wageRate;
  140. }
  141.  
  142. // Retrieve Hours Worked
  143. inline float Employee::getHours()
  144. {
  145. return hours;
  146. }
  147.  
  148. // Retrieve Overtime Hours
  149. inline float Employee::getOverTimeHrs()
  150. {
  151. return overTimeHrs;
  152. }
  153.  
  154. // Retrieve Gross Pay
  155. inline float Employee::getGrossPay()
  156. {
  157. return grossPay;
  158. }
  159.  
  160. // Retrieve State Tax
  161. inline float Employee::getStateTax()
  162. {
  163. return stateTax;
  164. }
  165.  
  166. // Retrieve Federal Tax
  167. inline float Employee::getFedTax()
  168. {
  169. return fedTax;
  170. }
  171.  
  172. // Retrieve Net Pay
  173. inline float Employee::getNetPay()
  174. {
  175. return netPay;
  176. }
  177.  
  178.  
  179. //********************************************************
  180. // Calculate Overtime Hours
  181. //********************************************************
  182.  
  183. float Employee::calcOverTimeHrs()
  184. {
  185. if (hours > STD_HOURS)
  186. overTimeHrs = hours - STD_HOURS;
  187. else
  188. overTimeHrs = 0;
  189.  
  190. return overTimeHrs;
  191.  
  192. } // calcOverTimeHrs
  193.  
  194.  
  195. //********************************************************
  196. // Calculate Gross Pay
  197. //********************************************************
  198.  
  199. float Employee::calcGrossPay()
  200. {
  201. if (overTimeHrs > 0)
  202. {
  203. // Regular pay plus overtime pay
  204. grossPay = (STD_HOURS * wageRate)
  205. + (overTimeHrs * (wageRate * OT_RATE));
  206. }
  207. else
  208. {
  209. // Regular pay only
  210. grossPay = wageRate * hours;
  211. }
  212.  
  213. return grossPay;
  214.  
  215. } // calcGrossPay
  216.  
  217.  
  218. //********************************************************
  219. // Calculate State Tax
  220. //********************************************************
  221.  
  222. float Employee::calcStateTax()
  223. {
  224. float theStateTax;
  225.  
  226. theStateTax = grossPay;
  227.  
  228. // Calculate state tax based on employee residence
  229. if (taxState.compare("MA") == 0)
  230. theStateTax *= MA_TAX_RATE;
  231.  
  232. else if (taxState.compare("VT") == 0)
  233. theStateTax *= VT_TAX_RATE;
  234.  
  235. else if (taxState.compare("NH") == 0)
  236. theStateTax *= NH_TAX_RATE;
  237.  
  238. else if (taxState.compare("CA") == 0)
  239. theStateTax *= CA_TAX_RATE;
  240.  
  241. else
  242. theStateTax *= DEFAULT_TAX_RATE;
  243.  
  244. return theStateTax;
  245.  
  246. } // calcStateTax
  247.  
  248.  
  249. //********************************************************
  250. // Calculate Federal Tax
  251. //********************************************************
  252.  
  253. float Employee::calcFedTax()
  254. {
  255. float theFedTax;
  256.  
  257. // Federal tax is the same for all employees
  258. theFedTax = grossPay * FED_TAX_RATE;
  259.  
  260. return theFedTax;
  261.  
  262. } // calcFedTax
  263.  
  264.  
  265. //********************************************************
  266. // Calculate Net Pay
  267. //********************************************************
  268.  
  269. float Employee::calcNetPay()
  270. {
  271. float theNetPay;
  272. float theTotalTaxes;
  273.  
  274. // Calculate total taxes
  275. theTotalTaxes = stateTax + fedTax;
  276.  
  277. // Calculate take-home pay
  278. theNetPay = grossPay - theTotalTaxes;
  279.  
  280. return theNetPay;
  281.  
  282. } // calcNetPay
  283.  
  284.  
  285. //********************************************************
  286. // Constructor with Arguments
  287. //********************************************************
  288.  
  289. Employee::Employee(string myFirstName, string myLastName,
  290. string myTaxState, int myClockNumber,
  291. float myWageRate, float myHours)
  292. {
  293. // Initialize employee information
  294. firstName = myFirstName;
  295. lastName = myLastName;
  296.  
  297. // Convert state abbreviation to uppercase
  298. if (myTaxState.length() >= 2)
  299. {
  300. if (std::islower(static_cast<unsigned char>(myTaxState[0])))
  301. myTaxState[0] = std::toupper(static_cast<unsigned char>(myTaxState[0]));
  302.  
  303. if (std::islower(static_cast<unsigned char>(myTaxState[1])))
  304. myTaxState[1] = std::toupper(static_cast<unsigned char>(myTaxState[1]));
  305. }
  306.  
  307. taxState = myTaxState;
  308. clockNumber = myClockNumber;
  309. wageRate = myWageRate;
  310. hours = myHours;
  311.  
  312. // Calculate overtime hours
  313. overTimeHrs = calcOverTimeHrs();
  314.  
  315. // Calculate gross pay
  316. grossPay = calcGrossPay();
  317.  
  318. // Calculate state tax
  319. stateTax = calcStateTax();
  320.  
  321. // Calculate federal tax
  322. fedTax = calcFedTax();
  323.  
  324. // Calculate net pay
  325. netPay = calcNetPay();
  326.  
  327. } // Employee constructor
  328.  
  329.  
  330. //********************************************************
  331. // Print Employee Information
  332. //********************************************************
  333.  
  334. void Employee::printEmployee()
  335. {
  336. // Display employee input information
  337. cout << endl << endl << "*** Entered Details are ***" << endl;
  338.  
  339. cout << endl << " First Name: " << getFirstName();
  340. cout << endl << " Last Name: " << getLastName();
  341. cout << endl << " Tax State: " << getTaxState();
  342. cout << endl << " Clock Number: " << getClockNumber();
  343. cout << endl << " Wage Rate: $" << getWageRate();
  344. cout << endl << " Hours: " << getHours();
  345.  
  346. // Display calculated employee information
  347. cout << endl << endl << "*** Calculated Values are ***" << endl;
  348.  
  349. // Display overtime hours
  350. cout << endl << " Overtime Hours: "
  351. << getOverTimeHrs();
  352.  
  353. // Display gross pay
  354. cout << endl << " Gross Pay: $"
  355. << getGrossPay();
  356.  
  357. // Display state tax
  358. cout << endl << " State Tax: $"
  359. << getStateTax();
  360.  
  361. // Display federal tax
  362. cout << endl << " Federal Tax: $"
  363. << getFedTax();
  364.  
  365. // Display net pay
  366. cout << endl << " Net Pay: $"
  367. << getNetPay();
  368.  
  369. cout << "\n";
  370.  
  371. } // printEmployee
  372.  
  373.  
  374. //********************************************************
  375. // Employee Destructor
  376. //********************************************************
  377.  
  378. Employee::~Employee()
  379. {
  380. // Nothing to print here
  381. }
  382.  
  383.  
  384. //********************************************************
  385. // Main Function
  386. //********************************************************
  387.  
  388. int main()
  389. {
  390. // Local variables for employee information
  391. string myFirstName;
  392. string myLastName;
  393. string myTaxState;
  394.  
  395. int myClockNumber;
  396.  
  397. float myWageRate;
  398. float myHours;
  399.  
  400. // Set output to two decimal places
  401. cout << fixed << setprecision(2);
  402.  
  403. // Array of Employee objects
  404. Employee e[EMP_SIZE];
  405.  
  406. // Collect information for five employees
  407. for (int i = 0; i < EMP_SIZE; ++i)
  408. {
  409. cout << "\n\nEnter Employee First Name: ";
  410. cin >> myFirstName;
  411.  
  412. cout << "\nEnter Employee Last Name: ";
  413. cin >> myLastName;
  414.  
  415. cout << "\nEnter Employee Tax State: ";
  416. cin >> myTaxState;
  417.  
  418. cout << "\nEnter Employee Clock Number: ";
  419. cin >> myClockNumber;
  420.  
  421. cout << "\nEnter Employee Hourly Wage Rate: ";
  422. cin >> myWageRate;
  423.  
  424. cout << "\nEnter Employee Hours Worked for the Week: ";
  425. cin >> myHours;
  426.  
  427. // Create and assign Employee object
  428. e[i] = {myFirstName, myLastName, myTaxState,
  429. myClockNumber, myWageRate, myHours};
  430.  
  431. // Print employee information
  432. e[i].printEmployee();
  433.  
  434. } // End for loop
  435.  
  436. return 0;
  437.  
  438. } // End 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