SO i have taken a couple of hacks at this and am completely lost any direction w
ID: 3731400 • Letter: S
Question
SO i have taken a couple of hacks at this and am completely lost any direction would be appreciated
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.
Submitting Your Assignment
Submit your 3 source files, just as you did in the previous project.
Sample execution follows. Note that only the checks are printed.
Explanation / Answer
//main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include "employee.h"
using namespace std;
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
const int> const int TWO = 2;
int userInput;
string fileName;
string emplyeeInfo;
ifstream inputFS;
cout << "This program has two options :" << ' ';
cout << "1 - Create a data file, or" << ' ';
cout << "2 - Read data from a file and print paychecks." << ' ';
cout << "Please enter (1) to create a file or (2) to print checks :"<< endl;
cin >> userInput;
//Create employees using the defualt constructor
Employee joe(37, 10.00, 45.00, "Joe Brown", "123 Main St.", "123-6788");
Employee sam(21, 12.00, 30.00, "Sam Jones", "45 East State", "661-9000");
Employee mary(15, 15.00, 40.00, "Mary Smith", "12 High Street", "401-8900");
//takes the information and passes it to the employee class. Will write the file.
if(userInput == ONE)
{
cout << "Please enter a file name: ";
cin >> fileName;
cout << endl;
ofstream outputFile;
outputFile.open(fileName.data());
if(!outputFile.is_open())
{
cout << "Could not open file." << endl;
return 0;
}
joe.write(outputFile);
sam.write(outputFile);
mary.write(outputFile);
cout << "Data file created ... you can now run option 2.";
}
else if(userInput == TWO)
{
cout << "Please enter a file name: " << endl;
cin >> fileName;
ifstream inputFile;
inputFile.open(fileName.data());
if(!inputFile.is_open())
{
cout << "Couldn't open file for input" << endl;
return 0;
}
Employee::read(inputFile);
joe.printCheck();
sam.printCheck();
mary.printCheck();
}
else
{
cout << "Invalid input." << endl;
}
}
--------------------------------------------------------------------
//employee.cpp
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "employee.h"
using namespace std;
//Default Constructor
Employee::Employee()
{
this->name = "";
this->address = "";
this->phone = "";
this->employeeNumber = 0;
this->hourlyWage = 0;
this->hoursWorked = 0;
}
//Parameterized Constructor
Employee::Employee(int employeeNumber, double hourlyWage, double hoursWorked, string name, string address, string phone)
{
this->name = name;
this->address = address;
this->phone = phone;
this->employeeNumber = employeeNumber;
this->hourlyWage = hourlyWage;
this->hoursWorked = hoursWorked;
}
//Destructor
/*
Employee::~Employee()
{
}
*/
//Getters code
string Employee::getName()
const {
return name;
}
string Employee::getAddress()
const {
return address;
}
string Employee::getPhone()
const {
return phone;
}
int Employee::getEmployeeNumber()
const {
return employeeNumber;
}
double Employee::getHourlyWage()
const {
return hourlyWage;
}
double Employee::getHoursWorked()
const {
return hoursWorked;
}
//Setters code
/*
void setName(string name)
{
this->name = name;
}
void setAddress(string address)
{
this-> address = address;
}
void setPhone(string phone)
{
this-> phone = phone;
}
void setHourlyWage(double hourlyWage)
{
this-> hourlyWage = hourlyWage;
}
void setHoursWorked(double hoursWorked)
{
this-> hoursWorked = hoursWorked;
}
*/
//Functions code
double Employee::calcPay()
{
double netPay = 0; //amount one receives after taxes
double grossPay = 0; //money you've made while working at your job, figured before any deductions are taken
double overtimeHours = 0;
double overtimeTotal = 0;
double overtime = 1.5; //Multiply your standard rate by 1.5
double federalTax = .2;
double stateTax = .075;
int fullTimeHours = 40;//how many hours required to work to be full time.
if (hoursWorked > fullTimeHours)
{
overtimeHours = (hoursWorked - fullTimeHours);//hours of overtime that were worked.
overtimeTotal = overtimeHours * (hourlyWage * overtime);//How much they get paid for working overtime
//finding your gross pay before deductions
grossPay = fullTimeHours * hourlyWage + overtimeTotal;
//finding take home pay after deductions
netPay = grossPay - (federalTax * grossPay) - (stateTax * grossPay);
return netPay;
}
else
{
//finding your gross pay before deductions
grossPay = hoursWorked * hourlyWage;
//finding take home pay after deductions
netPay = grossPay - (federalTax * grossPay) - (stateTax * grossPay);
return netPay;
}
}
void Employee::printCheck()
{
cout << endl;
cout << "....................UVU Computer Science Dept................................." << endl;
cout << endl;
cout << "Pay to the order of " << getName() << "....................................$" << calcPay() << endl;
cout << endl;
cout << endl;
cout << "United Community Credit Union" << endl;
cout << ".............................................................................." << endl;
cout << endl;
cout << "Hours worked: " << getHoursWorked() << endl;
cout << "Hourly wage: " << getHourlyWage() << endl;
cout << endl;
cout << endl;
}
//replaced Employee with void
void Employee::read(ifstream& inputStream)
{
cout << endl;
string joe;
getline(inputStream, joe);
string sam;
getline(inputStream, sam);
string mary;
getline(inputStream, mary);
}
void Employee::write(ofstream& outputStream)
{
outputStream << this-> employeeNumber << ",";
outputStream << this-> getName() << ",";
outputStream << this-> getAddress() << ",";
outputStream << this-> getPhone() << ",";
outputStream << this-> getHourlyWage() << ",";
outputStream << this-> getHoursWorked() << ",";
outputStream << endl;
}
---------------------------------------------------------------------------------------------------
//employee.h
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
//class declaration
class Employee
{
private:
int employeeNumber;
string name;
string address;
string phone;
double hourlyWage;
double hoursWorked;
public:
Employee ();//default construtor
Employee(int employeeNumber, double hourlyWage, double hoursWorked, string name, string address, string phone);
//~Empoyee();
//Getters
string getName() const;
string getAddress() const;
string getPhone() const;
int getEmployeeNumber() const;
double getHourlyWage() const;
double getHoursWorked() const;
//Setters
/*
void setName(string name);
void setAddress(string address);
void setPhone(string phone);
void setHourlyWage(double hourlyWage);
void setHoursWorked(double hoursWorked);
*/
//Functions
double calcPay();
void printCheck();
//replaced Employee with static void
static void read(ifstream&);
void write(ofstream&);
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.