In: Computer Science
C++ PLEASE
___________________________________________________________________________________________________________________________________
Your Community Supported Agriculture (CSA) farm delivers a box of fresh fruits and vegetables to your house once a week. For this Programming Project, define the class BoxOfProduce that contains exactly three bundles of fruits or vegetables. You can represent the fruits or vegetables as three instance variables of type String .Add an appropriate constructor, accessor, and mutator methods. Also write a toString() method that returns as a String the complete contents of the box.
Next, write a main method that creates a BoxOfProduce with three items randomly selected from this list:
Broccoli
Tomato
Kiwi
Kale
Tomatillo
This list should be stored in a text file that is read in by your program. For now you can assume that the list contains exactly five types of fruits or vegetables.
Do not worry if your program randomly selects duplicate produce for the three items. Next, the main method should display the contents of the box and allow the user to substitute any one of the five possible fruits or vegetables for any of the fruits or vegetables selected for the box. After the user is done with substitutions, output the final contents of the box to be delivered. If you create additional methods to select the random items and to select valid substitutions, then your main method will be simpler to write.
____________________________________________________________________________________________________________________________________
SOLUTION-
I have solve the problem in C++ code with comments and screenshot
for easy understanding :)
CODE-
//c++ code
//header file used
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
//The box of produce class
class BoxOfProduce
{
public:
// Number of boxes of produce
static const int NUM_BOXES = 3;
// Mutator
void setProduce(const int, const string);
// Accessor
const string getProduce(const int);
// Output the object
void output();
private:
// The boxes of produce
string produce[NUM_BOXES];
};
/*
Set the box of produce at index to item
*/
void BoxOfProduce::setProduce(const int index,const string
item)
{
produce[index] = item;
}
/*
Get the box of produce at index
*/
const string BoxOfProduce::getProduce(const int index)
{
return produce[index];
}
/*
Output the contents of all the boxes of produce
*/
void BoxOfProduce::output()
{
cout << "Box of produce contains:\n\t1. "<< produce[0]
<< "\n\t2. "<< produce[1] << "\n\t3. "<<
produce[2] << endl;
}
/*
Read from filename, the array produce of size
*/
int readProduce(const char filename[],string produce[],const int
size)
{
// The input file stream
fstream in;
// Open the file
in.open(filename);
// Number of items read
int numRead = 0;
// Read size items of produce
for (int index = 0; index < size; index++)
if (in >> produce[index])
++numRead;
else
break;
// Close the input file stream
in.close();
// Return the number of items read
return numRead;
}
/*
Seed the pseudorandom number generator
*/
void seedRandom()
{
cout << "Enter seed for pseudorandom ""number generator:
";
int seed;
cin >> seed;
srand(seed);
}
/*
Fill the boxes of produce of the BoxOfProduce
object from the array produce of size num_produce
*/
void fillBoxOfProduce(BoxOfProduce &boxOfProduce,const string
produce[],const int num_produce)
{
// Fill all the boxes of the BoxOfProduce object
for (int index = 0;
index < BoxOfProduce::NUM_BOXES;
index++)
// Set the produce to a randomly
// selected element of produce
boxOfProduce.setProduce(index,
produce[rand()
% num_produce]);
}
//Output the string message and get an index into an array between
1 and maximum. The index is decremented by 1 before being
returned.
/*
Print out message and get an index between 1 and
maximum. Return the index as starting from zero
*/
int readIndex(const string message, const int maximum)
{
int index;
// Repeat endlessly
for ( ;; ) {
// Print the message
cout << message;
// Get the index
cin >> index;
// Is the index okay?
if (index >= 1 && index <= maximum)
{
// Make it start from 0
index--;
// Get out of the loop as the index is valid
break;
}
// Dump error message
cerr << "Invalid index! It should be an ""integer between 1
and "
<< maximum << endl;
}
return index;
}
/*
Output the kinds of produce of size num
*/
void outputProduce(const string produce[], const int num)
{
cout << "Types of produce:\n";
for (int index = 0; index < num; index++)
cout << index + 1 << ". "
<< produce[index] << endl;
}
/*
Output the contents of the BoxOfProduce object
and let the user modify any box with the
possible kinds of produce.
*/
void modifyBox(BoxOfProduce &boxOfProduce,const string
produce[],const int numProduce)
{
// Infinite loop
while (true)
{
// Output the BoxOfProduce object
boxOfProduce.output();
// Check if the user wants
// to modify the BoxOfProduce object
cout << "Modify contents of box? ";
char modify;
cin >> modify;
// Is the object to modified?
if (modify != 'y' && modify != 'Y')
// No, so break out of the loop
break;
// Read the index of the box to be modified
int boxIndex = readIndex("Enter index of box to be modified?
",
BoxOfProduce::NUM_BOXES);
// Output the different kinds of produce
outputProduce(produce, numProduce);
// Get the index of the produce to be
// placed in the BoxOfProduce object
int produceIndex = readIndex("Enter index of produce to be added?
",
numProduce);
// Set the selected box to the selected produce
boxOfProduce.setProduce(boxIndex,produce[produceIndex]);
}
}
/*
The main startup function
*/
int main(int argc, char *argv[])
{
// The name of the produce file
const char PRODUCE_FILE[] = "Data.txt";
// The number of entries in the produce file
const int PRODUCE_RECORDS = 5;
// Create the produce array as an array of strings
string produce[PRODUCE_RECORDS];
// Read the produce from the file
int numProduce = readProduce(PRODUCE_FILE,
produce,
PRODUCE_RECORDS);
// Seed the pseudorandom generator
seedRandom();
// Declare a BoxOfProduce and fill it from items
// randomly selected from produce
BoxOfProduce boxOfProduce;
fillBoxOfProduce(boxOfProduce, produce,
numProduce);
// Let the user modify the
// contents of the BoxOfProduce object
modifyBox(boxOfProduce, produce, numProduce);
system("pause");
return 0;
}
Data.txt-
Broccoli
Tomato
Kiwi
Kale
Tomatillo
SCREENSHOT-
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL
SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------