2

I have a vector orderQueue that is stored in a class called OrderFood and a function called processUserOption in another class called MerkelMain. I am trying to get processUserOption to store orders in that orderQueue vector using the push_back function. How do I store orders in the orderQueue vector of the OrderFood class?

OrderFood.h

#pragma once
#include <string>
#include <vector>
#include "OrderEntry.h"
#include "CSVReader.h"
class OrderFood
{
 public:
 OrderFood(std::string filename);
 std::vector<OrderEntry> getOrders(OrderType type,
 int qty);
 private:
 std::vector<OrderEntry> orderQueue;
};

MerkelMain.cpp

#include <iostream>
#include <vector>
#include "OrderEntry.h"
#include "MerkelMain.h"
#include "OrderFood.h"
MerkelMain::MerkelMain()
{
}
void MerkelMain::init()
{
 std::string input;
 while (true)
 {
 printMenu(); 
 input = getUserOption();
 processUserOption(input);
 }
}
void MerkelMain::printMenu()
{
 std::cout << "Welcome to Restaurant Firebird" << std::endl;
 std::cout << "a . Order a plate of chicken rice" << std::endl;
 std::cout << "b . Order a plat for wanton noodles" << std::endl;
 std::cout << "c . Order a cup of Coffee" << std::endl;
 std::cout << "d . Order a cup of Tea" << std::endl;
 std::cout << "e . Repeat Order" << std::endl;
 std::cout << "f . Exit Menu" << std::endl;
 std::cout << "------------------------------------" << std::endl;
 std::cout << "Type an option from a-f" << std::endl;
}
std::string MerkelMain::getUserOption()
{
 std::string choice;
 std::cin >> choice;
 return choice;
}
int MerkelMain::getQuantity()
{
 int qty;
 std::cin >> qty;
 return qty;
}
void MerkelMain::processUserOption(std::string choice)
{
 std::endl(std::cout);
 if (choice == "a")
 {
 std::cout << "How many plates of chicken rice would you like to order?" << std::endl;
 OrderType order = OrderType::chickenrice;
 int qty = getQuantity();
 std::endl(std::cout);
 std::cout << "Thank you for your order of " << qty << " plates of Chicken Rice." << std::endl;
 std::endl(std::cout);
 OrderEntry food_order = OrderEntry(qty, order);
 orderQueue.push_back(food_order)
 }
 if (choice == "b")
 {
 std::cout << "How many plates of wanton noodles would you like to order?" << std::endl;
 OrderType order = OrderType::wantonnoodle;
 int qty = getQuantity();
 std::endl(std::cout);
 std::cout << "Thank you for your order of " << qty << " plates of Wanton Noodles." << std::endl;
 std::endl(std::cout);
 OrderEntry food_order = OrderEntry(qty, order);
 orderQueue.push_back(food_order);
 }
 if (choice == "c")
 {
 std::cout << "How many cups of Coffee would you like to order?" << std::endl;
 OrderType order = OrderType::coffee;
 int qty = getQuantity();
 std::endl(std::cout);
 std::cout << "Thank you for your order of " << qty << " cups of Coffee." << std::endl;
 OrderEntry food_order = OrderEntry(qty, order);
 orderQueue.push_back(food_order);
 }
 if (choice == "d")
 {
 std::cout << "How many cups of Tea would you like to order?" << std::endl;
 OrderType order = OrderType::tea;
 int qty = getQuantity();
 std::endl(std::cout);
 std::cout << "Thank you for your order of " << qty << " cups of Tea." << std::endl;
 std::endl(std::cout);
 OrderEntry food_order = OrderEntry(qty, order);
 orderQueue.push_back(food_order);
 }
 if (choice == "e")
 {
 std::cout << "You have ordered: " << std::endl;
 for (unsigned int i = 0; i < orderQueue.size(); ++i)
 {
 std::cout << orderQueue[i].qty << " " << OrderEntry::orderTypeToString(orderQueue[i].orderType) << std::endl;
 }
 std::endl(std::cout);
 }
 if (choice == "f")
 {
 std::cout << "Thank you and see you again soon." << std::endl;
 
 }
}
asked Nov 11, 2020 at 5:25
1
  • I would say use the public getOrders function of the OrderFood class, but that returns by value. So I'd suggest changing it to return by reference (and possibly adding a const overload that returns by const reference) and using that. Commented Nov 11, 2020 at 5:27

1 Answer 1

1

You can make class MerkelMain a friend of class OrderFood

class OrderFood
{
 public:
 OrderFood(std::string filename);
 std::vector<OrderEntry> getOrders(OrderType type,
 int qty);
 private:
 std::vector<OrderEntry> orderQueue;
 friend class MerkelMain; // <-- HERE
};

then delegate an OrderFood object stored in MerkelMain

class MerkelMain {
 private:
 Orderfood order; // <-- HERE
 ...
};

and then freely invoke the function in MerkelMain methods.

void MerkelMain::processUserOption(std::string choice) {
 ...
 OrderEntry food_order = OrderEntry(qty, order);
 order.orderQueue.push_back(std::move(food_order)); // <-- HERE
 ...
}
answered Nov 11, 2020 at 5:45
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.