C++ Suppose a local farm has hired you to write a program for them that keeps tr
ID: 3570640 • Letter: C
Question
C++
Suppose a local farm has hired you to write a program for them that keeps track
of all sensory information. This farm has installed sensors that monitor the farm
and has robots to water, harvest, and plant seeds. The farm has divided the land
into 20 plots. The first plot has 5 tomato plants, 2nd plot has 5 lettuce plants, the
third has 5 tomato plants, and 4rth has 5 lettuce plants, etc. The crop type is
alternated between tomato and lettuce. Each plot has the following information
associated with it:
? number of plants
? plant type (tomato or lettuce)
? moisture 0 (no water) to 5 (over watered)
? whether or not the plot has been harvested (if it is true that it has been harvested
then the plot is currently empty until it is replanted)
You should also have a class called Farm that contains an array of 20 plots. The Farm
class also has the following information:
? int number_plants: represents how many plants are growing (how many are not harvested)
? int harvested : represents how many plots are empty
Write a program that displays the following menu with description (which the user sees
after they select any choice other than 6 to quit)
Menu:
1. Display plot information
Description - This menu option will request a plot number and print out information for
that plot
2. Water plot
Description - This menu option will request a plot number and increment the moisture
level by 1
3. Harvest plot
Description - This menu option will request a plot number and harvest plot
4. Replant plot
Description - This menu option will request a plot number and use that number to plant
seeds for that plant again
5. Display All
Description - This menu option displays information for all of the plots
6. Quit application
Explanation / Answer
#include <iostream>
using namespace std;
enum plant{
tomato = 0,
lettuce = 1
};
struct plot{
int numPlants;
plant p;
int moisture;
bool harvested;
};
class farm{
private:
int number_plants;
int harvested;
plot *arr;
public:
farm(){
arr = new plot[20];
for(int i = 0; i < 20; ++i){
arr[i].harvested = false;
arr[i].numPlants = 5;
arr[i].moisture = 3;
if((i + 1) % 2 == 1){
arr[i].p = tomato;
}
else{
arr[i].p = lettuce;
}
}
}
void printPlot(int number){
if(number < 21){
cout << "Plot " << number << " Details" << endl;
plot temp = arr[number - 1];
cout << "Number of Plants: " << temp.numPlants << endl;
if((number % 2) == 1){
cout << "Plant type: Tomato" << endl;
}
else{
cout << "Plant type: Lettuce" << endl;
}
cout << "Moisture: " << temp.moisture << endl;
if(temp.harvested){
cout << "Plot has been harvested" << endl;
}
else{
cout << "Plot has not been harvested" << endl;
}
}
}
void waterPlot(int number){
number--;
if(arr[number].moisture < 6){
arr[number].moisture++;
cout << "Moisture level is " << arr[number].moisture << endl;
}
else{
cout << "Moisture level is already 5" << endl;
}
}
void harvestPlot(int number){
if(arr[number - 1].harvested){
cout << "Plot has already been harvested" << endl;
}
else{
cout << "Harvesting the Plot" << endl;
arr[number - 1].harvested = true;
arr[number - 1].moisture = 0;
arr[number - 1].numPlants = 0;
}
}
void rePlantPlot(int num1){
num1--;
arr[num1].harvested = false;
arr[num1].moisture = 3;
arr[num1].numPlants = 5;
cout << "replotting the plant..." << endl;
}
void display(){
for(int i = 0; i < 20; ++i){
printPlot(i);
cout << endl << endl;
}
}
};
int main(){
int option;
farm F;
while(true){
cout << endl << "1. Display plot information" << endl;
cout << "2. Water plot" << endl;
cout << "3. Harvest plot" << endl;
cout << "4. Replant plot" << endl;
cout << "5. Display All" << endl;
cout << "6. Quit application" << endl;
cout << "choose an option: ";
cin >> option;
switch(option){
case 1:
cout << "Enter plot Number: ";
cin >> option;
F.printPlot(option);
break;
case 2:
cout << "Enter plot Number: ";
cin >> option;
F.waterPlot(option);
break;
case 3:
cout << "Enter plot Number: ";
cin >> option;
F.harvestPlot(option);
break;
case 4:
cout << "Enter plot Number: ";
cin >> option;
F.rePlantPlot(option);
break;
case 5:
F.display();
break;
case 6:
return 0;
default:
cout << "Please, chhose a valid option" << endl;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.