Question

In: Computer Science

C++ PLEASE ___________________________________________________________________________________________________________________________________ Your Community Supported Agriculture (CSA) farm delivers a box of fresh fruits and...

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.

____________________________________________________________________________________________________________________________________

Solutions

Expert Solution

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!!!!!!!!----------


Related Solutions

In your (supported) opinion, what, if any, is the difference between "police community programs" and community...
In your (supported) opinion, what, if any, is the difference between "police community programs" and community policing?
PLEASE TYPE OUT ANSWER AND ANSWER COMPLETELY. What are fresh fruits more prone to microbial contamination,...
PLEASE TYPE OUT ANSWER AND ANSWER COMPLETELY. What are fresh fruits more prone to microbial contamination, when compared to dried fruits? what are different forms of cholesterol present in human body. How do those different types impact heart health? Why is the purpose of using food additives?
Please no copy and paste: Questions: 1. In your (supported) opinion, what, if any, is the...
Please no copy and paste: Questions: 1. In your (supported) opinion, what, if any, is the difference between "police community programs" and community policing? 2. What is meant by the term community policing and its relevance as regards serving the community? 3. Does community policing improve the relations between the police and members of a minority community?
How would you go about marketing your community as a sports venue?Please select a community and...
How would you go about marketing your community as a sports venue?Please select a community and give specific examples. Do not select a large city that is already an established sports center.
Enter your answer in the provided box. The solubility of N2 in blood at 37° C...
Enter your answer in the provided box. The solubility of N2 in blood at 37° C and at a partial pressure of 0.80 atm is 5.6 × 10−4 mol/L. A deep-sea diver breathes compressed air with the partial pressure of N2 equal to 3.8 atm. Assume that the total volume of blood in the body is 4.8 L. Calculate the amount of N2 gas released (in liters at 37° C and 1.00 atm) when the diver returns to the surface...
Please show your work 9.2.3 All Fresh Seafood is a wholesale fish company based on the...
Please show your work 9.2.3 All Fresh Seafood is a wholesale fish company based on the east coast of the U.S. Catalina Offshore Products is a wholesale fish company based on the west coast of the U.S. Table #9.2.5 contains prices from both companies for specific fish types ("Seafood online," 2013) ("Buy sushi grade," 2013). Do the data provide enough evidence to show that a west coast fish wholesaler is more expensive than an east coast wholesaler? Test at the...
Please share your reflections on the importance of volunteers and their value to the health community....
Please share your reflections on the importance of volunteers and their value to the health community. (500 words)
C++ please A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm...
C++ please A milk carton can hold 3.78 liters of milk. Each morning, a dairy farm ships cartons of milk to a local grocery store. The cost of producing one liter of milk is $0.38, and the profit of each carton of milk is $0.27. Write a program that prompts the user to enter: The total amount of milk produced in the morning. The program then outputs: The number of milk cartons needed to hold milk. Round your answer to...
Enter your answer in the provided box. Henry's law constant for CO2 at 38 ° C...
Enter your answer in the provided box. Henry's law constant for CO2 at 38 ° C is 2.28 × 10−3 mol / L · atm. Calculate the pH of a solution of CO2 at 38 ° C in equilibrium with the gas at a partial pressure of 8.05 atm.
Enter your answer in the provided box. A 58.92−g sample of water at 74.9°C is added...
Enter your answer in the provided box. A 58.92−g sample of water at 74.9°C is added to a sample of water at 25.3°C in a constant-pressure calorimeter. If the final temperature of the combined water is 42.8°C and the heat capacity of the calorimeter is 26.3 J/°C, calculate the mass of the water originally in the calorimeter.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT