Question: C++ Problem Please Help! : I need to rewrite a program to use classes. My program needs a main.cpp , and an AppleFarmer.cpp , and
C++ Problem Please Help!:
I need to rewrite a program to use classes. My program needs a main.cpp, and an AppleFarmer.cpp, and AppleFarmer.h for My classes.
Some Guidelines:
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.
My Current Code (to be edited later):
#include #define MONTH 30 using namespace std;
//update sales[] to set sales[currentDay] = demand //The sales[] array should be updated with demand if demand <= inventory //if demand > inventory, then set sales[currentDay] = 0 //if the array is updated with demand, the function should return True. //Otherwise, the function should return False. bool sellApples(int sales[], int inventory, int demand, int currentDay) { if(demand <= inventory) { sales[currentDay] = demand; return true; } else { sales[currentDay] = 0; return false; } } //update harvest[] to set harvest[currentDay] = dayHarvest void harvestApples(int harvest[], int currentDay, int dayHarvest) { harvest[currentDay] = dayHarvest; } //check if the array is full by checking if currentDay = MONTH. //if the array is full, then return True. Otherwise, return False bool endOfMonth(int currentDay) { if(currentDay == MONTH) return true; else return false; } //this function adds 1 to currentDay and returns this value //return currentDay+1; //Note: if you use currentDay++ here it wont work. int updateCurrentDay(int currentDay) { return currentDay+1; }
//Use this function to update the inventory variable after calling sellApples() or harvestApples() //to change the value of inventory. //The parameter change is the change to inventory. //If change < 0, it means that apples have been sold, and if change > 0, it means that apples have been harvested. //This function should only be called after sellApples() is called if sellApples() returns True, //indicating that the sale was successful and apples need to be deducted from the inventory. //The function should also be called after harvestApples() to add apples to the inventory. //The function should return the new value for inventory. int updateInventory(int inventory, int change) { return inventory + change; } //calculate the average daily harvest after MONTH days double calculateAverageHarvest(int harvest[]) { int totalHarvest = 0; for(int i = 0; i < MONTH; i++) totalHarvest += harvest[i]; return (double)totalHarvest/MONTH; } //calculate the average apple sales after MONTH days double calculateAverageSales(int sales[]) { int totalSales = 0; for(int i = 0; i < MONTH; i++) totalSales += sales[i]; return (double)totalSales/MONTH; } int main() { int harvest[MONTH], sales[MONTH], dayHarvest, demand, change; int inventory = 0, currentDay = 0; double averageHarvest, averageSale; bool sold; while(!endOfMonth(currentDay)) { cout<<"Enter the harvest for day "<
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< where x is index for the array. */ void printSales() /* print the contents of the entire sales[] array using the cout statement: cout< where x is the index variable for the array. */ Code in 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 the currentDay and inventory will also be initialized to 0. // We start with a current day = 0 because its the first index in the // array. The 30 days of data will be stored in the array indices from // 0 to 29. While endOfMonth() == false: Ask the user for a harvest amount, or read it in from an array cout< Call harvestApples to update the harvest and inventory variables. Ask the user for the sales amount, or read it in from an array. cout< Call sellApples to update the sales and inventory variables Print the current inventory: cout< Update the current day After harvest, and print the value. cout< the harvest and sales arrays are full, calculate the average Calculate the average sales, and print the value. cout< Here are some basic outlines I have for the assignment I have so far. Remember I should be changing the Code at the top of the question! Outline for applefarmer.cpp: #include #include "AppleFarmer.h" using namespace std; // You might need the #include and // using namespace std; // for using cout statement with // void printSales() and void printHarvest() functions. // Define the constructor and all the class functions in this file. AppleFarmer::AppleFarmer(int initVal) { // initialize sales[] and harvest[] to // initVal // for loop from 0 to 29. // ...need to code this up. } // .... function definitions need here. Outline for the main function: int main() { // Create AppleFarmer class instance. // All functions should be called using this instance. // E.g. farmer.update AppleFarmer farmer(0); // ... you need to define the loop that goes over each day // and does the whole process of getting user input for harvest and updating inventory, // user input for demand and updating inventory.... and finally updating currentDay. return 0; } Outline for Applefarmer.h: #ifndef APPLEFARMER_H #define APPLEFARMER_H class AppleFarmer { public: AppleFarmer(int initVal); // ... more functions here private: // ... need to put data }; #endif // APPLEFARMER_H Thanks!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
