1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
struct Menu {
std::string name;
double price {};
size_t qty {};
};
enum {FOOD = 0, DRINK, MAXTYPE = DRINK};
using Men = std::vector<Menu>;
using Menus = Men[MAXTYPE + 1];
int getInt(const std::string& prm)
{
int i {};
while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
std::cout << "Not an integer\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return i;
}
void read(std::istream& is, Men& vm)
{
for (Menu m; std::getline(is >> std::ws, m.name, '|') >> m.price; vm.push_back(m));
}
void display(const Menus& m)
{
std::cout << std::setw(134) << std::setfill('-') << '\n';
std::cout << "| SNACKS AND DRINKS | \n";
std::cout << std::setw(134) << std::setfill('-') << '\n';
std::cout << "| SNACKS AVALABLE | PRICE | DRINKS AVAILABLE | PRICE |\n";
std::cout << std::setw(134) << std::setfill('-') << '\n';
std::cout << "| | | | |\n";
auto& Food {m[FOOD]};
auto& Drink {m[DRINK]};
std::cout << std::setfill(' ') << std::left;
for (size_t e = 0, lst = std::max(Food.size(), Drink.size()); e < lst; ++e) {
if (e < Food.size())
std::cout << "|(" << std::setw(2) << std::right << e + 1 << std::left << ".) " << std::setw(50) << Food[e].name << '|' << std::setw(11) << std::setprecision(2) << std::fixed << Food[e].price;
else
std::cout << '|' << std::setw(56) << ' ' << '|' << std::setw(11) << ' ';
if (e < Drink.size())
std::cout << "|(" << std::setw(2) << std::right << e + 1 << std::left << ".) " << std::setw(44) << Drink[e].name << '|' << std::setw(11) << std::setprecision(2) << std::fixed << Drink[e].price << "|\n";
else
std::cout << '|' << std::setw(50) << ' ' << '|' << std::setw(11) << ' ' << "|\n";
}
}
void add(const Menus& m, Men& order)
{
int types {};
do {
do {
types = getInt("[Food = 1, Drink = 2, Receipt = 0] : ");
} while ((types < 0 || types > 2) && (std::cout << "Invalid option\n"));
if (types) {
int opt {};
do {
opt = getInt("Enter menu number: ");
} while ((opt < 1 || opt > m[types - 1].size()) && (std::cout << "Invalid menu number\n"));
const int qty {getInt("Enter quantity: ")};
order.push_back(m[types - 1][opt - 1]);
order.back().qty = qty;
}
} while (types);
}
void receipt(const Men& order)
{
double total {};
std::cout << "\nReceipt\n\n";
std::cout << std::setw(25) << "Name" << std::setw(10) << "Price" << std::setw(10) << "Quantity" << "Total\n";
for (const auto& [nam, prce, qty] : order) {
std::cout << std::setw(25) << nam << std::setw(10) << std::fixed << std::setprecision(2) << prce << std::setw(10) << qty << std::setw(10) << prce * qty << '\n';
total += prce * qty;
}
std::cout << "\nTotal is " << total << '\n';
}
void food_drinks()
{
std::ifstream foodfs("food.txt");
std::ifstream drinkfs("drink.txt");
if (!foodfs || !drinkfs) {
std::cout << "Cannot open input files\n";
return;
}
Menus menus {};
Men order;
read(foodfs, menus[FOOD]);
read(drinkfs, menus[DRINK]);
display(menus);
add(menus, order);
receipt(order);
}
int main()
{
food_drinks();
}
|