Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. a: The function below titled CANDY CLASS has a business rule enforced on set_

ID: 3686284 • Letter: 1

Question

 1.  a: The function below titled CANDY CLASS has a business rule enforced on set_unit_price. Create a similar function that will allow the caller to set the packing price. It must enforce a rule that the packing price has to be at least twice the unit price. Name the function set_pack_price. You can loosely follow set_unit_price as a guide.  b: In the main function execute two calls to set_pack_price. The first call will demonstrate that no change is made if the business rule is violated. The second call will demonstrate that the change is allowed otherwise. For instance if a unit price is 2.00, the pack price cannot be 3.00 but it can be 4.00 (e.g., at least twice the unit price).  c: Create a similar class function to set the pack size. Enforce a rule that restricts the size of the pack to between 4 and 24 units (inclusive). 
    
 CANDY CLASS FUNCTION:  
 #include #include #include #include  using namespace std;  class Candy { private:         string name;         float  unit_price;         int    pack_size;         float  pack_price;  public:         //Default (parameterless) constructor         Candy() { ; }          //Overloaded constructor with four parameters         Candy(string name, float unit_price, int pack_size, float pack_price)          {                  this->name = name;                  this->unit_price = unit_price;                 this->pack_size = pack_size;                 this->pack_price = pack_price;         }          //Manipulators/accessors         void set_name(string name)         {                 this->name = name;         }          string get_name()         {                 return this->name;         }          //Overloaded get_name with 1 parameter         string get_name(bool cap)         {                 string result(this->name);                 int len = result.length();                  for (int i = 0; i < len; i++)                 {                         if(cap)                                 result[i] = toupper(result[i]);                         else                                 result[i] = tolower(result[i]);                 }                  return result;         }          //Convert this entire object to a string representation         string str()         {                 //USE STRING STREAMS TO FORMAT THE STRING                 //WORKS ALMOST LIKE cout                 ostringstream sout;                  sout.precision(2);                 sout << fixed << this->name << " (UNIT_PRICE: " << this->unit_price << ", PACK_SIZE: " << this->pack_size << ", PACK_PRICE: " << this->pack_price << ")";                 return sout.str();         }          void set_unit_price(float price)         {                 //Enforce a business rule here                 //Price can't be 0 or less                 //Price can't be more than half the pack price                 if (price <= 0.0)                         return;                  if (price > 0.5 * this->pack_price)                         return;                  this->unit_price = price;         } };   int main() {         string s;         Candy thing;           //WE encapsulate all we know about a Snickers within a single object         Candy snickers("Snickers", 0.89, 10, 7.99);          //Use the accessor to retrieve name         cout << snickers.get_name() << endl;          //Use the accessor override to retrieve name with modified case         cout << snickers.get_name(true) << endl;         cout << snickers.get_name(false) << endl;          //INVOKE the str() function we defined to get the info about the object         cout << "ORIGINAL: " << snickers.str() << endl;          //Now attempt to set the unit price to $5.         snickers.set_unit_price(5.00);         //We see that a business rule we've established in set_unit_price prevents us from doing so         cout << "AFTER 5.00: " << snickers.str() << endl;                  //Now attempt to set the unit price to $2.         snickers.set_unit_price(2.00);         //Business rule allowed the change         cout << "AFTER 2.00: " << snickers.str() << endl;           getline(cin, s); } 

Explanation / Answer

#include <iostream>
#include<iomanip>
#include <sstream>
using namespace std;

class Candy
{
private:
string name;
float unit_price;
int pack_size;
float pack_price;

public:
//Default (parameterless) constructor
Candy()
{
;
}

//Overloaded constructor with four parameters
Candy(string name, float unit_price, int pack_size, float pack_price)
{
this->name = name;
this->unit_price = unit_price;
this->pack_size = pack_size;
this->pack_price = pack_price;
}

//Manipulators/accessors
void set_name(string name)
{
this->name = name;
}

string get_name()
{
return this->name;
}

//Overloaded get_name with 1 parameter
string get_name(bool cap)
{
string result(this->name);
int len = result.length();

for (int i = 0; i < len; i++)
{
if(cap)
result[i] = toupper(result[i]);
else
result[i] = tolower(result[i]);
}

return result;
}

//Convert this entire object to a string representation
string str()
{
//USE STRING STREAMS TO FORMAT THE STRING
//WORKS ALMOST LIKE cout
ostringstream sout;

sout.precision(2);
sout << fixed << this->name << " (UNIT_PRICE: " << this->unit_price << ", PACK_SIZE: " << this->pack_size << ", PACK_PRICE: " << this->pack_price << ")";
return sout.str();
}

void set_unit_price(float price)
{
//Enforce a business rule here
//Price can't be 0 or less
//Price can't be more than half the pack price
if (price <= 0.0)
return;

if (price > 0.5 * this->pack_price)
return;

this->unit_price = price;
}


void set_pack_price(float pack_price)
{

if (2*unit_price <= pack_price)
this->pack_price=pack_price;

else
return;

}

void set_pack_size(int pack_size)
{

if (pack_size <= 24&&pack_size>=4)
this->pack_size=pack_size;

else
return;

}
};


int main()
{
string s;
Candy thing;


//WE encapsulate all we know about a Snickers within a single object
Candy snickers("Snickers", 0.89, 10, 7.99);


//Use the accessor to retrieve name
cout << snickers.get_name() << endl;

//Use the accessor override to retrieve name with modified case
cout << snickers.get_name(true) << endl;
cout << snickers.get_name(false) << endl;

//INVOKE the str() function we defined to get the info about the object
cout << "ORIGINAL: " << snickers.str() << endl;

//Now attempt to set the unit price to $5.
snickers.set_unit_price(5.00);
snickers.set_pack_price(5.00);
snickers.set_pack_size(3);
//We see that a business rule we've established in set_unit_price prevents us from doing so
cout << "AFTER 5.00: " << snickers.str() << endl;

//Now attempt to set the unit price to $2.
snickers.set_unit_price(2.00);
snickers.set_pack_price(2.00);
snickers.set_pack_size(20);

//Business rule allowed the change
cout << "AFTER 2.00: " << snickers.str() << endl;


getline(cin, s);
}

OUTPUT:

Snickers
SNICKERS
snickers
ORIGINAL: Snickers (UNIT_PRICE: 0.89, PACK_SIZE: 10, PACK_PRICE: 7.99)
AFTER 5.00: Snickers (UNIT_PRICE: 0.89, PACK_SIZE: 10, PACK_PRICE: 5.00)
AFTER 2.00: Snickers (UNIT_PRICE: 2.00, PACK_SIZE: 20, PACK_PRICE: 5.00)


Process returned 0 (0x0) execution time : 6.038 s
Press any key to continue.