Question

Writing a program to represent a college food cabinet. How would i start this Shelf Class....

Writing a program to represent a college food cabinet.

How would i start this Shelf Class.

The purpose would be to store an array of Food objects. Each shelf created also needs a different number of food objects, so i know dynamic memory allocation should be used.

Here are the variable that are in the Shelf class:

The name of the Shelf
 A pointer to the array of Food objects, and because pointers don’t have any addition info on
their own…
 A maximum capacity of the array
 A count of how many Food objects you currently have
In addition, you should create the following functions (plus the special functions—a copy constructor,
assignment operator, and destructor):

// Store a food object in the first available spot

void addFood(const Food *f);

//Show the name of this object and all of its food

void ShowInventory() const;

//Accessors

const Food* GetFoodList() const;

unsigned int GetCapacity() const;

unsigned int GetCount() const;

const char* GetName() const;

Here is the main.cpp

void TestOne(Food *v);
void TestTwo(Food *v);

int main()
{
   // Initialize some data. It's hard-coded here, but this data could come from a file, database, etc
   Food foods[] =
   {
       Food("Vegetable", "Avocado", 2019, 15, 5),
       Food("Cheese", "Moldy Cheddar", 1999, 7, 400),
       Food("Breakfast", "Pop-Tart", 2025, 5, 754),
       Food("Cereal", "Lucky Charms", 2001, 4, 600),
       Food("Frozen", "Cheese Pizza", 2021, 10, 1001),
       Food("Very-Processed", "Ramen", 3005, 1, 9999),
       Food("Expensive", "Caviar", 2017, 180, 150),
   };

   int testNum;
   cin >> testNum;

   if (testNum == 1)
       TestOne(foods);
   else if (testNum == 2)
       TestTwo(foods);

   return 0;
}

void TestOne(Food *foods)
{
   // Shelfs to store the foods
   Shelf shelf("\"Healthy-Food Shelf\"", 3); //calls the constructor with parameters
   shelf.AddFood(&foods[0]);
   shelf.AddFood(&foods[1]);
   shelf.AddFood(&foods[2]);

   Shelf secondary("\"Treat Your Shelf\"", 4);
   secondary.AddFood(&foods[3]);
   secondary.AddFood(&foods[4]);
   secondary.AddFood(&foods[5]);
   secondary.AddFood(&foods[6]);

   // A "parent" object to store the Shelfs
   Cabinet cabinet("COP3503 Dormroom Cabinet", 2);
   cabinet.AddShelf(&shelf); //adding show rooms is the issue
   cabinet.AddShelf(&secondary);

   cabinet.ShowInventory();
}

void TestTwo(Food *foods)
{
   // Shelfs to store the foods
   Shelf shelf("\"Healthy-Food Shelf\"", 3);
   shelf.AddFood(&foods[0]);
   shelf.AddFood(&foods[1]);

   Shelf secondary("\"Treat Your Shelf\"", 4);

   secondary.AddFood(&foods[4]);
   secondary.AddFood(&foods[5]);

   Shelf third("\"Treat Your Shelf\"", 4);
   third.AddFood(&foods[3]);
   // A "parent" object to store the Shelfs
   Cabinet cabinet("COP3503 Dormroom Cabinet", 3);
   cabinet.AddShelf(&shelf);
   cabinet.AddShelf(&secondary);
   cabinet.AddShelf(&third);

   cout << "Using just the GetAveragePrice() function\n\n";

   cout << "Average price of the food in the cabinet: $" << std::fixed << std::setprecision(2);
   cout << cabinet.GetAveragePrice();
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <iomanip>

using namespace std;

class Food {
    private:
    string name;
    string brand;
    int year, price , calorie;

    public:
    Food(string n, string br, int x, int y, int z) {
        name = n;
        brand = br;
        year = x;
        price = y;
        calorie = z;
    }

    string getName() {
        return name;
    }

    int getPrice() {
        return price;
    }
};

class Shelf {
    private:
    int capacity;
    int size;
    Food **foodRow;
    string name;

    public:
    Shelf(string n, int s) {
        name = n;
        capacity = s;
        foodRow = new Food*[s];
        size = 0;
    }
    void AddFood(Food *f) {
        if(size < capacity) {
            foodRow[size++] = f;
        }
    }
    void printShelf() {
        cout << "Shelf: " << name << endl;
        for(int i=0; i<size; i++) {
            cout << foodRow[i]->getName() << endl;
        }
    }
    int getItemCount() {
        return size;
    }
    int getTotalPrice() {
        int p = 0;
        for(int i=0; i<size; i++) {
            p += foodRow[i]->getPrice();
        }
        return p;
    }
};

class Cabinet {
    private:
    int capacity, size;
    Shelf **shelvesRow;
    string name;

    public:
    Cabinet(string n, int s) {
        name = n;
        capacity = s;
        shelvesRow = new Shelf*[s];
        size = 0;
    }
    void AddShelf(Shelf *f) {
        if(size < capacity) {
            shelvesRow[size++] = f;
        }
    }
    void ShowInventory() {
        cout << "+++++++++++++++++++++++++++++++++" << endl;
        cout << "Cabinet: " << name << endl;

        for(int i=0; i<size; i++) {
            cout << endl;
            shelvesRow[i]->printShelf();
        }

        cout << "+++++++++++++++++++++++++++++++++" << endl;
    }

    double GetAveragePrice() {
        double totalPrice = 0;
        int count = 0;

        for(int i=0; i<size; i++) {
            totalPrice += shelvesRow[i]->getTotalPrice();
            count += shelvesRow[i]->getItemCount();
        }

        return totalPrice/count;
    }
};
void TestOne(Food *v);
void TestTwo(Food *v);

int main()
{
   // Initialize some data. It's hard-coded here, but this data could come from a file, database, etc
   Food foods[] =
   {
       Food("Vegetable", "Avocado", 2019, 15, 5),
       Food("Cheese", "Moldy Cheddar", 1999, 7, 400),
       Food("Breakfast", "Pop-Tart", 2025, 5, 754),
       Food("Cereal", "Lucky Charms", 2001, 4, 600),
       Food("Frozen", "Cheese Pizza", 2021, 10, 1001),
       Food("Very-Processed", "Ramen", 3005, 1, 9999),
       Food("Expensive", "Caviar", 2017, 180, 150),
   };

   int testNum;
   cin >> testNum;

   if (testNum == 1)
       TestOne(foods);
   else if (testNum == 2)
       TestTwo(foods);

   return 0;
}

void TestOne(Food *foods)
{
   // Shelfs to store the foods
   Shelf shelf(""Healthy-Food Shelf"", 3); //calls the constructor with parameters
   shelf.AddFood(&foods[0]);
   shelf.AddFood(&foods[1]);
   shelf.AddFood(&foods[2]);

   Shelf secondary(""Treat Your Shelf"", 4);
   secondary.AddFood(&foods[3]);
   secondary.AddFood(&foods[4]);
   secondary.AddFood(&foods[5]);
   secondary.AddFood(&foods[6]);

   // A "parent" object to store the Shelfs
   Cabinet cabinet("COP3503 Dormroom Cabinet", 2);
   cabinet.AddShelf(&shelf); //adding show rooms is the issue
   cabinet.AddShelf(&secondary);

   cabinet.ShowInventory();
}

void TestTwo(Food *foods)
{
   // Shelfs to store the foods
   Shelf shelf(""Healthy-Food Shelf"", 3);
   shelf.AddFood(&foods[0]);
   shelf.AddFood(&foods[1]);

   Shelf secondary(""Treat Your Shelf"", 4);

   secondary.AddFood(&foods[4]);
   secondary.AddFood(&foods[5]);

   Shelf third(""Treat Your Shelf"", 4);
   third.AddFood(&foods[3]);
   // A "parent" object to store the Shelfs
   Cabinet cabinet("COP3503 Dormroom Cabinet", 3);
   cabinet.AddShelf(&shelf);
   cabinet.AddShelf(&secondary);
   cabinet.AddShelf(&third);

   cout << "Using just the GetAveragePrice() function

";

   cout << "Average price of the food in the cabinet: $" << std::fixed << std::setprecision(2);
   cout << cabinet.GetAveragePrice();
}

becz you have not shared the food class, so i have created mine.. hope they will work fine for you, or else, you can use your class on same lines with small changes. please upvote.

Add a comment
Know the answer?
Add Answer to:
Writing a program to represent a college food cabinet. How would i start this Shelf Class....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This is for my c++ class and I would really appreciate the help, Thank you! Complete...

    This is for my c++ class and I would really appreciate the help, Thank you! Complete the definitions of the functions for the ConcessionStand class in the ConcessionStand.cpp file. The class definition and function prototypes are in the provided ConcessionStand.h header file. A testing program is in the provided main.cpp file. You don’t need to change anything in ConcessionStand.h or main.cpp, unless you want to play with different options in the main.cpp program. ___________________ Main.cpp ____________________ #include "ConcessionStand.h" #include <iostream>...

  • Greetings, everybody I already started working in this program. I just need someone to help me...

    Greetings, everybody I already started working in this program. I just need someone to help me complete this part of the assignment (my program has 4 parts of code: main.cpp; Student.cpp; Student.h; StudentGrades.cpp; StudentGrades.h) Just Modify only the StudentGrades.h and StudentGrades.cpp files to correct all defect you will need to provide implementation for the copy constructor, assignment operator, and destructor for the StudentGrades class. Here are my files: (1) Main.cpp #include <iostream> #include <string> #include "StudentGrades.h" using namespace std; int...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

    Write a C++ Program. You have a following class as a header file (dayType.h) and main(). #ifndef H_dayType #define H_dayType #include <string> using namespace std; class dayType { public:     static string weekDays[7];     void print() const;     string nextDay() const;     string prevDay() const;     void addDay(int nDays);     void setDay(string d);     string getDay() const;     dayType();     dayType(string d); private:     string weekDay; }; #endif /* // Name: Your Name // ID: Your ID */ #include <iostream>...

  • Sometimes when I sit in restaurants waiting on food, I request the children’s menu to play...

    Sometimes when I sit in restaurants waiting on food, I request the children’s menu to play the games. One of my favorites is the word puzzle in which you search for words hidden in a scramble of letters. I began to work out a solution to this game programmatically. That seemed a little simple, so I decided to have you solve the problem of a scramble of integers, finding the sub-row or sub-column which sums to another integer. You will...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • I need help implementing 3 function prototypes with using operator overloading in a class. I have...

    I need help implementing 3 function prototypes with using operator overloading in a class. I have bolded the section I need help with since the cpp file is bigger than I expected. Any comments throughout would be extremely helpful. Time.cpp file #include "Time.h" #include #include #include // The class name is Time. This defines a class for keeping time in hours, minutes, and AM/PM indicator. // You should create 3 private member variables for this class. An integer variable for...

  • Assume that we are writing a game like Minecraft in C++. We will have a World...

    Assume that we are writing a game like Minecraft in C++. We will have a World class that stores a 3-dimensional array of "Voxels". A voxel is a simple element of volume (think Pixel, but 3D). Your task for this assignment is to create a world, set some Voxels and display them. You are provided with a simple class for a Voxel that stores a RGB color value, as well as a character code for the type of element. You...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT