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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
class Customer {
const std::string custName, custAddress;
public:
void custInfo(std::ostream &) const
{
std::cout << "Name" << custName << "\n"
"Address" << custAddress << "\n" << std::endl;
}
void custWelcome(std::ostream &) const
{
std::cout << "\nHello " << custName << "\nAddress: " << custAddress << std::endl;
}
std::string custValue() const
{
return custName;
}
Customer(const std::string &inputCustName, const std::string &inputCustAddress)
: custName(inputCustName), custAddress(inputCustAddress)
{
}
};
struct Beverage {
const std::string name;
double price;
void bevInfo(std::ostream &) const
{
std::cout << "Name" << name << "\n"
<< "Price" << "$" << price << "\n"
<< "Total" << "$" << std::endl;
}
void bevSelect(std::ostream &) const
{
std::cout << "Selection: " << name << ", price per unit: $" << price << std::endl;
}
Beverage(const std::string &bevName, double bevPrice)
: name(bevName), price(bevPrice)
{
}
};
const int AVAILABLE_PRODUCTS_COUNT = 3;
const Beverage AVAILABLE_PRODUCTS[AVAILABLE_PRODUCTS_COUNT] = {
Beverage("Water", 1.45),
Beverage("Soda", 2.98),
Beverage("Ice Tea", 3.29)
};
typedef int Quantities[AVAILABLE_PRODUCTS_COUNT];
double totalCost(Quantities);
void orderSummary(Quantities);
std::string getStringFromUser(const std::string &prompt);
char getCharFromUser(const std::string &prompt);
Customer getCustomerFromUser();
void displayMenu(const Customer &);
void loadProductQuantity(const Beverage &, Quantities);
void doneShowOrder(const Customer &, Quantities);
void getStringFromUser(std::string &prompt)
{
}
void getCharFromUser(std::string &prompt)
{
std::cout << std::endl << std::endl
<< &prompt
<< ", Please select the beverage you would like to purchase from the menu: "
<< std::endl;
std::cout << "Drink Menu" << std::endl;
std::cout << "========" << std::endl;
std::cout << "1 - Water $1.45" << std::endl;
std::cout << "2 - Soda $2.98" << std::endl;
std::cout << "3 - Iced Tea $3.29" << std::endl;
std::cout << "X - Exit " << std::endl << std::endl;
}
Customer getCustomerFromUser()
{
std::string custName;
std::string custAddress;
std::cout << "Please enter your name ==> ";
std::getline(std::cin, custName);
std::cout << "Please enter your address ==> ";
std::getline(std::cin, custAddress);
return Customer(custName, custAddress);
}
int main(void) {
Quantities ordered = { 0 };
Customer patron = getCustomerFromUser();
patron.custInfo(std::cout);
bool done = false;
while (!done) {
displayMenu(patron);
switch (getCharFromUser("Your selection: ")) {
case '1':
loadProductQuantity(AVAILABLE_PRODUCTS[0], ordered);
break;
case '2':
loadProductQuantity(AVAILABLE_PRODUCTS[1], ordered);
break;
case '3':
loadProductQuantity(AVAILABLE_PRODUCTS[2], ordered);
break;
case 'X': case 'x':
done = true;
break;
default:
std::cout << "Invalid selection. Please try again";
std::cin.get();
// no break in the default case
}
}
doneShowOrder(patron, ordered);
return 0;
}
void orderSummary(Quantities ordered) {
std::cout << std::endl;
std::cout << "======= ORDER SUMMARY ====" << std::endl;
std::cout << "Items selected" << std::endl;
for (int i = 0; i < AVAILABLE_PRODUCTS_COUNT; i++) {
// only show beverages that has at least one order
if (ordered[i] > 0) {
std::cout << "You selcted: " << std::endl;// print something for AVAILABLE_PRODUCTS[i]
}
}
}
|