this is assignment i am working on they are two projects the first one i have do
ID: 3730380 • Letter: T
Question
this is assignment i am working on they are two projects the first one i have done and the specifications on the first are listed then we have to modify the first project which i added the paramteres below the first I am not going to lie i am completely lost on this and any direction would be appreciated.
Project 1 parameters
Background
This program is the first of a series of three programs that you will do over the course of the semester that deal with a payroll application. When you are done with this project, be sure to put a copy in a safe place because you will use it later. Modern software development is very much an incremental and iterative process. Programs are developed a piece at a time as their developers iterate on the design, refactor existing code, and add new functionality. This set of exercises will give you a glimpse into that process. In this first program you will develop an Employee class. This project is all about properly designing and coding a program that uses a class of your own design that is easy to modify.
Objective
Develop object-oriented design skills.
Create the proper structure and code for a C++ program containing a class of your design.
Create a driver program to test your class.
The Employee Class
For this project you need to design an Employee class. Your Employee class should have the following data members:
employeeNumber (integer)
name (string)
address (string)
phone (string)
hourlyWage (double)
hoursWorked this week (double)
Your Employee class will have the following functions:
A constructor for the Employee class that takes arguments to initialize all of the above mentioned data members.
'Getters' for each data attribute of your class.
Setters for every attribute except employeeNumber. We will not use the setters in this assignment, but in an actual payroll application, employee data, except for the employee number, can change (due to address change, marriage, a raise or promotion etc.).
A function, calcPay() that calculates and returns an employee's net pay. An employee's gross pay is calculated by multiplying the hours worked by their hourly wage. Be sure to give time-and-a-half for overtime (anything over 40 hours). To compute the net pay, deduct 20% of the gross for Federal income tax, and 7.5% of the gross for state income tax.
A function, printCheck() that prints out a pay check for each employee.
The major function of this first exercise is to test your Employee class. You will upload 3 files:
employee.h
employee.cpp
main.cpp
Your main() function does the following.
Creates two Employee objects with data that you can glean from the output below. These objects will be 'hard coded' into your .cpp file. There will be no interactive input for this program.
Calls the needed getter member functions to display the employee's name, number, address, and phone, as shown below.
Calls printCheck() to print the check, which follows the employee's personal information. Note that printCheck calls calcPay, and prints the whitespace that you see below before and after the check portion.
Project 2 paramters
Background
Recall that we are iterating on the development of a payroll program. At this point, you should have a working Employee class. In this project you will add the ability for objects of your Employee class to write themselves out to a file, and be read in from a file. The ability for an object to save itself in a file is called persistence.
You will also throw an exception if there are any file I/O errors. Get your program working without exception handling first. Then add the exception handling.
Objectives
Continue to refine your object oriented design skills.
Create the proper structure and code for a C++ program containing a class of your design.
Create a class that has persistence.
Throw and handle an exception.
Create a main() function to test the additions to your class.
The Employee Class
For this project you need to add the following member functions to your Employee class.
The function Employee::read is a static member function. It returns an Employee object based on the data it reads from a file. It must be called as Employee::read(...) since it's job is to create an object. If there is a read error, it throws a std::runtime_error (defined in <stdexcept>) exception with an appropriate message. Note that you are passing the stream by reference, not by value (streams cannot be copied).
The main() function
The major goal of main() is to test the additions to your Employee class. Use the printCheck() function from your previous Employee project. The output of your program will look similar to the output of your previous Employee project.
Your driver will contain a main() function that does the following:
Present the user with a menu of choices, create a data file, or read data from a file and print checks:
If the user selects the first option, your program should:
Create an ofstream object using a file name obtained from the user. Pass just the file name as the parameter (no path) so that your program assumes the file to be in the same folder as your executable file.
Create three employee objects as shown:
Employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 45.00, 10.00)
Employee sam(21, "Sam Jones", "45 East State", "661-9000", 30.00, 12.00)
Employee mary(15, "Mary Smith", "12 High Street", "401-8900", 40.00, 15.00)
Send messages to each of the three Employee objects to write themselves out to the file.
Print a message that creation of the file is complete.
Exit.
If the user selects the second option, your program should:
Prompt for the name of the file that the program saved.
Call Employee::read to read in the objects written in step 1, one-by-one.
Call the printCheck() function for each of the three new objects, just as you did in the previous project.
Exit.
Run the second option twice. Once with the correct filename, and once with an incorrect one. The second run will test your exception handling. In the error case, throw a std::runtime_error (defined in <stdexcept>) with a suitable error message string. Catch the exception and print its message in main, then exit the program. Note: you must test for input errors for each input operation.
In addition, when reading the file for the second part, you need to check that the expected data is there. If there are any errors in the data (there shouldn't be, but you should check anyway), your code should throw a std::runtime_error with a message explaining which data was corrupted.
my code
.h file
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
class employee {
public:
void SetEmployeeNumber(int num);
void SetName(string eName), SetPhone(string phoneNum), SetAddress(string addressNum);
void SetHourlyWage(), SetHoursWorked();
void Print() const;
void PrintCheck(double hoursWorked, double hourlyWage);
double calcPay(double hoursWorked, double hourlyWage);
double netIncome(double federalTax, double stateTax);
private:
int employeeNumber;
string name, phone, address;
double hourlyWage, hoursWorked/*,federalTax, stateTax, moneyz*/;
};
#endif
.cpp file
#include "employee.h"
#include <iostream>
#include <iomanip>
using namespace std;
int employeeNumber;
string name;
string address;
string phone;
double hourlyWage;
double hoursWorked;
double PayCheck;
double gross;
double lilBro;
double bigBro;
void employee::SetEmployeeNumber(int num)
{
employeeNumber = num;
return;
}
void employee::SetName(string eName)
{
name = eName;
return;
}
void employee::SetPhone(string phoneNum)
{
phone = phoneNum;
return;
}
void employee::SetAddress(string addressNum)
{
address = addressNum;
return;
}
void employee::SetHourlyWage()
{
}
void employee::SetHoursWorked()
{
}
void employee::Print() const
{
cout << "Employee Name: " << name << endl;
cout << "Employee Number: " << employeeNumber << endl;
cout << "Address: " << address << endl;
cout << "Phone: " << phone << endl;
cout << ' ';
cout << endl;
cout << endl;
return;
}
//Calculates the amount of their paycheck and returns
double employee::calcPay(double hoursWorked, double hourlyWage)
{
if (hoursWorked > 40)
{
gross = ((hoursWorked - 40) * (hourlyWage * 1.5)) + (40 * hourlyWage);
}
else
{
gross = hourlyWage * hoursWorked;
}
return gross;
}
void employee::PrintCheck(double hoursWorked, double hourlyWage)
{
calcPay(hoursWorked, hourlyWage);
cout << fixed << showpoint << setprecision(2);
cout << "Hours worked: " << hoursWorked << " Hourly wage: " << hourlyWage << endl;
}
main
#include <iostream>
#include <iomanip>
using namespace std;
#include "employee.h"
int main() {
employee person1;
person1.SetName("Joe Brown");
person1.SetEmployeeNumber(37);
person1.SetAddress("123 Main St.");
person1.SetPhone("123-6788");
person1.Print();
double gross1 = person1.calcPay(45, 10);
double myMoney = gross1 - (gross1 *.2 ) - (gross1 *.075);
cout << "....................UVU Computer Science Dept................................."<< endl;
cout << "Pay to the order of Joe Brown...................................." << "$" << fixed << showpoint << setprecision(2) << myMoney << endl;
cout << "United Community Credit Union" <<endl;
cout << ".............................................................................."<< endl;
person1.calcPay(45, 10);
person1.PrintCheck(45, 10);
employee person2;
person2.SetName("Sam Jones");
person2.SetEmployeeNumber(37);
person2.SetAddress("45 East State");
person2.SetPhone("661-9000");
person2.Print();
double gross2 = person2.calcPay(30, 12.50);
double myMoney1 = gross2 - (gross2 *.2 ) - (gross2 *.075);
cout << "....................UVU Computer Science Dept................................."<< endl;
cout << "Pay to the order of Sam Jones...................................."<< "$" << fixed << showpoint << setprecision(2) << myMoney1 << endl;
cout << "United Community Credit Union" <<endl;
cout << ".............................................................................."<< endl;
person2.calcPay(30, 12.50);
person2.PrintCheck(30, 12.50);
return 0;
Explanation / Answer
// File name: Employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;
// Class employee definition
class employee
{
public:
// Prototype of member functions
employee();
employee(int, string, string, string, double, double);
// Setter method prototype
void SetEmployeeNumber(int num);
void SetName(string eName);
void SetPhone(string phoneNum);
void SetAddress(string addressNum);
void SetHourlyWage(double);
void SetHoursWorked(double);
// Getter method prototype
int GetEmployeeNumber();
string GetEmployeeName();
string GetPhone();
string GetAddress();
double GetHourlyWage();
double GetHoursWorked();
void Print() const;
void PrintCheck(double hoursWorked, double hourlyWage);
double calcPay(double hoursWorked, double hourlyWage);
double netIncome(double federalTax, double stateTax);
private:
// Data member
int employeeNumber;
string name, phone, address;
double hourlyWage, hoursWorked/*,federalTax, stateTax, moneyz*/;
double gross;
};// End of class
#endif
----------------------------------------------------------------------------
// File name: Employee.cpp
#include "employee.h"
#include <iostream>
#include <iomanip>
using namespace std;
// Default constructor definition
employee::employee()
{
employeeNumber = 0;
name = phone = address = "";
hourlyWage = hoursWorked = gross = 0;
}// End of constructor
// Parameterized constructor definition
employee::employee(int no, string na, string add, string ph, double hWork, double hWage)
{
employeeNumber = no;
name = na;
address = add;
phone = ph;
hourlyWage = hWage;
hoursWorked = hWork;
gross = 0;
}// End of parameterized constructor
// Function to return employee number
int employee::GetEmployeeNumber()
{
return employeeNumber;
}// End of function
// Function to return name
string employee::GetEmployeeName()
{
return name;
}// End of function
// Function to return phone number
string employee::GetPhone()
{
return phone;
}// End of function
// Function to return address
string employee::GetAddress()
{
return address;
}// End of function
// Function to return hourly wage
double employee::GetHourlyWage()
{
return hourlyWage;
}// End of function
// Function to return hourly worked
double employee::GetHoursWorked()
{
return hoursWorked;
}// End of function
// Function to set employee number
void employee::SetEmployeeNumber(int num)
{
employeeNumber = num;
return;
}// End of function
// Function to set name
void employee::SetName(string eName)
{
name = eName;
return;
}// End of function
// Function to set phone number
void employee::SetPhone(string phoneNum)
{
phone = phoneNum;
return;
}// End of function
// Function to set address
void employee::SetAddress(string addressNum)
{
address = addressNum;
return;
}// End of function
// Function to set hourly wage
void employee::SetHourlyWage(double hw)
{
hourlyWage = hw;
}// End of function
// Function to set hourly worked
void employee::SetHoursWorked(double hw)
{
hoursWorked = hw;
}// End of function
// Function to display data
void employee::Print() const
{
cout << "Employee Name: " << name << endl;
cout << "Employee Number: " << employeeNumber << endl;
cout << "Address: " << address << endl;
cout << "Phone: " << phone << endl;
cout << ' ';
cout << endl;
cout << endl;
return;
}// End of function
// Calculates the amount of their paycheck and returns
double employee::calcPay(double hoursWorked, double hourlyWage)
{
if (hoursWorked > 40)
{
gross = ((hoursWorked - 40) * (hourlyWage * 1.5)) + (40 * hourlyWage);
}
else
{
gross = hourlyWage * hoursWorked;
}
return gross;
}// End of function
void employee::PrintCheck(double hoursWorked, double hourlyWage)
{
calcPay(hoursWorked, hourlyWage);
cout << fixed << showpoint << setprecision(2);
cout << "Hours worked: " << hoursWorked << " Hourly wage: " << hourlyWage << endl;
}// End of function
-----------------------------------------------------------------------------
// File name: EmployeeMain.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
using namespace std;
#include "employee.cpp"
// Function to display menu and return user choice
int menu()
{
int choice;
// Displays menu
cout<<" 1 - Create a data file";
cout<<" 2 - Read data from a file and print paychecks.";
cout<<" 3 - Exit";
// Accept user choice
cout<<" Enter your choice: ";
cin>>choice;
// Return choice
return choice;
}// End of function
// Function to accept a file name from the user and read file contents
employee read(ifstream &inFile)
{
// Crates an object using default constructor
employee e;
int no;
double data;
string data1;
// Reads the data from file and stores it in data member using set methods
inFile>>no;
e.SetEmployeeNumber(no);
getline(inFile, data1);
e.SetName(data1);
getline(inFile, data1);
e.SetAddress(data1);
inFile>>data1;
e.SetPhone(data1);
inFile>>data;
e.SetHoursWorked(data);
inFile>>data;
e.SetHourlyWage(data);
// Return s the object
return e;
}// End of function
// Function to write data
void write(ofstream &ofs, employee e)
{
int no;
string data;
double data1;
// Extracts data from object using getter method and writes it into the file
no = e.GetEmployeeNumber();
ofs<<no<<endl;
data = e.GetEmployeeName();
ofs<<data<<endl;
data = e.GetAddress();
ofs<<data<<endl;
data = e.GetPhone();
ofs<<data<<endl;
data1 = e.GetHoursWorked();
ofs<<data1<<endl;
data1 = e.GetHourlyWage();
ofs<<data1<<endl;
}// End of function
// main function definition
int main()
{
// Declares ifstream object to read file
ifstream inFile;
// Declares ofstream object to write file
ofstream outFile;
string fileName;
double gross1, gross2, gross3;
double myMoney1, myMoney2, myMoney3;
// Creates three objects using parameterized constructor
employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 45.00, 10.00);
employee sam(21, "Sam Jones", "45 East State", "661-9000", 30.00, 12.50);
employee mary(15, "Mary Smith", "12 High Street", "401-8900", 40.00, 15.00);
// Loops until user choice is not 3
do
{
// Cass the function to check user choice
switch(menu())
{
// To write file
case 1:
// Accepts file name from the user
cout<<" Enter the file name to write: ";
cin>>fileName;
// Opens the file for writing data to text file
outFile.open (fileName.c_str());
// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!outFile.is_open())
{
// Displays error message
cout<<" Error: Unable to open the file!";
// Exit the program
exit(0);
}// End of if condition
// Calls the function to write object contents to the file
write(outFile, joe);
write(outFile, sam);
write(outFile, mary);
break;
// Read data
case 2:
// Accepts file name from the user
cout<<" Enter the file name to read: ";
cin>>fileName;
// Opens the file for reading data from text file
inFile.open (fileName.c_str());
// Check that file can be opened or not
// is_open() function returns true if a file is open and associated with this stream object.
// Otherwise returns false.
if(!inFile.is_open())
{
// Displays error message
cout<<" Error: Unable to open the file!";
// Exit the program
exit(0);
}// End of if condition
// Calls the function to read data from file
joe = read(inFile);
sam = read(inFile);
mary = read(inFile);
// Calls the function to print data
joe.Print();
// Calls the function to calculate gross
gross1 = joe.calcPay(45, 10);
// Calculates tax
myMoney1 = gross1 - (gross1 *.2 ) - (gross1 *.075);
cout << "....................UVU Computer Science Dept................................."<< endl;
cout << "Pay to the order of Joe Brown...................................." << "$" << fixed << showpoint << setprecision(2) << myMoney1 << endl;
cout << "United Community Credit Union" <<endl;
cout << ".............................................................................."<< endl;
joe.calcPay(45, 10);
joe.PrintCheck(45, 10);
sam.Print();
gross2 = sam.calcPay(30, 12.50);
myMoney2 = gross2 - (gross2 *.2 ) - (gross2 *.075);
cout << "....................UVU Computer Science Dept................................."<< endl;
cout << "Pay to the order of Sam Jones...................................."<< "$" << fixed << showpoint << setprecision(2) << myMoney2 << endl;
cout << "United Community Credit Union" <<endl;
cout << ".............................................................................."<< endl;
sam.calcPay(30, 12.50);
sam.PrintCheck(30, 12.50);
mary.Print();
gross3 = mary.calcPay(30, 12.50);
myMoney3 = gross3 - (gross3 *.2 ) - (gross3 *.075);
cout << "....................UVU Computer Science Dept................................."<< endl;
cout << "Pay to the order of Sam Jones...................................."<< "$" << fixed << showpoint << setprecision(2) << myMoney3 << endl;
cout << "United Community Credit Union" <<endl;
cout << ".............................................................................."<< endl;
mary.calcPay(30, 12.50);
mary.PrintCheck(30, 12.50);
break;
case 3:
exit(0);
default:
cout<<" Invalid choice!";
}// End of switch case
}while(1); // End of do - while loop
return 0;
}// End of main function
Sample Output:
1 - Create a data file
2 - Read data from a file and print paychecks.
3 - Exit
Enter your choice: 1
Enter the file name to write: employee.txt
1 - Create a data file
2 - Read data from a file and print paychecks.
3 - Exit
Enter your choice: 2
Enter the file name to read: employee.txt
Employee Name:
Employee Number: 37
Address: Joe Brown
Phone: 123
....................UVU Computer Science Dept.................................
Pay to the order of Joe Brown....................................$344.38
United Community Credit Union
..............................................................................
Hours worked: 45.00
Hourly wage: 10.00
Employee Name:
Employee Number: 2751056
Address:
Phone:
....................UVU Computer Science Dept.................................
Pay to the order of Sam Jones....................................$271.88
United Community Credit Union
..............................................................................
Hours worked: 30.00
Hourly wage: 12.50
Employee Name:
Employee Number: 2751056
Address:
Phone:
....................UVU Computer Science Dept.................................
Pay to the order of Sam Jones....................................$271.88
United Community Credit Union
..............................................................................
Hours worked: 30.00
Hourly wage: 12.50
1 - Create a data file
2 - Read data from a file and print paychecks.
3 - Exit
Enter your choice: 3
Process returned 0 (0x0) execution time : 12.803 s
Press any key to continue.
File employee.txt contents
37
Joe Brown
123 Main St.
123-6788
45
10
21
Sam Jones
45 East State
661-9000
30
12.5
15
Mary Smith
12 High Street
401-8900
40
15
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.