Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

10.5 Programming Project #5: File I/O Background Recall that we are iterating on the development of a payroll program. At this point, you should have

10.5 Programming Project #5: File I/O

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.

Employee read(ifstream&) void write(ofstream&) 

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 ) 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:

This program has two options: 1 - Create a data file, or 2 - Read data from a file and print paychecks. Please enter <1> to create a file or <2> to 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 ) 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.

This program has two options: 1 - Create a data file, or 2 - Read data from a file and print paychecks. Please enter (1) to create a file or (2) to print checks:1 Please enter a file name: employee.dat Data file created ... you can now run option 2. This program has two options: 1 - Create a data file, or 2 - Read data from a file and print paychecks. Please enter (1) to create a file or (2) to print checks:2 Please enter a file name: employee.dat ....................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 ....................UVU Computer Science Dept................................. Pay to the order of Sam Jones....................................$261.00 United Community Credit Union .............................................................................. Hours worked: 30.00 Hourly wage: 12.00 ....................UVU Computer Science Dept................................. Pay to the order of Mary Smith....................................$435.00 United Community Credit Union .............................................................................. Hours worked: 40.00 Hourly wage: 15.00 This program has two options: 1 - Create a data file, or 2 - Read data from a file and print paychecks. Please enter (1) to create a file or (2) to print checks:2 Please enter a file name: foo.dat Couldn't open file for input

// Working program i wrote

//main

#include "employee.h" #include #include using namespace std;

void PrintCalc(Employee a) { cout << "Employee Name: "<< a.getName() << endl; cout << "Employee Number: " << a.getENum() << endl; cout << "Address: " << a.getAdd() << endl; cout << "Phone: " << a.getPhone() << endl; cout << "....................UVU COMPUTER SCIENCE Dept................................. "; cout << "Pay to the order of " << a.getName() << ".................................... $" << fixed << setprecision(2) << a.calcPay(a.getWage(), a.getHours()) << endl << endl; cout << "United Community Credit Union "; cout << ".............................................................................. "; cout << "Hours worked: " << fixed << setprecision(2) << a.getHours() << endl; cout << "Hourly wage: " << a.getWage() << endl << endl; }

int main(int argc, const char * argv[]) { // insert code here... Employee a( 37, "Joe Brown", "123 Main St.", "123-6788", 10.00, 45); Employee b( 37, "Sam Jones", "45 East State", "661-9000", 12.50, 30 ); PrintCalc(a); PrintCalc(b); //ifstream file; // a.read(file); return 0; }

//employee.cpp

#include "employee.h" #include #include using namespace std;

Employee::Employee(int en, std::string n, std::string a, std::string pn, double hw, double hwo) { this->employeeNum = en; this->name = n; this->address = a; this->phoneNum = pn; this->hrWage = hw; this->hrWorked = hwo; }

string Employee::getName() { return name; }

void Employee::setName(string n) { this->name = n; }

int Employee::getENum() { return employeeNum; }

string Employee::getAdd() { return address; }

void Employee::setAdd(string a) { this->address = a; }

string Employee::getPhone() { return phoneNum; }

void Employee::setPhone(string p) { this->phoneNum = p; }

double Employee::getWage() { return hrWage; }

void Employee::setWage(double w) { this->hrWage = w; }

double Employee::getHours() { return hrWorked; }

void Employee::setHours(double h) { this->hrWorked = h; }

double Employee::calcPay(double a, double b) { const double x = 40.00; double gross; if(b > 40) { b -= x; gross = (a * x) + ((a + (a / 2)) * b); } else { gross = a * b; } double net = gross - (gross * .2) - (gross * .075); return net; }

int Employee::read(ifstream &n){ cout << "Type in name of file: "; string x; cin >> x; n.open(x); string output; if (n.is_open()) { while(!n.eof()) { n >> output; cout << output; } } return 0; }

//employee.h

#include #include #include

class Employee { private: int employeeNum; std::string name; std::string address; std::string phoneNum; double hrWage, hrWorked; public: Employee(int en, std::string n, std::string a, std::string pn, double hw, double hwo); std::string getName(); void setName(std::string n); int getENum(); std::string getAdd(); void setAdd(std::string a); std::string getPhone(); void setPhone(std::string p); double getWage(); void setWage(double w); double getHours(); void setHours(double h); double calcPay(double a, double b); int read(std::ifstream& n); int write(std::ofstream p); };

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

More Books

Students also viewed these Databases questions

Question

What does the parameter multiplied by a dummy variable express?

Answered: 1 week ago