In: Computer Science
C++
include a class called VendingMachine.
A user can buy a bottle of water ($1.50), a bottle of coffee ($2.00), a chip ($1.00), and a chocolate bar ($2.50) from the machine. The user can select several item(s) if the items are available from the machine. Furthermore, the user can de-select item(s) that are already selected.
After finishing the selection of item(s), the user can pay for the items with either a debit card (Valid PIN: 7777) or cash. A user should pay the item(s) selected just one trial and if the user fails to pay due to the incorrect PIN or insufficient cash amount, the machine should de-select all sections the user chose.
Note that an administrator of the machine can refill the machine or reset it to the initial status.
At the payment() function, if a user provides incorrect payment information such as an incorrect PIN or insufficient money amount, all selections the user chose should be de-selected.
At the payment() function, if a user didn’t select any items before, the function should return false because there’s nothing to pay.
#include <iostream>
#include<functional>
using namespace std;
int options;
double chips,water,coffee,chocolate bar,milk,cola,quantity_chips,quantity_water,quantity_coffee,quantity_chocolate bar,quantity_milk,quantity_cola,payment,pi,error,tax,final_cost;
void menu()
{
cout<<"welcome .. \n";
do { cout<<"select from menu..\n";
<< "\t1 chips ... $1.00\n"
<< " \t2 water........$1.50\n"
<< "\t3 coffee.....$2.00\n"
<< "\t4 chocolate bar.........$2.50\n"
cout<<"enter your option: ";
cin>>option;
switch(option)
{ case 1: cout<<"quantity:";
cin>>quantity_chips;
chips=(1*quantity_chips); break;
case 2:
cout<<"quantity:";
cin>>quantity_water;
water=(1.50*quantity_water); break;
case 3:
cout<<"quantity";
cin>>quantity_coffee;
coffee=(2*quantity_cofffee); break;
case 4:
cout<<"quantity";
cin>>quantity_chocolate bar;
chocolate bar=(2.5*quantity_chocolate bar); break;
case 5:
void checkout(); break;
default:
cout<<"invalid option:"<<endln;
}
} while(option<5);
}
void checkout() {
if(quantity_chips>0||quantity_water>0||quantity_coffee>0||quantity_chocolate bar>0) {
total=(chips)+(water)+(coffee)+(chocolate bar);
cout<<"select payment option (1:debit card 2:cash):";
cin>>payment;
if(payment=1)
{ void debit card(); }
else if(payment=2) { void cash(); }
else { cout<<"unknown option, please try again later \n";
} } else { cout<<"please select atleast one item \n"; } }
void debitcard()
{
error=1;
cout<<"enter pin:";
cin>>pin;
if(pin=7777) {
void receipt(); } else {
while(error>=1) {
cout<<"invalid pin. tryagain:";
cin>>pin;
error++;
if(pin=7777)
{ void receipt(); }
else{ cout<<"we are sorry.. invalid card details \n";
return; } } } }
void receipt()
{ cout<< "this is your reciept:\n";
if(quantity_chips>0) {
cout<<"chips:1.00 x"<< quantity_chips<<"=$"<<chips<<endl; }
if(quantity_water>0){
cout<<" water : 1.50 x"<<quantity_water<<"=$"<<water<<endl; }
if (quantity_coffee>0){
cout<<"coffee:2.00 x"<<quantity_coffee<<"=$"<<coffee<<endl; }
if(quantity_chocolate bar>0) {
cout<<"chocolate bar:2.50 x"<<quantity_chocolate bar<<"=$"<<chocolate bar<<endl; }
tax=(total* .10);
final_cost=tax+total;
cout<<"tax(12.2%): $"<<tax<<endl;
cout<<"total:$"<<final_cost<<endl;
}
int main()
{
menu();
checkout();
debitcard();
receipt();
return 0;
}