1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
// File Prologue
// Name: Nathan Kelsey
// CS 1410 Section 001
// Project: Project 08
// Date: 03/01/2013
//
// I declare that the following code was written by me or provided
// by the instructor for this project. I understand that copying source
// code from any other source constitutes cheating, and that I will receive
// a zero on this project if I am found in violation of this policy.
#include "EmployeeClass.h"
#include "Salaried.h"
#include "Hourly.h"
#include "MyVector.h"
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
const int ONE = 1;
const int TWO = 2;
const int THREE = 3;
const int SIZE = 4;
//variables
int answer = 0;
string fileName;
string response;
bool exitLoop = true;
// printCheckSalaried function prologue
// Purpose: Display necessary employee data for a paycheck/pay stub
// Parameters: EmployeeClass object passed as const reference
// Returns: None
void printCheckSalary(const Salaried&);
// printCheckHourly function prologue
// Purpose: Display necessary employee data for a paycheck/pay stub
// Parameters: Hourly object passed as const reference
// Returns: None
void printCheckHourly(const Hourly&);
int main()
{
// Fix Numerical Output data to two places after the decimal
cout.setf(ios::fixed);
cout.precision(2);
// Create an array of Employee Pointers
MyVector<EmployeeClass*> myEmployees;
// Opening User Interface
cout <<"This program has two functions." << endl;
cout <<"Press 1 to create a data file." << endl;
cout <<"Press 2 to read from the file and print paychecks." << endl;
cout <<"Input: ";
cin >> answer;
// User enters one, then execute following code
if(answer == ONE)
{
ofstream employeeInput(fileName.c_str());
do
{
// Open file for output
//Create fstream object
startover:
cout << "Please enter a file name:";
cin.ignore();
cin >> fileName;
employeeInput.open(fileName.c_str());
if(employeeInput)
{
exitLoop = false;
break;
}
if(employeeInput.fail())//Check to see if the file opened
{
cout << "\nInvalid file name:" << endl;
cout << "\nWould you like to try again? ";
cin.ignore();
cin >> response;
}
while (response.compare("Y") != 0 && response.compare("y") != 0 && response.compare("n") != 0 && response.compare("N") != 0);
{
if (response == "y" || response == "Y")
{
goto startover;
}
else
{
cout << "\nThank you and Goodbye." << endl;
system("PAUSE");
exit(1);
}
}
}while(exitLoop);
// write the data
//The only place you have to worry about throwing an exception in this assignment is when you find that you cannot read data from the file.
myEmployees.push_back(new Hourly(1, "H. Potter", "Privet Drive", "201-9090", 40, 12.00));
myEmployees.push_back(new Salaried(2, "A. Dumbledore", "Hogewarts", "803-1230", 1200));
myEmployees.push_back(new Hourly(3, "R. Weasley", "The Burrow", "892-2000", 40, 10.00));
myEmployees.push_back(new Salaried(4, "R. Hagrid", "Hogwarts", "910-8765", 1000));
try
{
for(int i = 0; i < myEmployees.size(); i++)
{
// Utilizing pointer array, write the data to the file
myEmployees.at(vectorData)->writeData(employeeInput);
}
}
catch(EmployeeExceptions e)
{
cout <<" \n\nThere was a file error while writing employee data";
cout << "\nto the file. The data may not be saved correctly.";
cout << "\nCheck your output file";
}
cout << "\nWrite Successful!!\n" << endl;
employeeInput.close( );
for(int i = 0; i < SIZE; i++)
{
delete myEmployees.size();
myEmployees.at(vectorData) = NULL;
}
system("PAUSE");
}
if (answer == TWO)
{
//Create an object of the required kind, Hourly or Salaried.
//Save the pointer to this object in the new array of Employee pointers.
//Have the object read its data from the file.
//Repeat this for each set of data in the file.
ifstream employeeData(fileName.c_str());
do
{
startAgain:
cout << "\nPlease enter in a filename: ";
cin.ignore();
cin >> fileName;
employeeData.open(fileName.c_str());
if (employeeData)
{
exitLoop = false;
goto readData;
break;
}
if (employeeData.fail())
{
do
{
cout << "\nInvalid file name:" << endl;
cout << "\nWould you like to try again? ";
cin.ignore();
cin >> response;
}while (response.compare("Y") != 0 && response.compare("y") != 0 && response.compare("n") != 0 && response.compare("N") != 0);
if (response == "y" || response == "Y")
{
goto startAgain;
}
else
{
cout << "\nThank you and Goodbye."<< endl;
system("PAUSE");
exit(1);
}
}
} while (exitLoop);
readData:
string type;
int index = 0;
try
{
while(employeeData >> type)
{
if(type == "Hourly")
{
myEmployees.at(vectorData) = new Hourly();
}
else
{
myEmployees.at(vectorData) = new Salaried();
}
myEmployees.at(vectorData)->readData(employeeData);
index++;
}
}
// Catch if Read Failed
catch(EmployeeExceptions e)
{
if (e.getCode( ) == 1 || e.getCode( ) == 2)
{
cout << "\nError reading data, terminating program.";
system("PAUSE");
exit(1);
}
if (e.getCode( ) == 3)
{
cout << "\nUnexpected end of file, terminating program.";
system("PAUSE");
exit(1);
}
}
int count = index;
// if read was successful
// call respective printCheck()
for(int i = 0; i < count; i++)
{
system("CLS");
EmployeeClass* employeePtr = myEmployees.at(vectorData);
if(typeid( *myEmployees.at(vectorData)) == typeid(Hourly))
{
Hourly* hrlyPtr = dynamic_cast<Hourly*>(employeePtr);
cout<<"\n------------------FluffShuffle Electronics------------------------\n";
cout<<"\nPay to the order of "<<hrlyPtr->getEmployeeName()<<"..............................$"<<hrlyPtr->getCalcPay();
cout<<"\n\nGlobal Bank of Computer Science Theory";
cout<<"\n------------------------------------------------------------------\n";
cout<<"Hours worked: "<< hrlyPtr->getWeeklyHours() <<"\n";
cout<<"Hourly Wage: "<< hrlyPtr->getHourlyWage() << "\n" << endl;
}
else if(typeid( *myEmployees.at(vectorData)) == typeid(Salaried))
{
Salaried* slryPtr = dynamic_cast<Salaried*>(employeePtr);
cout<<"\n------------------FluffShuffle Electronics------------------------\n";
cout<<"\nPay to the order of "<<slryPtr->getEmployeeName()<<"..............................$"<<slryPtr->getCalcPay();
cout<<"\n\nGlobal Bank of Computer Science Theory";
cout<<"\n------------------------------------------------------------------\n";
cout<<"Weekly Salary: " <<slryPtr->getSalary() << endl;
}
system("PAUSE");
}
employeeData.close();//close the read file
}
return 0;
}
|