CSE1002-Automatic vending machine
An automatic vending machine has many snack items arranged in its shelves. Each item has an item code and a cost. A user can enter the amount and key-in the item code. If the itemcode matches an entry in the item list and amount entered by user is less than cost of the item, then the item will be dispensed. Develop an algorithm and write a C++ code to print name of the item if the amount entered is greater than the cost of the item and item code is valid. If the amount is less than the cost of the item then throw a double exception, if the item code entered is wrong then throw a integer exception and when the item entered is not in stock throw a string exception. Print appropriate messages such as “Insufficient amount”, “Wrong item code” or “Less stock”.
Input Format:
Enter the number of items in the vending machine
Itemcode-1
Cost of item1
stock in hand item1
Itemcode-2
Cost of item2
stock in hand item2
…
Itemcode-n
Cost of item1
stock in hand item3
Item code entered by user
Cost entered by user
Output Format:
Print either item name or “Insufficient amount” or “Wrong item code” or “Less Stock”
Boundary Conditions:
Number of Item >1
for(i=0;i < num;i++)
{ if(stack[i].code==cd) {
flag=1;
if(co > stack[i].cost) {
if(stack[i].stock>0) cout<<stack[i].code;
else cout<<"Less stock"; }
else cout<<"Insufficient amount"; } }
if(flag==0)
cout<<"Wrong item code";
2)Read num and stack
3)For i in the range of num
4) If stack[i].code is equals to cd
5) Then assume flag is equals to one i
6)If co is greater than stack[i].cost)
7)If stack[i].stock is greater than zero
8)Then cout is lessthan stack[i].code
9)Else cout is lesss than"Less stock"
10)Else cout is less than "Insufficient amount"
11) If flag is equals to zero
12)Then cout is less than"Wrong item code"
13)END
An automatic vending machine has many snack items arranged in its shelves. Each item has an item code and a cost. A user can enter the amount and key-in the item code. If the itemcode matches an entry in the item list and amount entered by user is less than cost of the item, then the item will be dispensed. Develop an algorithm and write a C++ code to print name of the item if the amount entered is greater than the cost of the item and item code is valid. If the amount is less than the cost of the item then throw a double exception, if the item code entered is wrong then throw a integer exception and when the item entered is not in stock throw a string exception. Print appropriate messages such as “Insufficient amount”, “Wrong item code” or “Less stock”.
Input Format:
Enter the number of items in the vending machine
Itemcode-1
Cost of item1
stock in hand item1
Itemcode-2
Cost of item2
stock in hand item2
…
Itemcode-n
Cost of item1
stock in hand item3
Item code entered by user
Cost entered by user
Output Format:
Print either item name or “Insufficient amount” or “Wrong item code” or “Less Stock”
Boundary Conditions:
Number of Item >1
C++ CODE: #include<iostream>
using namespace std;
int main()
{
struct item
{
int code,cost,stock;
}stack[20];
int num,i,cd,co,flag=0;
cin>>num;
for(i=0;i < num;i++) cin>>stack[i].code>>stack[i].cost>>stack[i].stock;
cin>>cd>>co;
for(i=0;i < num;i++) { if(stack[i].code==cd) { flag=1; if(co > stack[i].cost)
{
if(stack[i].stock>0)
cout<<stack[i].code;
else
cout<<"Less stock";
}
else
cout<<"Insufficient amount";
}
}
if(flag==0)
cout<<"Wrong item code";
return(0);
}INPUT :num - number of items
item - values of each itemPROCESSING:for(i=0;i < num;i++)
{ if(stack[i].code==cd) {
flag=1;
if(co > stack[i].cost) {
if(stack[i].stock>0) cout<<stack[i].code;
else cout<<"Less stock"; }
else cout<<"Insufficient amount"; } }
if(flag==0)
cout<<"Wrong item code";
OUTPUT:
if(stack[i].stock > 0) cout<<stack[i].code; else if cout<<"Insufficient amount"; else if cout<<"Less stock"; else cout<<"Wrong item code";
PSUEDO CODE:
1)START2)Read num and stack
3)For i in the range of num
4) If stack[i].code is equals to cd
5) Then assume flag is equals to one i
6)If co is greater than stack[i].cost)
7)If stack[i].stock is greater than zero
8)Then cout is lessthan stack[i].code
9)Else cout is lesss than"Less stock"
10)Else cout is less than "Insufficient amount"
11) If flag is equals to zero
12)Then cout is less than"Wrong item code"
13)END
0 Comments