In: Computer Science
Write a C++ program that allows a user choose item to purchase from a list; ask for the amount to calculate the cost of the purchase; offer to add a tip; add a delivery fee based on the subtotal; then calculate the total amount that the user needs to pay.
----------------------------------
----- GROCERY SHOPPING ITEMS -----
----------------------------------
Other Values to be used in the program.
TIP (Optional) : $5.00
SHIP_RATE1 (for $20 or below) : $0.00
SHIP_RATE2 (for between $20 and $35): 4.35
SHIP_RATE3 (for above #35) : 7.65
Follow the Steps Below
Refer to the below code for the solution:
#include<bits/stdc++.h>
#include <iostream>
using namespace std;
// function declaration
string displayMenu();
float tipCalculator(float cost);
float shippingCalculator(float cost);
//declare global variable
const float m = 5.99;
const float e = 6.99;
const float c = 10.98;
const float p = 2.75;
const float SHIP_RATE1 = 0;
const float SHIP_RATE2 = 4.35;
const float SHIP_RATE3 = 7.65;
//enum and map for items
enum level {milk, egg, cheese, pasta};
map<string, level> items;
void register_items()
{
items["milk"] = milk;
items["egg"] = egg;
items["cheese"] = cheese;
items["pasta"] = pasta;
}
//function to change the case of a string
string change_case(string sl)
{
transform(sl.begin(), sl.end(), sl.begin(), ::tolower);
return sl;
}
int main () {
// local variable declaration:
string order;
string input;
float cost = 0;
int quantity = 0;
register_items();
//fixed point notation for 2 decimal place precision
std::cout.precision(2);
//call function to display Menu
order = displayMenu();
while (!change_case(order).compare("yes"))
{
do{
cout << "Enter the name of the item from the above list - ";
cin >> input;
input = change_case(input);
switch(items[input])
{
case milk:
cost = cost + m;
break;
case egg:
cost = cost + e;
break;
case cheese:
cost = cost + c;
break;
case pasta:
cost = cost + p;
break;
default:
cout<<"Invalid input. Try again";
}
}while ( !(input.compare("milk") || input.compare("egg") ||
input.compare("cheese") || input.compare("pasta")));
cout<<"Enter the quantity - ";
cin>>quantity;
cost = quantity * cost;
order = displayMenu();
}
// calling a function to get cost
if(cost != 0){
cost = tipCalculator(cost);
cost = shippingCalculator(cost);
std::cout << "Total cost = " << std::fixed;
std::cout <<cost;
}
return 0;
}
// function displaying menu
string displayMenu() {
// local variable declaration
string order;
cout<<"----------------------------------\n";
cout<<"----- GROCERY SHOPPING ITEMS -----\n";
cout<<"Milk - $5.99 / gallon\n";
cout<<"Egg - $6.99 / dozen\n";
cout<<"Cheese – $10.98 / 8oz\n";
cout<<"Pasta – $2.75 / packet\n";
cout<<"----------------------------------\n\n";
cout<<"Do you want to give a order? ";
cin >> order;
return order;
}
// function adding tip
float tipCalculator(float cost){
string tip;
cout<<"Do you want to give a tip? ";
cin>>tip;
if(!change_case(tip).compare("yes"))
{
cost = cost + 5;
}
return cost;
}
//function to calculate shipping
float shippingCalculator(float cost){
if(cost<=20){
cost = cost + SHIP_RATE1;
}
else if(cost>20 && cost<=35){
cost = cost + SHIP_RATE2 ;
}
else if(cost>35){
cost = cost + SHIP_RATE3 ;
}
return cost;
}
Snippet of running code:
OUTPUT 1:
OUTPUT 2:
OUTPUT 3: