Question

In: Computer Science

C++ UML is required for this program. Partial credit will be given in the program if...

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:

  • Guinea Pig type (string)
  • Number pigs available
  • Monthly rental rate

The class should have the following overloaded constructors:

  • Constructor that takes the rentAPig‘s type, number of pigs, and rental rate
  • Default constructor that sets the pig type to empty string and the other values to zero

Write Get and Set methods for each attribute: model, number of days, and rental rate.

  • Do not allow rate to be less than zero.
  • Do not allow the number of pigs available to be less than zero
  • Have a chargeRental function (only parameter is number of pigs wanted) that:
    • Makes sure you have enough pigs to rent.
    • If enough, use the number of pigs wanted (the parameter), multiply it by rental rate, subtract parameter from the number available and return total charge for the rental.
    • If not enough, return 0 and the main should print an error.

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.

  • Have pig1use the default constructor then use the set methods to set the pig type to “Long hair”, 5 pigs, and rate of $15.95.
  • Have pig2 use the constructor that takes the “Short hair”, 3 pigs, and rate of $9.98.
  • Have a grandTotal variable (not part of class) for totaling the amount of pig rentals collected.

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

  1. Pig 1: Long hair
  2. Pig 2: Short Hair
  3. quit

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

  1. Pig 1: Long hair
  2. Pig 2: Short Hair
  3. quit

Selection: 2

How many Short Hair guinea pigs do you wish to rent? 4

Not enough pigs

Rent-a-Pig

  1. Pig 1: Long hair
  2. Pig 2: Short Hair
  3. quit

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

  1. Pig 1: Long hair
  2. Pig 2: Short Hair
  3. quit

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.

Solutions

Expert Solution

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 :


Related Solutions

Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
Write a c++ program that given a set of letter grade/credit hour combiniation, determines the grade...
Write a c++ program that given a set of letter grade/credit hour combiniation, determines the grade point average (GPA) Each A is worth 4 points. Each B is worth 3 points. Each C is worth 2 points. Each D is worth 1 point, and Each F is worth 0 points. The total quality points earned is the sum of the product of letter grade points and associated course credit hours. The GPA is the quotient of the quality points divided...
Instructions: Show all calculations in detail. No partial credit will be given for just answers. 3....
Instructions: Show all calculations in detail. No partial credit will be given for just answers. 3. An importer of Swiss watches has an account payable of CHF750,000 due in 90 days. The following data is available: Rates and prices in US-cents/CHF.               Spot rate: 71.42 cents/CHF 90-day forward rate: 71.14 cents/CHF US –dollar 90-day interest rate: 3.75% per year Swiss franc 90-day interest rate: 5.33% per year Option Data in cents/CHF _______________________________                         Strike                     Call                  Put 70                          2.55                1.42 72                          1.55               ...
Program in C++ **********Write a program to compute the number of collisions required in a long...
Program in C++ **********Write a program to compute the number of collisions required in a long random sequence of insertions using linear probing, quadratic probing and double hashing. For simplicity, only integers will be hashed and the hash function h(x) = x % D where D is the size of the table (fixed size of 1001). The simulation should continue until the quadratic hashing fails.*********
In C Program #include Create a C program that calculates the gross and net pay given...
In C Program #include Create a C program that calculates the gross and net pay given a user-specified number of hours worked, at minimum wage. The program should compile without any errors or warnings (e.g., syntax errors) The program should not contain logical errors such as subtracting values when you meant to add (e.g., logical errors) The program should not crash when running (e.g., runtime errors) When you run the program, the output should look like this: Hours per Week:...
Instructions: In this section, please show all calculations. Partial credit will be given wherever possible, when...
Instructions: In this section, please show all calculations. Partial credit will be given wherever possible, when your calculations are shown and they are completed correctly. 33. – 35. Corporate Income Taxes Corporate Tax Schedule If Corporation's Taxable It Pays This Amount on the Plus this Percentage on the Income is: Base of the Bracket: Excess Over the Base: Up to $50,000 0 15% $50,000 -- $75,000 $7,500 25% $75,000 -- $100,000 $13,750 34% $100,000 -- $335,000 $22,250 39% $335,000 --...
write a program on c++ that outputs a calendar for a given month in a given...
write a program on c++ that outputs a calendar for a given month in a given year, given the day of the week on which the 1st of the month was. The information in numeric format (months are: 1=Januay, 2=February 3=March, 4= April, 5= May, 6= June, 7= July... etc days are 1=Sunday, 2=Monday, 3= Tuesday, 4= Wednesday, 5= Thursday, 6= Friday and 7= Saturday ). The program output that month’s calendar, followed by a sentence indicating on which day...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
write a program in c++ that opens a file, that will be given to you and...
write a program in c++ that opens a file, that will be given to you and you will read each record. Each record is for an employee and contains First name, Last Name hours worked and hourly wage. Example; John Smith 40.3 13.78 the 40.3 is the hours worked. the 13.78 is the hourly rate. Details: the name of the file is EmployeeNameTime.txt Calculate the gross pay. If over 40 hours in the week then give them time and a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT