Solution
/Programmer: Eric Barnes date 02/25/13
//File shopping_menu.cpp class: CS53
//Purpose: this file contains the main function for an online shopping terminal
//which allows the user to select the type and quantity of items desired
//without exceeding their available funds
#include <iostream>
using namespace std;
int main ()
{
//Variable declaration
const string USER = "Marge";
const string OPTION_1 = "Beer";
const string OPTION_2 = "Spam";
const string OPTION_3 = "DOHnuts"; // Amazing name
const string OPTION_4 = "Mustard Packets"; // Mmmmmmmm, mustard packets....
const string OPTION_5 = "Quit";
const float PRICE_1 = 4.88;
const float PRICE_2 = 1.77;
const float PRICE_3 = 3.29;
const float PRICE_4 = 2.50;
//total quantity of a particular item bought so far
int quant_1, quant_2, quant_3, quant_4;
//amount of any item to be purchaced this iteration
//will change in order to not exceed available money
int buytemp;
//total amount of money initially available
float total;
int choice;
//amount of money spent on this shopping excursion
float spent;
char cAgain;
//set output to 2 decimal places
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//initial loop to repeat entire program on request
do
{
//reset quantities used by menu
quant_1 = 0;
quant_2 = 0;
quant_3 = 0;
quant_4 = 0;
total = 0;
spent = 0;
cout << "Welcome to the store: " << USER <<endl;
//data cleansing for initial amount of money
do
{
cout << "Please enter your starting funds: ";
cin >> total;
if(total < 0)
cout << "ERROR: NEGATIVE AMOUNT" << endl;
} while(total < 0);
cout << "__________________MENU:_____________________"<<endl;
cout << "1: " << OPTION_1 << ": $" << PRICE_1 << endl;
cout << "2: " << OPTION_2 << ": $" << PRICE_2 << endl;
cout << "3: " << OPTION_3 << ": $" << PRICE_3 << endl;
cout << "4: " << OPTION_4 << ": $"<< PRICE_4 << endl;
cout << "5: " << OPTION_5 << endl;
//loop to repeatedly ask user to input a choice from the menu
do
{
cout << "you currently have : $" << total - spent << " remaining"<< endl;
cout << "Please choose an item from the menu: ";
cin >> choice;
//switch on users choice and prompts for amount of item to add
//exits loop when 5 is selected
switch (choice)
{
case 1:
//data cleansing to ensure non-negative amount is purchased
do
{
cout <<"enter the amount of " << OPTION_1 <<" you would like ";
cin >> buytemp;
if (buytemp < 0)
cout << "please enter a positive value" << endl;
} while (buytemp < 0);
spent += (buytemp * PRICE_1);
//take items out of the shopping cart, one by one, until the
//purchase is affordable
while(total < spent)
{
buytemp --;
spent -= PRICE_1;
}
cout << "You successfully added "<< buytemp <<" "<< OPTION_1
<< " to your shopping cart" << endl;
quant_1 += buytemp;
break;
case 2:
do
{
cout <<"enter the amount of " << OPTION_2 <<" you would like: ";
cin >> buytemp;
if (buytemp < 0)
cout << "please enter a positive value" << endl;
} while (buytemp < 0);
spent += (buytemp * PRICE_2);
while(total < spent)
{
buytemp --;
spent -= PRICE_2;
}
cout << "You successfully added "<< buytemp <<" " << OPTION_2
<< " to your shopping cart" << endl;
quant_2 += buytemp;
break;
case 3:
do
{
cout <<"enter the amount of " << OPTION_3 <<" you would like: ";
cin >> buytemp;
if (buytemp < 0)
cout << "please enter a positive value" << endl;
} while (buytemp < 0);
spent += (buytemp * PRICE_3);
while(total < spent)
{
buytemp --;
spent -= PRICE_3;
}
cout << "You successfully added "<< buytemp <<" "<< OPTION_3
<< " to your shopping cart" << endl;
quant_3 += buytemp;
break;
case 4:
do
{
cout <<"enter the amount of " << OPTION_4 <<" you would like: ";
cin >> buytemp;
if (buytemp < 0)
cout << "please enter a positive value" << endl;
} while (buytemp < 0);
spent += (buytemp * PRICE_4);
while(total < spent)
{
buytemp --;
spent -= PRICE_4;
}
cout << "You successfully added "<< buytemp <<" " << OPTION_4
<<" to your shopping cart" << endl;
quant_4 += buytemp;
break;
//output result of shopping spree to user
case 5:
cout << "You purchased: "<<endl;
cout << "\t\t " << quant_1 << " " << OPTION_1 << endl;
cout << "\t\t " << quant_2 << " " << OPTION_2 << endl;
cout << "\t\t " << quant_3 << " " << OPTION_3 << endl;
cout << "\t\t " << quant_4 << " " << OPTION_4 << endl;
cout << "at a cost of $" << spent << endl;
cout << "and have $" << total - spent << "remaining"<<endl;
break;
default:
cout << "Please enter a valid option from the menu" << endl;
}
} while (choice != 5);
cout << "would you like to place another order (y/n)?";
cin >> cAgain;
} while(cAgain == 'y' || cAgain == 'Y');
cout << "Thank you for using our online store"
<< USER<< ", Goodbye!" << endl;
return 0;
}