C++ Employee Class Project #2 For a class I have to take a program I previously
ID: 3804868 • Letter: C
Question
C++ Employee Class Project #2
For a class I have to take a program I previously created and adapt it to use read and write functions. I have the prgraom working in its current state but Im a little lost as to where I am supposed to go from here.
Here is my .cpp
http://pastebin.com/5DFDq0ep
Here is my .h
http://pastebin.com/EVR0R7K8
Here are the assignment instructions provided by my instructor
The Employee Class
For this project you need to add the following functions to your Employee class.
- read(ifstream&)
- write(ofstream&)
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
Your driver will contain a main( ) function that does the following. You may use this description as your pseudo-code:
1. 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.
- Exit.
2. 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.
- Exit.
- 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 in accordance with course standards. Check to make sure you have included the following:
1. A PDF of your class diagram.
2. The complete source code for your Employee class (employee.h and employee.cpp)
3. The source code for your driver.
4. A screen shot of your execution results.
Users 10002705 DocumentsServer Backup 1410Newprojects projecto4projo4Debugprojo4.... le E0 CM This program has two options: Create a data file or 2 Read data from a file and print paychecks. Please enter (1) to create a file or X2> to print checksExplanation / Answer
PROGRAM CODE:
/*
* Employee.cpp
*
* Created on: 26-Mar-2017
* Author: kasturi
*/
#include <iostream>
#include <string>
#include <fstream>
#include "Employee.h"
#include <iomanip>
using namespace std;
Employee::Employee()
{
employeeNumber = 0; // Employee's employee number
employeeName = ""; //Employee's name
streetAddress = ""; //Employee's street address
phoneNumber = ""; //Employee's phone number
hourlyWage = 0; //Employee's hourly wage
hoursWorked = 0;
grossPay = 0;
netPay = 0;
}
Employee::Employee(int empNum, string empName, string streetAddress, string phoneNumber, double hourlyWage, double hoursWorked)
{
employeeNumber = empNum;
employeeName = empName;
this->streetAddress = streetAddress;
this->phoneNumber = phoneNumber;
this->hourlyWage = hourlyWage;
this->hoursWorked = hoursWorked;
grossPay = 0;
netPay = 0;
}
int Employee::getEmployeeNumber()
{
return employeeNumber;
}
void Employee::setEmployeeNumber(int empNum)
{
employeeNumber = empNum;
}
string Employee::getEmployeeName()
{
return employeeName;
}
void Employee::setEmployeeName(string empName)
{
employeeName = empName;
}
string Employee::getStreetAddress()
{
return streetAddress;
}
void Employee::setStreetAddress(string strtAddrs)
{
streetAddress = strtAddrs;
}
string Employee::getPhoneNumber()
{
return phoneNumber;
}
void Employee::setPhoneNumber(string phnNum)
{
phoneNumber = phnNum;
}
double Employee::getHourlyWage()
{
return hourlyWage;
}
void Employee::setHourlyWage(double hrWage)
{
hourlyWage = hrWage;
}
double Employee::getHoursWorked()
{
return hoursWorked;
}
void Employee::setHoursWorked(double hrWorked)
{
hoursWorked = hrWorked;
}
void printCheck(Employee ee)
{
cout << " --------------------- Fluff Shuffle Electronics -------------------------------- ";
cout << " Pay to the order of " << ee.getEmployeeName() << "...........................$" << ee.calcPay();
cout << " United Bank of Eastern Orem ";
cout << "------------------------------------------------------------------------------- ";
cout << " Hours Worked: " << ee.getHoursWorked();
cout << " Hourly Wage: " << ee.getHourlyWage();
}//End of function
void read(ifstream &in)
{
Employee employees[10];
int counter = 0;
while( in.read ((char *)&employees[counter++], sizeof(Employee)))
for(int i=0; i<counter; i++)
{
printCheck(employees[i]);
}
in.close();
}
void write(ofstream &out)
{
// Instantiate your employees here first, then call their functions.
Employee emp1(111, "Steve Jobs", "77 N 3484 E", "1234556785", 10.00, 45.00);
printCheck(emp1);
Employee emp2(222, "Bill Gates", "239 N 5103 E", "8835556788", 12.50, 30.00);
printCheck(emp2);
out.write ((char *)(&emp1), sizeof(Employee));
out.write ((char *)(&emp2), sizeof(Employee));
out.close();
}
//Main function
int main()
{
int choice;
string filename;
while(true)
{
cout<<" This program has two options: ";
cout<<"1 - Create a data file, or ";
cout<<"2 - Read data from a file and print paychecks ";
cout << " Press any other key to quit.......... ";
cout<<"Please enter <1> to create a file or <2> to print checks: ";
cin>>choice;
if(choice == 1)
{
cout<<"Enter the file name: ";
cin>>filename;
ofstream out(filename);
out.open (filename.c_str(), ios::binary);
write(out);
}
else if(choice == 2)
{
cout<<"Enter the file name: ";
cin>>filename;
ifstream in(filename);
in.open (filename.c_str(), ios::binary);
read(in);
}
else break;
//Calls function to displays information
}
}//End of main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.