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