Sunday, April 1, 2018

Automatic vending machine (Id-1143)

Just for Practice 3 Additional [8-Jan-2018 to 31-Mar-2018]

Program ID- 1143

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

Please Comment Working if the code worked to you

If you have other working codes please comment the codes enclosing with <pre> and </pre> 

Example: <pre> Your Code </pre>

Code: C++

#include<iostream>
using namespace std;
class machine
{
    private:
    int code,stock,cost;
    public:
    int get()
    {
        cin>>code>>cost>>stock;
    }
    int display(int x, int y)
    {
        if(code==x)
        {
            if(stock!=0)
            {
                if(y>cost)
                {
                    cout<<code;
                    return 1;
                }
                else
                {
                    cout<<"Insufficient amount";
                    return 1;
                }
            }
            else
            {
                cout<<"Less Stack";
                return 1;
            }
        }
        else
        return 0;
    }
};

int main()
{
    int n,i,j=0,k=0,cost1,code1;
    cin>>n;
    machine s[n];
    for(i=0;i<n;i++)
    {
        s[i].get();
    }
    cin>>code1>>cost1;
    for(i=0;i<n;i++)
    {
        if(j==0)
        {
            j=s[i].display(code1,cost1);
            k++;
        }
    }
    if(k==n)
    cout<<"Wrong item code";
}

NOTE: The Above Codes are for reference only. It doesn't mean everyone to directly copy/paste those codes.

No comments:

Post a Comment