Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please include a class diagram. Change the database.cpp program to throw exceptions when there is an unexpected problem. When appropriate offer the user an option
please include a class diagram.
Change the database.cpp program to throw exceptions when there is an unexpected problem. When appropriate offer the user an option to fix the problem.
Database.cpp---------------------------
#include
#include
#include
#include
using namespace std;
#include "ccc_empl.h"
const int NEWLINE_LENGTH = 2;
const int RECORD_SIZE = 30 + 10 + NEWLINE_LENGTH;
/**
Converts a string to a floating-point value, e.g.
"3.14" -> 3.14.
@param s a string representing a floating-point value
@return the equivalent floating-point value
*/
double string_to_double(string s)
{
istringstream instr(s);
double x;
instr >> x;
return x;
}
/**
Raises an employee salary.
@param e employee receiving raise
@param percent the percentage of the raise
*/
void raise_salary(Employee& e, double percent)
{
double new_salary = e.get_salary() * (1 + percent / 100);
e.set_salary(new_salary);
}
/**
Reads an employee record from a file.
@param e filled with the employee
@param in the stream to read from
*/
void read_employee(Employee& e, istream& in)
{
string line;
getline(in, line);
if (in.fail()) return;
string name = line.substr(0, 30);
double salary = string_to_double(line.substr(30, 10));
e = Employee(name, salary);
}
/**
Writes an employee record to a stream.
@param e the employee record to write
@param out the stream to write to
*/
void write_employee(Employee e, ostream& out)
{
out << e.get_name()
<< setw(10 + (30 - e.get_name().length()))
<< fixed << setprecision(2)
<< e.get_salary();
}
int main()
{
cout << "Please enter the data file name: ";
string filename;
cin >> filename;
fstream fs;
fs.open(filename.c_str());
fs.seekg(0, ios::end); // Go to end of file
int nrecord = fs.tellg() / RECORD_SIZE;
cout << "Please enter the record to update: (0 - "
<< nrecord - 1 << ") ";
int pos;
cin >> pos;
const double SALARY_CHANGE = 5.0;
Employee e;
fs.seekg(pos * RECORD_SIZE, ios::beg);
read_employee(e, fs);
raise_salary(e, SALARY_CHANGE);
cout << "New salary: " << e.get_salary();
fs.seekp(pos * RECORD_SIZE, ios::beg);
write_employee(e, fs);
fs.close();
return 0;
}
Ccc_empl.h--------------------------------
#ifndef CCC_EMPL_H
#define CCC_EMPL_H
#include
using namespace std;
/**
A basic employee class that is used in many examples
in the book "Computing Concepts with C++ Essentials"
*/
class Employee
{
public:
/**
Constructs an employee with empty name and no salary.
*/
Employee();
/**
Constructs an employee with a given name and salary.
@param employee_name the employee name
@param initial_salary the initial salary
*/
Employee(string employee_name, double initial_salary);
/**
Sets the salary of this employee.
@param new_salary the new salary value
*/
void set_salary(double new_salary);
/**
Gets the salary of this employee.
@return the current salary
*/
double get_salary() const;
/**
Gets the name of this employee.
@return the employee name
*/
string get_name() const;
private:
string name;
double salary;
};
#endif
Ccc_empl.cpp------------------------------
#include "ccc_empl.h"
Employee::Employee()
{
salary = 0;
}
Employee::Employee(string employee_name, double initial_salary)
{
name = employee_name;
salary = initial_salary;
}
void Employee::set_salary(double new_salary)
{
salary = new_salary;
}
double Employee::get_salary() const
{
return salary;
}
string Employee::get_name() const
{
return name;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started