#include <iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;
int main()
{int n=0;// Number of items bool flag=true;// flag use for indicate that if customer still want to continue shopping or stop int sum=0;// summary of price int drinkChoice=0;// number in menu for each coffee int stopChoice=0;// choice of customer to continue or stop string lineCheck;// string for safely getting input int coffee[7];// coffee counter string menu[7];// string conain menu menu[0]="Espresso"; menu[1]="Americano"; menu[2]="Latte"; menu[3]="Cappuccino"; menu[4]="Mocha"; menu[5]="Chocolate"; menu[6]="White Choco"; for(int i=0;i<=6;i++)// initial coffee counter coffee[i]=0; //---------------------------------------------------------------
while(flag)// while customer still want to continue shopping {// show them a menu cout << "##############################\n"; cout << "############ Menu ############\n"; cout << "##############################\n"; cout << "## 1) Espresso 40 ##\n"; cout << "## 2) Americano 40 ##\n"; cout << "## 3) Latte 45 ##\n"; cout << "## 4) Cappuccino 45 ##\n"; cout << "## 5) Mocha 50 ##\n"; cout << "## 6) Chocolate 50 ##\n"; cout << "## 7) White Choco 60 ##\n"; cout << "##############################\n"; cout << "Select your drink (1-7): "; while(drinkChoice==0)// if customer still can't make a correct choice { getline(cin, lineCheck);// get input stringstream linestream(lineCheck);// make it carefully drinkChoice = atoi(lineCheck.c_str());// convert it into integer (return 0 if it not an ingeter)
// check if user input drinkChoice correctly if(drinkChoice!=0) { // price calculation and coffee count switch(drinkChoice) {case 1 :sum += 40; coffee[0]++; break; case 2 :sum += 40; coffee[1]++; break; case 3 :sum += 45; coffee[2]++; break; case 4 :sum += 45; coffee[3]++; break; case 5 :sum += 50; coffee[4]++; break; case 6 :sum += 50; coffee[5]++; break; case 7 :sum += 60; coffee[6]++; break; default :// in case that input out of range cout << "Please input a number between 1-7 !\n"; cout << "Select your drink (1-7): "; drinkChoice=0;// reset drinkChoice value break; } } else {// in case that user input incorrectly cout << "Please input a number between 1-7 !\n"; cout << "Select your drink (1-7): "; } }
drinkChoice=0;// reset drinkChoice value, after select correctly system("clear");
// show the summary and ask cutomer if they want to continue or not cout << "##############################\n"; cout << "# Price: " << sum << " $\n"; cout << "##############################\n"; cout << "# 1) to continue shopping #\n"; cout << "# 2) to pay a bill #\n"; cout << "##############################\n"; cout << "Would you like to continue shopping? (1 or 2) : "; // while customer can't decide to continue or stop while(stopChoice==0) { getline(cin, lineCheck);// get input stringstream linestream(lineCheck);// make it carefully stopChoice = atoi(lineCheck.c_str());// convert it into integer (return 0 if it not an ingeter) if(stopChoice!=0) {switch(stopChoice) {case 1 : break; case 2 :flag = false; break; default:// in case that input out of range cout << "Please input a number 1 or 2 !\n"; cout << "Would you like to continue shopping? (1 or 2) : "; stopChoice=0; break; } } else {// in case that user input incorrectly cout << "Please input a number 1 or 2 !\n"; cout << "Would you like to continue shopping? (1 or 2) : "; stopChoice=0; } }
stopChoice=0;// reset stopChoice value, after select to continue system("clear"); }
// shopping finished, show the summary result cout << "##############################\n"; cout << "# Summary Order: \n"; for(int i=0;i<7;i++) { if(coffee[i]!=0) {cout << "# " << menu[i] << " : " << coffee[i] << endl; } } cout << "##############################\n"; cout << "# Summary price: " << sum << " $\n"; cout << "##############################\n"; cout << "# T H A N K Y O U #\n";