I need to write a program that does the following: A farm delivers a box of fres
ID: 3545615 • Letter: I
Question
I need to write a program that does the following:A farm delivers a box of fresh fruits and vegetables to your house once a week. For this assignment, define the class BoxOfProduce that contains exactly three bundles of fruits or vegetables. You can represent the fruits or vegetables as an array of type string. Add appropriate constructors and accessor/mutator functions to get or set the fruits or vegetables stored in the array. Also write an output function that displays the complete contents of the box on the console.
Next, write a main function that creates a BoxOfProduce with three items randomly selected from this list:
Broccoli Tomato Kiwi Kale Tomatillo
Do not worry if your program randomly selects duplicate produce for the three items. Next, the main function should display the contents of the box and allow the user to substitute any one of the possible fruits or vegetables for any of the fruits or vegetables selected for the box. After the user is done with substitutions it should output the final contents of the box to be delivered. Then it should ask if the user wants to create another box and if yes, it should repeat the above steps. It should keep doing so until the user chooses not to create another box of produce.
Finally, add a static variable to your class that keeps track of the total number of boxes of produce created and a static function that returns that value. Display this value in the main function at the end of each iteration of the main loop. I need to write a program that does the following:
A farm delivers a box of fresh fruits and vegetables to your house once a week. For this assignment, define the class BoxOfProduce that contains exactly three bundles of fruits or vegetables. You can represent the fruits or vegetables as an array of type string. Add appropriate constructors and accessor/mutator functions to get or set the fruits or vegetables stored in the array. Also write an output function that displays the complete contents of the box on the console.
Next, write a main function that creates a BoxOfProduce with three items randomly selected from this list:
Broccoli Tomato Kiwi Kale Tomatillo
Do not worry if your program randomly selects duplicate produce for the three items. Next, the main function should display the contents of the box and allow the user to substitute any one of the possible fruits or vegetables for any of the fruits or vegetables selected for the box. After the user is done with substitutions it should output the final contents of the box to be delivered. Then it should ask if the user wants to create another box and if yes, it should repeat the above steps. It should keep doing so until the user chooses not to create another box of produce.
Finally, add a static variable to your class that keeps track of the total number of boxes of produce created and a static function that returns that value. Display this value in the main function at the end of each iteration of the main loop.
Explanation / Answer
#include <iostream>
#include <stdio.h>
using namespace std;
/*Broccoli
Tomato
Kiwi
Kale
Tomatillo*/
class BoxOfProduce{
private:
char i1[20];
char i2[20];
char i3[20];
public:
int static BoxCount;
public:
BoxOfProduce(char *f1, char*f2, char *f3){
BoxCount++;
strcpy(i1, f1);
strcpy(i2, f2);
strcpy(i3, f3);
}
void Display(void){
cout<<" Box Contents"<<endl;
cout<<"1. "<<i1<<endl;
cout<<"2. "<<i2<<endl;
cout<<"3. "<<i3<<endl;
}
void Replace (int i, char *f){
switch( i ){
case 1: strcpy(i1, f); break;
case 2: strcpy(i2, f); break;
case 3: strcpy(i3, f); break;
}
}
};
int BoxOfProduce::BoxCount=0;
int main()
{
int exit = 0;
char ch2;
char veg[][15] = { "Broccoli",
"Tomato",
"Kiwi",
"Kale",
"Tomatillo" };
do{
BoxOfProduce b1( veg[rand()%5], veg[rand()%5], veg[rand()%5] );
do{
b1.Display();
cout<<"Would you like to Replace any(Y/N)?";
cin >> ch2;
if( ch2 == 'y' || ch2=='Y'){
int rep=0, rep_with;
cout<<"Enter index of fruit to replace :";
cin>>rep;
for( int i=0; i<5; i++)
cout<<i+1<<". "<<veg[i]<<endl;
cout<<"Replace with : ";
cin >> rep_with;
b1.Replace(rep, veg[rep_with-1]);
}
}while( ch2 != 'n' && ch2!='N' );
cout<<"Final Delivery";
b1.Display();
cout<<" Would you like to create another box(Y/N)";
cin.get();
cin >> ch2;
}while(ch2=='y' || ch2=='Y') ;
cout<<"Total Boxes Created: "<<BoxOfProduce::BoxCount;
}