Practice

Cashable Vacation


Vacation

Your company needs a program to track the vacation time that its employees accumulate and has asked you to design and to code a class named Vacation that holds vacation data for a single employee. 

Upon instantiation, a Vacation object accepts either no arguments or two arguments.  If the object accepts arguments, the first one is an integer that holds the number of the employee.  This number is positive-valued and contains no more than 6 digits.  The second argument is a C-style null-terminated string that holds the name of the employee.  This string may contain any number of characters without limitation, except that it is not an empty string.  If the object receives no data or unacceptable data - either an invalid number or an empty string as a name - the object stores a safe empty state. 

Your design includes the following member functions:

  • Queries:
    • valid() - returns true if the object's data is valid; false if the object holds a safe empty state
    • name() - returns the address of a string that contains the employee's name if the object's data is valid; NULL otherwise
    • number() - returns the employee's number if the object's data is valid; 0 otherwise
    • time() - returns the vacation time that the employee has accumulated if the object's data is valid; 0 otherwise
  • Modifiers:
    • += operator - takes a floating-point value as its right operand and adds this value to the accumulated vacation time if the value is positive and the object's data is valid; does nothing otherwise.  This function returns true if successful, false otherwise.
    • -= operator - takes a floating-point value as its right operand and subtracts this value from the accumulated vacation time if the value is positive, the value is not greater than the accumulated vacation time and the object's data is valid; does nothing otherwise.  This function returns true if successful, false otherwise.

Your design also overloads the insertion operator (<<) for displaying vacation data on an output stream.  The data fields occupy a single line in the following order: the employee number in a field of 6 with 0 padding, the accumulated vacation time in a field of 12 with 2 decimal places, and the employee name preceded by a single space.  Refer to the sample output listed above to clarify this description.  Note that in the case where the object holds a safe empty state, this operator simply displays: no data available

Since the employee's name may contain any number of characters, your design must use dynamic memory.  Hence, the code includes a copy constructor, an assignment operator, and a destructor.  [For those who wish to use the string class in the C++ library, please note that this class is not implemented on your company's platform.]

Consider the following application that will use your class:

 #include <iostream>
 using namespace std;
 #include "Vacation.h"

 int main() {

     Vacation jim(345, "Jim"), ghost;

     // Output heading
     cout << "Number Accumulated Name" << endl;

     // should show that Jim starts with nothing
     cout << jim << endl;

     // Jim accumulates 6.5 hours of vacation time
     jim += 6.5;
     cout << jim << endl;

     // Jim accumulates 4.5 more hours of vacation time
     jim += 4.5;
     cout << jim << endl;

     // Jim takes 5 hours of vacation
     jim -= 5;
     cout << jim << endl;

     // data for the ghost employee
     cout << endl << ghost << endl;

     return 0;
 }

This application will produce the following output:

 Number Accumulated Name
 000345        0.00 Jim
 000345        6.50 Jim
 000345       11.00 Jim
 000345        6.00 Jim

 no data available

CashableVacation

Your company has decided to let its employees cash out their accumulated vacation time.  To implement this feature, you are to derive a class named CashableVacation from the Vacation class described above. 

Upon instantiation, a CashableVacation object accepts either no arguments or three arguments.  If the object accepts arguments, the first one is an integer that holds the employee's number.  This number is positive-valued and contains no more than 6 digits.  The second argument is a C-style null-terminated string that holds the employee's name.  This string may contain any positive number of characters without limitation.  It may not be an empty string.  The third argument is a floating-point number that holds the rate for converting vacation time to cash.  The provincial minimum wage is defined in symbolic constant MIN_WAGE.  If the object receives no data or unacceptable data - either an invalid number, empty name, or a rate less than MIN_WAGE - the object stores a safe empty state.

Your design includes the following member functions:

  • Queries:
    • valid() - returns true if the object's data is valid; false if the object holds a safe empty state
    • cashable() - returns the cash left from the vacation time that the company converted into cash if the object's data is valid; 0 otherwise
    • conversionRate() - returns the rate for converting vacation time into cash if the object's data is valid; 0 otherwise
  • Modifiers:
    • -= operator - takes a floating-point value as its right operand and subtracts this value from the cash available for withdrawal if the value is positive, the value is not greater than the cash available and the object's data is valid; does nothing otherwise.  This function returns true if successful, false otherwise.
    • makeCashable(double) - takes a floating-point value as the amount of vacation time to convert into cash and converts this amount into cash for future withdrawal by the employee if the value received is positive and not greater than the accumulated vacation time; does nothing otherwise.  This function does not return anything.

Your design also overloads the insertion operator (<<) for displaying vacation data on an output stream.  The data fields occupy a single line in the following order: the employee number in a field of 6 with 0 padding; the accumulated vacation time in a field of 12 with 2 decimal places; the conversion rate in a field of 6 with 2 decimal places; the cash that is available for withdrawals in a field of 9 with 2 decimal places.  Refer to the sample listed above to clarify this description.  Note that in the case where the object holds a safe state, this operator simply displays: no data available

Consider the following application that will use your derived class:

 #include <iostream>
 using namespace std;
 #include "CashableVacation.h"

 int main() {

     CashableVacation jim(345, "Jim", 8.50), ghost;

     cout << "Number Accumulated  Rate Cashable Name" << endl;

     // should show that Jim starts with nothing
     cout << jim << endl;

     // Jim accumulates 6.5 hours of vacation time
     jim += 6.5;
     cout << jim << endl;

     // Jim accumulates 4.5 more hours of vacation time
     jim += 4.5;
     cout << jim << endl;

     // The company makes 10 hours of vacation time cashable
     jim.makeCashable(10);
     cout << jim << endl;

     // Jim withdraws $40
     jim -= 40.0;
     cout << jim << endl;

     // data for ghost employee
     cout << endl << ghost << endl;

     return 0;
 }

This application generates the following output:

 Number Accumulated  Rate Cashable Name
 000345        0.00  8.50     0.00 Jim
 000345        6.50  8.50     0.00 Jim
 000345       11.00  8.50     0.00 Jim
 000345        1.00  8.50    85.00 Jim
 000345        1.00  8.50    45.00 Jim

 no data available






  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License