In: Computer Science
C++
UML is required for this program.
Partial credit will be given in the program if you comment your code and I can see you had the right idea even if it is not working correctly. It is better to write comments for parts of the program you cannot figure out than to write nothing at all.
Read the directions for the program carefully before starting. Make sure and ask if the directions are unclear.
The Rent-a-Pig company rents guinea pigs by the month if you own a single elderly guinea pig and you don’t want to buy any more (not healthy for a guinea pig to get alone). You can rent more than one at a time if you wish.
Create a class called rentAPig that has the following attributes:
The class should have the following overloaded constructors:
Write Get and Set methods for each attribute: model, number of days, and rental rate.
Recommended to have a actualRental function that will take a rentAPig as a refence parameter p and do the input for the number of pigs wanted, check to see if there are enough pigs, and, if so, call the chargeRental of p, print the receipt and return the actual value of the rental. If not enough, print an error and return a zero.
Protype for the function is: double actualRent(rentAPig &p);
Create the program with pig1 and pig2 as the objects that have your current order.
Use a loop to drive a menu to access the pigs and do the rentals and totaling. On quit, print the grandTotal (formatted).
Note: use the data members and get functions wherever the type, price, number of pigs are used. Do NOT hard code the information printed (should be able to change the objects at any time).
Here is a sample run:
Rent-a-Pig
Selection: 1
How many Long hair guinea pigs do you wish to rent? 2
You have rented 2 adorable Long hair guinea pigs
Monthly rate of $15.95 per month each.
Total monthly charge is $31.90
Rent-a-Pig
Selection: 2
How many Short Hair guinea pigs do you wish to rent? 4
Not enough pigs
Rent-a-Pig
Selection: 2
How many Short Hair guinea pigs do you wish to rent? 3
You have rented 3 adorable Short Hair guinea pigs
Monthly rate of $9.98 per month each.
Total monthly charge is $29.94
Rent-a-Pig
Selection: 0
Quitting
Grand total = $61.84
Create your UML for the class rentAPig first and put it in the comments at the top of your program (required). Don't forget your name at the top in comments also.
For the user's output use the iomanip to format the fractional numbers (to only 2 digits to the right of the decimal point for length).
Make sure your program has a header (call it “rentAPig.h”) and an implementation (“rentAPig.cpp”), and a main.
Submit your program via Canvas as a zip file under Final Programming.
Summary :
As part of the solution provided (i) Notes , (ii) UML diagram , (iii) Code for header and cpp file & ( iv) sample output
Notes :
Implemented the rentApig code as per the given logic . provided the uml and output for sample inputs.
Note - input validation is done for both first and 2nd level inputs .
UML :
// rentAPig.h
#include <string>
using namespace std;
#ifndef RENTAPIGG_H_
#define RENTAPIGG_H_
class rentAPig
{
private :
string model ;
int Number;
double Monthly_rent_rate;
public :
rentAPig();
rentAPig(string type, int count , double rate);
void setModel( string model );
void setNumber(int count);
void setRate(double rate);
string getModel() const;
int getCount() const;
double getRate() const;
double chargeRental( int count );
};
double actualRent(rentAPig &p);
#endif /* RENTAPIGG_H_ */
// rentAPig.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "rentAPigg.h"
using namespace std;
rentAPig::rentAPig()
{
this->model = "";
this->Number = 0 ;
this->Monthly_rent_rate = 0.0;
}
rentAPig::rentAPig(string type, int count , double rate)
{
this->model = type ;
if ( count >= 0 )
Number = count ;
else
Number = 0 ;
if ( rate >= 0 )
Monthly_rent_rate = rate;
else
Monthly_rent_rate = 0.0;
}
void rentAPig::setModel( string model )
{
this->model = model;
}
void rentAPig::setNumber(int count)
{
if ( count >= 0 )
{
this->Number = count ;
} else
cout << " Count cannot be set negative value \n";
}
void rentAPig::setRate(double rate)
{
if ( rate >= 0.0 )
Monthly_rent_rate = rate;
else
cout << " Monthly rate cannot be set to negative value \n";
}
string rentAPig::getModel() const
{
return model;
}
int rentAPig::getCount() const
{
return Number;
}
double rentAPig::getRate() const
{
return Monthly_rent_rate;
}
// THis method implements Charge rental and returns the amount of monthly rental
// if the count number of pigs are available for rental otherwise 0
double rentAPig::chargeRental( int count )
{
if ( count <= Number )
{
Number -= count ;
return count * Monthly_rent_rate ;
}
//cout << " Not enought Pigs available for rent \n";
return 0.0;
}
double actualRent(rentAPig &p)
{
int count ;
double amt =0.0;
cout << " How many " << p.getModel() << " guinea pigs do you wish to rent : ";
cin >> count ;
// check if input is > 0
if ( count > 0 )
{
// Get the amout by invoking chargeRental on the pig object
amt = p.chargeRental( count );
if ( amt > 0.0 )
{
cout << "You have rented " << count << " adorable " << p.getModel() << " guinea pigs \n";
cout << " Monthly rate of $" << p.getRate() << " per month each.\n";
cout << " Total monthly charge is $" << amt << "\n";
} else
cout << " Not enough pigs \n";
} else
cout << " Enter a positive number >0 .. try again ..\n";
return amt;
}
int main()
{
rentAPig pig1;
// “Long hair”, 5 pigs, and rate of $15.95.
pig1.setModel("Long hair");
pig1.setNumber(5);
pig1.setRate(15.95);
// Initiate pig2 object with custom constructor.
rentAPig pig2("Short hair",3,9.98);
double grandTotal =0.0;
bool flag = true;
// declare ichoice to capture the input choice
int ichoice ;
while ( flag )
{
// show menu
cout << " Rent-a-Pig \n";
cout << " Pig1 : " << pig1.getModel() << "\n";
cout << " Pig2 : " << pig2.getModel() << "\n";
cout << " quit \n";
cout << " Selection :";
cin >> ichoice ;
// Based on choice provided take the appropriate action
if ( ichoice == 0 )
{
cout << "Quitting\n";
break;
}
else if ( ichoice == 1 )
grandTotal += actualRent(pig1);
else if ( ichoice == 2)
grandTotal += actualRent(pig2);
else
cout << "Invalid input try again ..\n";
}
cout << " Grand Total : " << grandTotal << "\n";
}
Sample output :