Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A local apple farmer has asked you to write a program to help him manage his app

ID: 3767956 • Letter: A

Question

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.

Like any self-respecting apple farmer, this farmer thinks the inventory and harvest data should be stored as private C++ arrays as part of a class. He has also advised you 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;

}

void sellApples(int demand)

/* • if demand <= inventory

• update sales[] to set sales[currentDay] = demand

• the demand should be subtracted from the inventory

• if demand > inventory, then 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  
• Use   the   cout statement:
cout<<”Day: ”<<x<<” Harvest: “<<harvest[x]<<endl;
where   x is   the   index   variable   for   the   array.
*/

void   printSales()
/*
• print   the   contents   of   the   entire   sales[] array
• Use   the   cout statement:
cout<<”Day: ”<<x<<” Sales: “<<sales[x]<<endl;
where   x is   the   index   variable   for   the   array.
*/

Code in your main function:

Create an instance of the AppleFarmer class to initialize the sales and harvest arrays to 0. If you use the AppleFarmer.h file provided, then currentDay and inventory will also be initialized to 0.

//We start with a current day = 0 because it’s the index in the array

//The 30 days of data will be stored in the array at indices 0 … 29.

While endOfMonth() == false:

Ask the user for a harvest amount, or read from another array cout<<”Enter a harvest amount: ”<>harvest;

Call harvestApples to update the harvest and inventory variables.

Ask the user for a sales amount. cout<<”Enter a sales amount: “<>sales;

Call sellApples to update the sales and inventory variables
Print the current inventory
cout<<”Apple inventory: “<<getInventory()<<endl;

Update the current day

After the harvest and sales arrays are full, calculate the average
harvest, and print the value.
cout<<”Average harvest: “<<calculateAverageHarvest()<<endl;

Calculate the average sales, and print the value.
cout<<”Average sales: “<<calculateAverageSales()<<endl;

Explanation / Answer

AppleFarmer.cpp

#include <iostream>
using namespace std;
#include "AppleFarmer.h"

void sellApples(int demand){
if(demand <= inventory){
sales[currentDay] = demand;
}
else{
sales[currentDay] = 0;
}
}

void harvestApples(int dayHarvest){
harvest[currentDay] = dayHarvest;
inventory = inventory+dayHarvest;
}

bool endOfMonth(){
if(currentDay == maxDays){
return true;
}
return false;
}


void updateCurrentDay(){
currentDay = currentDay+1;
}

double calculateAverageHarvest(){
double total = 0;
for(int i=0;i<currentDay;i++){
total = total + harvest[i];
}
return total / currentDay;
}

double calculateAverageSales(){
double total = 0;
for(int i=0;i<currentDay;i++){
total = total + sales[i];
}
return total / currentDay;
}

int getInventory(){
return inventory;
}

void printHarvest(){
for(int i=0;i<currentDay;i++){
cout<<"Day: "<<i+1<<" Harvest: "<<harvest[i];
}
}

void printSales(){
for(int i=0;i<currentDay;i++){
cout<<"Day: "<<i+1<<" Sales: "<<sales[i];
}
}

AppleFarmer.h

class AppleFarmer{
private:
int sales[30];
int harvest[30];
int maxDays = 30;
int currentDay = 0;
int inventory = 0;
public:
AppleFarmer();
void sellApples(int demand);
void harvestApples(int dayHarvest);
bool endOfMonth();
void updateCurrentDay();
int getInventory();
double calculateAverageSales();
double calculateAverageHarvest();
void printSales();
void printHarvest();

};
  

MAIN method

#include <iostream>

using namespace std;
#include "AppleFarmer.h";


int main() {
AppleFarmer farmer;
int harvestAmount;
int salesApples;
while(farmer.endOfMonth()){
cout<<"Enter a harvest amount: ";
cin>>harvestAmount;
farmer.harvestApples(harvestAmount);
cout<<"Enter a sales amount: ";
cin>>salesApples;
farmer.sellApples(salesApples);
cout<<"Apple inventory: "<<farmer.getInventory()<<endl;
farmer.updateCurrentDay();
}
//calculate the average harvest, and print the value.
cout<<"Average harvest: "<<farmer.calculateAverageHarvest()<<endl;
//Calculate the average sales, and print the value.
cout<<"Average sales: "<<farmer.calculateAverageSales()<<endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote