PROGRAMMING PROJECT #1: BUY ITEMS Objectives: Use basic C++ constructs: if, swit
ID: 3724581 • Letter: P
Question
PROGRAMMING PROJECT #1: BUY ITEMS
Objectives:
Use basic C++ constructs: if, switch, and repetition (looping)
Perform simple data type manipulations
Solve problem, design solution and implement using C++
Description:
Write a menu-driven program named “BuyItems.cpp” that provides the following options:
Buy items by asking for the item’s description, price per item and the quantity
Show receipts by listing all the purchases
Show summary by listing only the number of purchases and the total purchase cost
Requirements:
The program must produce the provided expected sample output.
The user must enter the correct “access code” which is one of the followings (12345 or 54321) in order to start the program.
There is no array, just simply use string and string concatenation to keep track of the purchase history.
The program should NOT have any global variables
The program should have at least 3 functions and some documentation including your name and program’s description
Required error handling:
The program MUST perform the following checks:
Case-insensitive when to ask the user to confirm exiting the program (e.g. accepting both ‘Y’ and ‘y’)
Check for bad access code and allow up to 3 trials.
Check for negative or invalid price and/or quantity
Check for invalid menu answer
Sample program run 1
D:>BuyItems
Welcome to my shopping program.
Please enter the access code: 12345
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: -1
Invalid selection. Please try it again.
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 5
Invalid selection. Please try it again.
Shopping Menu:
1. buy
2. show receipt
3. show summary
4. exit the program
Enter your option: Hello
Invalid selection. Please try it again.
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 1
Please enter the description: Book
Please enter the quantity: 10
Please enter the price for each item: 5
ITEM(Book) QUANTITY(10) PRICE($5.00) COST($50.00)
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 1
Please enter the description: CD
Please enter the quantity: 2
Please enter the price for each item: 20
ITEM(CD) QUANTITY(2) PRICE($20.00) COST($40.00)
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 1
Please enter the description: Movie
Please enter the quantity: 4
Please enter the price for each item: 8
ITEM(Movie) QUANTITY(4) PRICE($8.00) COST($32.00)
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 2
List of all purchases:
ITEM(Book) QUANTITY(10) PRICE($5.00) COST($50.00)
ITEM(CD) QUANTITY(2) PRICE($20.00) COST($40.00)
ITEM(Movie) QUANTITY(4) PRICE($8.00) COST($32.00)
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 3
PURCHASES(3) TOTAL_COST($122.00)
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 4
Are you sure that you want to exit the program (y/n)? y
Thank you for using our program.
Thank you for using my program.
D:>
Sample program run 2 – invalid price and quantity input
D:>BuyItems
Welcome to my shopping program.
Please enter the access code: 54321
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 1
Please enter the description: Book
Please enter the quantity: -1
Invalid quantity. Quantity cannot be negative or string. Please try it again.
Please enter the quantity: -10
Invalid quantity. Quantity cannot be negative or string. Please try it again.
Please enter the quantity: Hello
Invalid quantity. Quantity cannot be negative or string. Please try it again.
Please enter the quantity: 10
Please enter the price for each item: -1
Invalid price. Price cannot be negative or string. Please try it again.
Please enter the price for each item: -10
Invalid price. Price cannot be negative or string. Please try it again.
Please enter the price for each item: Hello
Invalid price. Price cannot be negative or string. Please try it again.
Please enter the price for each item: 5
ITEM(Book) QUANTITY(10) PRICE($5.00) COST($50.00)
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 2
List of all purchases:
ITEM(Book) QUANTITY(10) PRICE($5.00) COST($50.00)
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 3
PURCHASES(1) TOTAL_COST($50.00)
Shopping Menu:
1. buy
2. show receipts
3. show summary
4. exit the program
Enter your option: 4
Are you sure that you want to exit the program (y/n)? y
Thank you for using our program.
Thank you for using my program.
D:>
Sample program run 3 – invalid access code
D:>BuyItems
Welcome to my shopping program.
Please enter the access code: 123
Invalid access code. Please try it again.
Please enter the access code: hello
Invalid access code. Please try it again.
Please enter the access code: 12345
Shopping Menu:
1. buy
2. show receipt
3. show summary
4. exit the program
Enter your option: 2
There is no purchase available.
Shopping Menu:
1. buy
2. show receipt
3. show summary
4. exit the program
Enter your option: 3
PURCHASES( 0 ) TOTAL_COST($0)
Shopping Menu:
1. buy
2. show receipt
3. show summary
4. exit the program
Enter your option: 4
Are you sure that you want to exit the program (y/n)? n
Shopping Menu:
1. buy
2. show receipt
3. show summary
4. exit the program
Enter your option: 4
Are you sure that you want to exit the program (y/n)? y
Thank you for using our program.
Thank you for using my program.
D:>
Explanation / Answer
// NOTE:compile your code by:
// g++ -std=c++11 filename.cpp
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class item
{
public:
string item_name[10]; // here '10' is max item can store (request you to change as per your requirement)
int quality[10];
int price_of_each[10];
int cost[10];
int item_count; // to count total count input by user
int total_cost;
int user_choice;
int access_code;
item()
{
item_count = 0;
access_code = 12345; // NOTE: default acess code is 12345
total_cost=0;
user_choice = -1; //invalid
}
void verify_accessCode();
void buy_item();
void show_summery();
void show_receipt();
void show_menu();
void execute_menu();
};
// function to verify access code
void item::verify_accessCode()
{
int uin_access_code;
while(1)
{
cout<<" Please enter the access code: ";
cin>>uin_access_code;
if(uin_access_code !=access_code )
{
cout<<" Invalid access code. Please try it again. ";
continue;
}
break;
}
}
// function to add new item ,quantity and price (scanning each thing and validating input)
void item:: buy_item()
{
string str;
cout<<" Please enter the description:";
cin>>item_name[item_count];
while(1)
{
cout<<" Please enter the Quantity: ";
cin >>str;
if(!(str[0] >= '0' && str[0] <= '9'))
{
cout<<"Invalid Price. Price cannot be negative or string. Please try it again. ";
continue;
}
quality[item_count] = 0;
quality[item_count] = stoi(str);
if(quality[item_count] > 0)
break;
else
cout<<"Invalid quantity. Quantity cannot be negative or string. Please try it again. ";
}
// scanning item from user and validating it
do
{
cout<<" Please enter the price for each item: ";
cin >>str;
if(!(str[0] >= '0' && str[0] <= '9'))
{
cout<<"Invalid Price. Price cannot be negative or string. Please try it again. ";
continue;
}
price_of_each[item_count] = 0;
price_of_each[item_count] = stoi(str);
if(price_of_each[item_count] > 0)
break;
else
cout<<"Invalid Price. Price cannot be negative or string. Please try it again. ";
}while(1);
cost[item_count] = price_of_each[item_count] * quality[item_count];
item_count++;
}
// function printing summary
void item:: show_summery()
{
int i;
if(item_count == 0)
{
cout<<"There is no purchase available. ";
return;
}
for(i=0;i<item_count;i++)
{
cout <<"ITEM("<<item_name[i]<<") QUANTITY("<<quality[i]<<") PRICE($"<<price_of_each[i]<<") COST($"<<cost[i]<<")"<<endl;
}
}
// function to generate recceipt
void item:: show_receipt()
{
int i;
total_cost = 0;
if(item_count == 0)
{
cout<<" PURCHASES(0) "<<"total_cost(0) ";
return;
}
for(i=0;i<item_count;i++)
{
total_cost+=cost[i];
}
cout << "PURCHASES("<<(item_count)<<") TOTAL_COST($"<<total_cost<<")"<<endl;
}
// function to print menu
void item:: show_menu()
{
cout<<" Shopping Menu: 1. buy 2. show receipt 3. show summary 4. exit the program"<<endl;
}
// function to take user choice and execute it
void item:: execute_menu()
{
int input;
char c;
while(1)
{
show_menu();
cout<<"Enter your option: ";
cin>>input;
switch(input)
{
case 1:
buy_item();
break;
case 2:
show_receipt();
break;
case 3:
show_summery();
break;
case 4:
{
cout<<"Are you sure that you want to exit the program (y/n)? :";
cin>>c;
if(c == 'y' || c == 'Y')
{
cout<<"Thank you for using my program ";
return;
}
else if (c == 'N' || c == 'n')
{
continue;
}
else
{
cout<<"Ufff... Invalid ";
}
break;
}
default:
cout<<"Invalid Choice";
}
}
}
int main()
{
// creating object of class
item i;
// validating access code
i.verify_accessCode();
// contineue asking user choice and run untill exit
i.execute_menu();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.