The apple-management system A local apple farmer has asked you to write a progra
ID: 3693033 • Letter: T
Question
The apple-management system
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: “<<x<<” Harvest: “<<harvest[x]<<endl;
where x is index for the array. */
void printSales() /*
print the contents of the entire sales[] array using the cout statement:
cout<<”Day:”<<x<<” Sales: “<<sales[x]<<endl;
where x is the index variable for the array. */
Explanation / Answer
main.cpp
#include <iostream>
#include "AppleFarmer.h"
using namespace std;
int main() {
AppleFarmer farmerBob(0);
int demand;
int dayHarvest;
while (farmerBob.endOfMonth() == false) {
cout << "Enter a harvest amount: " << endl;
cin >> dayHarvest;
farmerBob.harvestApples(dayHarvest);
cout << "Enter a sales amonut: " << endl;
cin >> demand;
farmerBob.sellApples(demand);
cout << "Apple inventory: " << farmerBob.getInventory() << endl;
farmerBob.updateCurrentDay();
}
cout << "Average harvest: " << farmerBob.calculateAverageHarvest() << endl;
cout << "Average sales: " << farmerBob.calculateAverageSales() << endl;
return 0;
}
AppleFarmer.cpp
#include "AppleFarmer.h"
#include <iostream>
using namespace std;
AppleFarmer::AppleFarmer(int initVal) {
for (int i=0; i < 30; i++) {
sales[i] = initVal;
harvest[i] = initVal;
}
}
AppleFarmer::~AppleFarmer()
{
//dtor
}
void AppleFarmer::sellApples(int demand) {
if (demand <= inventory) {
sales[currentDay] = demand;
inventory -= demand;
} else {
sales[currentDay] = 0;
}
}
void AppleFarmer::harvestApples(int dayHarvest) {
harvest[currentDay] = dayHarvest;
inventory += dayHarvest;
}
bool AppleFarmer::endOfMonth() {
if (currentDay == maxDays)
return true;
return false;
}
void AppleFarmer::updateCurrentDay() {
currentDay += 1;
}
double AppleFarmer::calculateAverageHarvest() {
int total = 0;
for (int i=0; i < 30; i++) {
total += harvest[i];
}
return total/30.0;
}
double AppleFarmer::calculateAverageSales() {
int total = 0;
for (int i=0; i < 30; i++) {
total += sales[i];
}
return total/30.0;
}
int AppleFarmer::getInventory() {
return inventory;
}
void AppleFarmer::printHarvest() {
for (int i=0; i < 30; i++) {
cout<<"Day: "<<i<<" Harvest: "<<harvest[i]<<endl;
}
}
void AppleFarmer::printSales() {
for (int i=0; i < 30; i++) {
cout<<"Day: "<<i<<" Sales: "<<sales[i]<<endl;
}
}
AppleFarmer.h
#ifndef APPLEFARMER_H
#define APPLEFARMER_H
class AppleFarmer
{
public:
AppleFarmer(int);
~AppleFarmer();
void sellApples(int);
void harvestApples(int);
bool endOfMonth();
void updateCurrentDay();
int getInventory();
double calculateAverageHarvest();
double calculateAverageSales();
void printSales();
void printHarvest();
protected:
private:
int sales[30];
int harvest[30];
int maxDays = 30;
int currentDay = 0;
int inventory = 0;
};
#endif // APPLEFARMER_H
sample output
Apple inventory: 0
Enter a harvest amount:
Enter a sales amonut:
Apple inventory: 0
Enter a harvest amount:
Enter a sales amonut:
Apple inventory: 0
Enter a harvest amount:
Enter a sales amonut:
Apple inventory: 0
Enter a harvest amount:
Enter a sales amonut:
Apple inventory: 0
Average harvest: 300
Average sales: 300
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.