in c++ A local apple farmer has asked you to write a program to help him manage
ID: 3688230 • Letter: I
Question
in c++
A local apple farmer has asked you to write a program to help him manage his apple inventory. The farmer needs to keep track of how many apples he sells everyday, how many apples he picks, and how many apples he has in storage. Your apple inventory system needs to track data for 30 days.
The inventory and harvest data should be stored as private C++ arrays as part of a class. You also need to include private variables for the inventory, current day, and maximum number of entries in your array, and then access all private variables through public methods. The AppleFarmer.h file includes the following class definition:
Class AppleFarmer{
public:
AppleFarmer(int);
void sellApples(int Demand);
void harvestApples(int dayHarvest);
bool endOfMonth();
void updateCurrentDay();
int getInventory();
double calculateAverageSales();
double calculateAverageHarvest();
void printSales();
void printHarvest();
private:
int sales[30];
int harvest[30];
int maxDays = 30;
int currentDay = 0;
int inventory = 0;
}
Functionality for each method:
AppleFarmer(int initVal)
/* initialize all elements in the sales[] and harvest[] to initVal */
void sellApples(int demand)
/*
if demand <= inventory
update sales[] to set sales[currentDay] = demand
the demand should be subtracted from the inventory
if the demand > inventory
set sales[currentDay] = 0 (and leave inventory unchanged)
*/
void harvestApples(int dayHarvest)
/*
update harvest[] to set harvest[currentDay] = dayHarvest
the dayHarvest should be added to the inventory
*/
bool endOfMonth()
/*
check if currentDay = maxDays, indicating that the sales[] and harvest[]
arrays are full
if currentDay = maxDays, return true. Otherwise return false
*/
void updateCurrentDay()
/*
add 1 to currentDay
*/
double calculateAverageHarvest()
/*
return the average daily harvest calculation for the harvest[] array
*/
double calculateAverageSales()
/*
return the average daily apple sales for the sales[] array
*/
int getInventory()
/*
return inventory
*/
void printHarvest()
/*
print the contents of the entire harvest[] array using cout statement:
cout<<”Day: “< where x is index for the array.
*/
void printSales()
/*
print the contents of the entire sales[] array using the cout statement:
cout<<”Day:”< where x is the index variable for the array.
*/
Explanation / Answer
Answer
#include <iostream>
using namespace std;
class AppleFarmer
{
int sales[30];
int harvest[30];
int maxDays = 30;
int currentDay = 0;
int inventory = 0;
public:
AppleFarmer(int initVal)
{
for(int i=0;i<30;i++)
{
sales[i]=initVal;
harvest[i]=initVal;
}
}
void sellApples(int Demand)
{
if(Demand <= inventory)
{
sales[currentDay] += Demand;
inventory -= Demand;
}
else if(Demand>inventory)
{
sales[currentDay] = 0;
}
}
void harvestApples(int dayHarvest)
{
harvest[currentDay] = dayHarvest;
inventory += dayHarvest ;
}
bool endOfMonth()
{
if(currentDay ==maxDays)
return true;
else
return false;
}
void updateCurrentDay()
{
currentDay++;
}
int getInventory()
{
return inventory;
}
double calculateAverageSales()
{
double tot=0.0;
for(int i=0;i<currentDay;i++)
{
tot+=sales[i];
}
return (tot/currentDay);
}
double calculateAverageHarvest()
{
double tot=0.0;
for(int i=0;i<currentDay;i++)
{
tot+=harvest[i];
}
return (tot/currentDay);
}
void printSales()
{
for(int i=0;i<currentDay;i++)
{
cout<<" Day "<<i+1<<" Sale = "<<sales[i];
}
}
void printHarvest()
{
for(int i=0;i<currentDay;i++)
{
cout<<" Day "<<i+1<<" Harvest = "<<harvest[i];
}
}
};
int main()
{
AppleFarmer obj(0);
int todayharvest,todaysale;
while(obj.endOfMonth()==false)
{
cout<<" Start Inventory : "<<obj.getInventory();
cout<<" Enter Harvesting for Today : ";
cin>>todayharvest;
obj.harvestApples(todayharvest);
cout<<" Enter Sales for Today : ";
cin>>todaysale;
obj.sellApples(todaysale);
cout<<" End Inventory : "<<obj.getInventory();
obj.updateCurrentDay();
}
cout<<" -----Monthly Harvesting of Apples-----";
obj.printHarvest();
cout<<" -----Monthly Sales of Apples-----";
obj.printSales();
cout<<" -----Final Statement of Month-----";
cout<<" Average Sale : "<<obj.calculateAverageSales();
cout<<" Average Harvest : "<<obj.calculateAverageHarvest();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.