Create a Job class that holds a Job ID number and the cost of the Job. Include insertion
and extraction operators. Create a JobException class that holds a Job and an error message.
When the user enters Job data, if the Job fee is below $250, then create a JobException
object and throw it. Write a main()function that declares an array of eight Job objects.
If a JobException object is thrown during the data entry for any Job, require the user
to enter data for a new Job, and replace the invalid Job. Save the file as Job.cpp.
1
Expert's answer
2015-02-13T12:46:22-0500
#include <stdlib.h> #include <iostream> #include <stdio.h> #include <cstdlib> #include <string> using namespace std; class Jobs{ public: int idNumber; double cost; Jobs() {idNumber = 0; cost = 0;}; Jobs(int _idNumber, int _cost){idNumber = _idNumber; cost = _cost;} }; class JobException{ public: Jobs job; string message; JobException(Jobs _job, string _message = "Error!!!Cost of the job can not be below 250$!") { job = _job; message = _message; } }; // insertion ostream & operator<<(ostream &stream;, Jobs obj) { stream <<"----------------------------------\n"<<endl; stream <<" id number = "<< obj.idNumber << "\n "; stream <<"cost = "<<obj.cost << "\n"; stream <<"----------------------------------\n"<<endl; return stream; } // extraction istream &operator;>>(istream &stream;, Jobs &obj;) { cout << "Enter cost of the Job: "; stream >> obj.cost; if (obj.cost < 250) throw JobException(obj); return stream; } int main(){ Jobs jobs[8]; for (int i = 0; i < 8; i++) { jobs[i].idNumber = i; try{ cin>>jobs[i]; } catch (JobException jobExp){ cout<<jobExp.message<<endl; --i; } } cout<<endl<<endl<<"Jobs: "<<endl; for (int i = 0; i < 8; i++) cout<<jobs[i]; system("pause"); return 0; }
Comments
Leave a comment