Project #5 Solution

$30.00 $24.90



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 read themselves 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.


Continue to refine your object oriented design skills.
Give you practice drawing a class diagram.
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 driver program to test the additions to your class.
The Employee Class

For this project you need to add the following functions to your Employee class.

Note that read is a static member function. It returns an Employee object based on the data it reads from a file. If there is an read error, it throws a std::runtime_error exception with an appropriate message.

The Driver

The major function of the driver is to test the additions to your Employee class. Keep the printCheck( ) function from your previous project. The output of your program will look similar to the output of your previous project.

Your driver will contain a main( ) function that does the following. You may use this description as your pseudo-code:

Presents 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, 10.00);
Employee sam(21, “Sam Jones”, “45 East State”, “661-9000”, 30, 12.00);
Employee mary(15, “Mary Smith”, “12 High Street”, “401-8900”, 40, 15.00);
Send messages to each of the three Employee objects to write themselves out to the file.
Print an message that creation of the file is complete.
If the user selects the second option, your program should
Prompt for the name of the file that you just saved.
Call Employee::read to read in the objects written in step 1.
Call the printCheck( ) function for each of the three new objects, just as you did in the previous project.
Run this 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.
Submitting Your Assignment

After you are satisfied that your program works correctly, submit it to Canvas as project #5. Create a zip file for this project and include the following:

A PDF of your class diagram.
The complete source code for your Employee class (employee.h and employee.cpp)
The source code for your driver.
A screen shot of your execution results.
Grading Guidelines

Description Points possible Your points
Assignment meets the submission guidelines.
Source code files contain a declaration that you did not copy any code.
Project meets style guidelines.
All source code files contain a file prologue
All methods have a complete method prologue
Code is properly documented
Submission includes a properly drawn class diagram for your Employee class.

The Employee class contains a read( ) and a write( ) function that make the class persistent, as described above.

Your program provides a menu and responds to the option input by the user as described above.

Your program successfully creates a file that contains the data from the three Employee objects that you created.

Your program creates three new Employee objects and reads their data in from the file.

Your program correctly handles file I/O errors. Check that the output file opened without error for Option 1, and that the file opened without error for input in Option 2.

-Late penalty (-20% per day)

Total 30