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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream> // stringstream
#include <cctype> // isalph, isdigit
using namespace std;
class Customer
{
string custName; // Customer name
string custAddress; //Customer Address
void info()
{
cout << setw(10) << left << "Name" << custName << "\n"
<< setw(10) << left << "Address" << custAddress << "\n" << endl;
}
void simpleInfo()
{
cout << "Hello: " << custName << " , Address: " << custAddress << endl;
}
Customer snafu()
{
std::string custName;
std::string custAddress;
cout << "Please enter your name ==> ";
std::getline(std::cin, custName);
cout << "Please enter your address ==> ";
std::getline(std::cin, custAddress);
return Customer(custName, custAddress);
}
Customer() {}
~Customer() {}
Customer(string n, string a) {
custName = n;
custAddress = a;
}
}; // END OF CLASS
struct Beverage
{
string name; // The name
int quantity{}; // The quantity
double price{}; //The price
void info()
{
cout << setw(10) << left << "Name" << name << "\n"
<< setw(10) << left << "Price" << "$" << price << "\n"
<< setw(10) << left << "Quantity" << quantity << "\n"
<< setw(10) << left << "Total" << "$"
<< quantity * price << "\n" << endl;
}
void simpleInfo()
{
cout << "Selection: " << name << ", price per unit: $" << price << endl;
}
Beverage() {}
~Beverage() {}
Beverage(string n, double p) {
name = n;
price = p;
}
}; // END OF STRUCT
double totalCost(Beverage beverages[], int numOfBeverages);
void orderSummary(Beverage beverages[], int numOfBeverages);
bool isAllDigits(string input);
int getValidNumber(string text, int min, int max);
void doShopping();
void displayMenu(string userName) //Menu function
{
cout << endl << endl
<< userName
<< ", Please select the beverage you would like to purchase from the menu: "
<< endl;
cout << "Drink Menu" << endl;
cout << "========" << endl;
cout << "1 - Water $1.45" << endl;
cout << "2 - Soda $2.98" << endl;
cout << "3 - Iced Tea $3.29" << endl;
cout << "X - Exit " << endl << endl;
}
int main(void)
{
doShopping();
return 0;
}
int getQuantity() //Quantity function
{
int quantity = 0;
cout << "Enter quantity : ";
cin >> quantity;
return quantity;
}
bool isAllDigits(string input)
{
for (unsigned i = 0; i < input.length(); i++) {
if (!isdigit(input[i])) {
return false;
}
}
return true;
}
int getValidNumber(string text, int min, int max)
{
string input{};
int num{};
while (true)
{
cout << text << " [" << min << " - " << max << "]: ";
getline(cin, input);
stringstream ss(input);
if (!isAllDigits(ss.str())) {
cout << "\nInvalid input. Try again!" << endl;
}
else {
if (isAllDigits(ss.str()) && ss >> num) {
if (num >= min && num <= max) {
break;
}
else {
std::cout << "\nOut of range. Try again!" << endl;
}
}
}
}
return num;
}
double totalCost(Beverage beverages[], int numOfBeverages) //Total cost function
{
double totalCost = 0;
for (int i = 0; i < numOfBeverages; i++) {
totalCost += (beverages[i].price*beverages[i].quantity);
}
return totalCost;
}
void orderSummary(Beverage beverages[], int numOfBeverages) //Summary function
{
cout << "\n======= ORDER SUMMARY ====" << endl;
cout << "Items selected" << endl << endl;
for (int i = 0; i < numOfBeverages; i++) {
// only show beverages that has at least one order
if (beverages[i].quantity > 0) {
beverages[i].info();
}
}
}
void doShopping() //Shopping function
{
// declare our beverages
Beverage
water("Water", 1.45),
soda("Soda", 2.98),
iceTea("Ice Tea", 3.29);
// Beverage array to hold the beverages
Beverage beverages[] = { water, soda, iceTea };
char selection = ' ';
Customer patron = snafu(); // get info, create patron
patron.simpleInfo(); // greet patron
/*string name = "";
string address = "";
//Ask user for her/his name
cout << "Please enter your name ==> ";
getline(cin, name);
cout << "Please enter your address ==> ";
getline(cin, address);
//display user name
cout << "Hello " << name << endl;
cout << "Address: " << address << endl;
*/
do
{
system("cls");
// display menu
displayMenu(name);
// read user selection
cout << "Your selection: ";
cin >> selection;
cin.get(); // takes last ENTER character in the stream
switch (selection)
{
case '1':
beverages[0].simpleInfo();
// assuming the customer can shop at least 1 and max 100
beverages[0].quantity += getValidNumber("Enter quantity", 1, 100);
break;
case '2':
beverages[1].simpleInfo();
beverages[1].quantity += getValidNumber("Enter quantity", 1, 100);
break;
case '3':
beverages[2].simpleInfo();
beverages[2].quantity += getValidNumber("Enter quantity", 1, 100);
break;
case 'X':
case 'x':
orderSummary(beverages, 3);
if (totalCost(beverages, 3) > 0) {
cout << "\nGrand total = $" << totalCost(beverages, 3) << endl;
}
cout << "\nThank you for your purchase, " << name << " Come back soon!!!" << endl;
break;
// other than 1, 2, 3 and X...
default: cout << "Invalid selection. Please try again";
cin.get();
// no break in the default case
}
cout << endl << endl;
} while (selection != 'X' && selection != 'x'); // 'X' or 'x' displays the summary
system("pause"); //Pauses the system until a key is hit, The the program will end.
}
|