Question

C++

Project Overview

The 18th season of the reality series Hell's Kitchen began airing on Sep 28th, 2018 on Fox. Suppose you are working for the boss, Chef Gordon Ramsay, your job is to create an efficient system that generates a menu. Since in Hell's Kitchen, menu changes every day, your system should easily add items to the menu. Therefore, dynamically allocated arrays would be an excellent solution.

The Dish Class (Header File)

You need to create a struct called Dish, which contains the following attributes and functions.

1. A string variable called dishName, which represents the name of the dish (e.g. "Chicken Parmesan"). We assume that the length of dishName is no more than 60 characters. Please note that dishName could have spaces.

2. An integer variable called dishType, which represents if the Dish is an appetizer (value 0), entrée (value 1), or dessert (value 2).

3. A double variable called dishPrice, which represents the price (in USD) of the dish. We assume that the price of each Dish will be no more than $30.00.

4. A default constructor (no arguments) and a constructor that takes in 3 arguments, which are the name, price and type of the Dish. You can combine the two constructors into one constructor with default values of the parameters.

5. Overload the operator >>, which reads in the data from an input stream and store the data as a Dish. Since the left operand of operator >> has to be an istream, the function must be defined outside the structDish. The table below shows the data fields in the input file. Since the dish name may contain spaces, you probably need to review section 9.7 in your textbook, the getline() function. Please note that the getline() function will add a newline character at the end of the string.

Sample Input File Input Format Appetizer 7.99 Chicken Caesar Salad Entree 9.99 SteakTaco <dishType> <dishPrice> <dishName>

The Menu Class (Header File)

Create a class called Menu, which is essentially an array of Dishes. You must use dynamic array (pointers) here. Using static array or vector instead of dynamic array will result in an immediate zero for this project.

1. In the private section, there are two integer variables, capacity and used, and a pointer to Dishes, which is the dynamic array.

2. You need a default constructor (no arguments) which initializes the capacity to be 10, used to be 0, and the pointer to be a dynamic array of size 10.

3. Since you use pointer in the class, you must overload the assignment operator, copy constructor and destructor.

4. You need a private member function called resize(), which is called when adding a Dish to the array yet the array is full and has no more room for the new Dish. The function takes no arguments and does notreturn a value. The function doubles the capacity of the current array. However, no matter how you change the capacity of the array, the data stored in the array cannot be lost.

5. Overload the operator +, which takes a Dish as the only argument. The function generates a new Menuwhich is the current Menu plus the Dish added to the end. The function does not change the current Menunor the Dish. The function returns the new created Menu.

6. Using operator +, overload operator +=, which takes a Dish as the only argument. The function assigns the current Menu with current Menu + the Dish. The function does not change the Dish. The function returns *this.

7. Overload the operator <<, which is a friend function that outputs a Menu. The function outputs all the Dishes in the Menu in appetizer, entrée, and dessert categories. You can assume that there is at least one appetizer, one entrée and one dessert in the Menu. Please see the sample output file for the output format.

The main() Function (.CPP)

#include "Dish.h"
#include "Menu.h"

#include
#include

using namespace std;

// You cannot change anything in the main() function.
int main() {
ifstream fin("input.txt");
if (!fin) {
cout << "Error: input file does not exist." << endl;
system("pause");
return -1;
}
ofstream fout("output.txt");
Menu menu;
Dish dish;
while (fin >> dish) { menu += dish; }
fout << menu;
fin.close();
fout.close();
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Dish.h

#ifndef DISH_H_

#define DISH_H_

#include <iostream>

#include <cctype>

using namespace std;

struct Dish

{

               string dishName;

               int dishType;

               double dishPrice;

               Dish()

               {

                              dishName = "";

                              dishType=0;

                              dishPrice=0;

               }

               Dish(string name,int type,double price)

               {

                              dishName = name;

                              dishType = type;

                              dishPrice = price;

               }

};

istream& operator>>(istream &in, Dish &dish)

{

               string type;

               in>>type>>dish.dishPrice;

               getline(in,dish.dishName);

               if(isspace(dish.dishName.at(dish.dishName.length()-1))) // if string contains \n at the end

                              dish.dishName = dish.dishName.substr(0,dish.dishName.length()-1); //remove \n from string

               if(type.compare("Appetizer") == 0)

                              dish.dishType = 0;

               else if(type.compare("Entree") == 0)

                              dish.dishType = 1;

               else

                              dish.dishType = 2;

               return in;

}

#endif /* DISH_H_ *//

//end of Dish.h

// Menu.h

#ifndef MENU_H_

#define MENU_H_

#include "Dish.h"

class Menu

{

private:

               Dish *dishes;

               int capacity ;

               int used;

               void resize();

public:

               Menu();

               Menu(const Menu &other);

               Menu operator=(const Menu &other);

               ~Menu();

               Menu operator+(const Dish &dish);

               Menu operator+=(const Dish &dish);

               friend ostream& operator<<(ostream &out, const Menu &menu);

};

Menu::Menu()

{

               capacity = 10;

               used = 0;

               dishes = new Dish[capacity];

}

Menu::Menu(const Menu &other)

{

               capacity = other.capacity;

               used = other.used;

               dishes = new Dish[capacity];

               for(int i=0;i<used;i++)

               {

                              dishes[i].dishName = other.dishes[i].dishName;

                              dishes[i].dishPrice = other.dishes[i].dishPrice;

                              dishes[i].dishType = other.dishes[i].dishType;

               }

}

Menu Menu::operator=(const Menu &other)

{

               if(this != &other)

               {

                              delete [] dishes;

                              capacity = other.capacity;

                              used = other.used;

                              dishes = new Dish[capacity];

                              for(int i=0;i<used;i++)

                              {

                                             dishes[i].dishName = other.dishes[i].dishName;

                                             dishes[i].dishPrice = other.dishes[i].dishPrice;

                                             dishes[i].dishType = other.dishes[i].dishType;

                              }

               }

               return *this;

}

Menu::~Menu()

{

               delete [] dishes;

}

Menu Menu:: operator+(const Dish &dish)

{

               if(used == capacity)

               {

                              resize();

               }

               dishes[used].dishName = dish.dishName;

               dishes[used].dishPrice = dish.dishPrice;

               dishes[used].dishType = dish.dishType;

               used++;

               return *this;

}

Menu Menu::operator+=(const Dish &dish)

{

               if(used == capacity)

               {

                              resize();

               }

               dishes[used].dishName = dish.dishName;

               dishes[used].dishPrice = dish.dishPrice;

               dishes[used].dishType = dish.dishType;

               used++;

               return *this;

}

ostream& operator<<(ostream &out, const Menu &menu)

{

               out<<"Menu :"<<endl;

               out<<"Appetizer :"<<endl;

               for(int i=0;i<menu.used;i++)

               {

                              if(menu.dishes[i].dishType == 0)

                                             out<<menu.dishes[i].dishName<<" $"<<menu.dishes[i].dishPrice<<endl;

               }

               out<<"Entrees :"<<endl;

               for(int i=0;i<menu.used;i++)

               {

                              if(menu.dishes[i].dishType == 1)

                                             out<<menu.dishes[i].dishName<<" $"<<menu.dishes[i].dishPrice<<endl;

               }

               out<<"Dessert :"<<endl;

               for(int i=0;i<menu.used;i++)

               {

                              if(menu.dishes[i].dishType == 2)

                                             out<<menu.dishes[i].dishName<<" $"<<menu.dishes[i].dishPrice<<endl;

               }

               return out;

}

void Menu::resize()

{

               int new_capacity = 2*capacity;

               Dish *new_dishes = new Dish[new_capacity];

               capacity = new_capacity;

               for(int i=0;i<used;i++)

                              new_dishes[i] = dishes[i];

               delete [] dishes;

               dishes = new_dishes;

}

#endif /* MENU_H_ */

//end of Menu.h

//main.cpp

#include "Dish.h"

#include "Menu.h"

#include <iostream>

#include <fstream>

using namespace std;

// You cannot change anything in the main() function.

int main() {

       ifstream fin("input.txt");

       if (!fin) {

       cout << "Error: input file does not exist." << endl;

       system("pause");

       return -1;

       }

       ofstream fout("output.txt");

       Menu menu;

       Dish dish;

       while (fin >> dish) { menu += dish; }

       fout << menu;

       fin.close();

       fout.close();

       return 0;

}

//end of main.cpp

Output:

Input file:

Appetizer 7.99 Chicken Caesar Salad Entree 9.99 Steak Taco Dessert 5.99 Chocolate Brownie Entree 7.99 Nachos

Output file:

Add a comment
Know the answer?
Add Answer to:
C++ Project Overview The 18th season of the reality series Hell's Kitchen began airing on Sep 28th, 2018 on Fox. Suppose you are working for the boss, Chef Gordon Ramsay, your job is to create an...
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
  • IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5...

    IN c++ i post this question 5 times. hope this time somebody answer.. 16.5 Homework 5 OVERVIEW This homework builds on Hw4(given in bottom) We will modify the classes to do a few new things We will add copy constructors to the Antique and Merchant classes. We will overload the == operator for Antique and Merchant classes. We will overload the + operator for the antique class. We will add a new class the "MerchantGuild." Please use the provided templates...

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