Saturday, 12 November 2016

Q)A common place to buy candy is from a machine. A new candy machine has been purchased for the gym, but it is not working properly. The machine sells candies, chips, gum, and cookies. You have been asked to write a program for this candy machine so that it can be put into operation.
 The program should do the following:  Show the customer the different products sold by the candy machine.  Let the customer make the selection.  Show the customer the cost of the item selected.  Accept money from the customer.  Release the item.

#include<iostream>
using namespace std;

class candy_M {
      string items[4];//intialized by constructor the name of the items
      double price[4]; //intilized by constructor the price of items
      int choice; //stores the choice of item to be selected
      double E_amount; //stores the Amount entered by the user
      double r_amount; //stores the reaming amount if the entered amount is greater than the item price
public:
      candy_M(); //constructor
      void menu(); //declaration of a function  display the menu of items
      void set_Choice(int);  //declaration of a function set the choice entered ny the user
      void set_amount(double); //declaration of a function  set the Amount entered ny the user
      void check_Amount()const; //declaration of a function  check wheather the amont entered is ==  or > or < then the items price
};
int main()
{
      int ch;
      double am;
      candy_M M1; //creating object
      M1.menu(); //display menu
      while (!(cin >> ch) || (ch>4 || ch <= 0)) //takes the choice and checking it if it is valid or not
      {
            cin.clear();
            cin.ignore(900, '\n');
            cout << "  Invalid choice!\nAgain Enter the Choice:";
      }
      M1.set_Choice(ch);
      cout << "Enter the Amount :";
      while (!(cin >> am) || (am <= 0)) //takes the Amount and checking it if it is valid or not
      {
            cin.clear();
            cin.ignore(900, '\n');
            cout << "  Invalid Amount!\nAgain Enter the Amount:";
      }
      M1.set_amount(am);
      M1.check_Amount();

      return 0;

}
void candy_M::menu()//function  display the menu of items
{
      cout << "**************CANDY MACHINE*****************\n";
      cout << "\t 1 for Chips          RS 50.\n";
      cout << "\t 2 for Candies        RS 20.\n";
      cout << "\t 3 for Gum            RS 10.\n";
      cout << "\t 4 for Cookies        RS 40.\n";
      cout << "Enter your Choice :";
}
void candy_M::set_Choice(int ch) //function set the choice entered ny the user
{
      choice = ch - 1;
}
void candy_M::set_amount(double p) //function set the Amount entered ny the user
{
      E_amount = p;
}
void candy_M::check_Amount()const //function  check wheather the amont entered is ==  or > or < then the items price
{
      double am = E_amount - price[choice];
      if (am > 0)
      {
            cout << "Take the Remaining Money\n";
            cout << "You the purchased the item\nTake the item\n";
      }
      else if (am == 0)
            cout << "You the purchased the item\nTake the item";
      else
            cout << "\nYou have entered the Amount less than the price of an item\n";
}
candy_M::candy_M() //Constructor
{
      items[0] = "Chips";
      items[1] = "Candies";
      items[2] = "Gum";
      items[3] = "Cookies";
      price[0] = 50;
      price[1] = 20;
      price[2] = 10;
      price[3] = 40;
}

No comments:

Post a Comment