$29
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 <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:
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 <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.
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
$344.38
Brown....................................
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
$435.00
Smith....................................
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