C# Program for Coin Vending Machine

                                                              C# Program for Coin Vending Machine

Posted by Libin Antony@10-02-2023 IST : 09:12:23

The code uses the C# programming language to simulate a coin vending machine that accepts coins in increments of 0.25, 0.5, 1, and 2. The program calculates the total amount of coins inserted by the user, and compares it to the price of the item being sold by the machine. When the total amount equals or exceeds the item price, the transaction is complete and the item is dispensed.

                                                                Program

using System;


namespace CoinVendingMachine

{

    class Program

    {

        static void Main(string[] args)

        {

            decimal totalAmount = 0;

            decimal itemPrice = 1.5m;


            Console.WriteLine("Welcome to the Coin Vending Machine");

            Console.WriteLine("Please insert coins in increments of 0.25");

            Console.WriteLine("Item price: $" + itemPrice);


            while (totalAmount < itemPrice)

            {

                Console.WriteLine("Current balance: $" + totalAmount);

                Console.WriteLine("Insert coin: ");

                decimal insertedCoin = decimal.Parse(Console.ReadLine());


                if (insertedCoin == 0.25m || insertedCoin == 0.5m || insertedCoin == 1 || insertedCoin == 2)

                {

                    totalAmount += insertedCoin;

                }

                else

                {

                    Console.WriteLine("Invalid coin. Please insert a coin of value 0.25, 0.5, 1, or 2.");

                }

            }


            Console.WriteLine("Transaction complete. Collect your item.");

        }

    }

}

Here's a step-by-step explanation of the code:

1.The program starts with a Main method, which is the entry point for the program.

2.A decimal variable totalAmount is declared and initialized to 0. This variable will keep track of the total amount of coins inserted by the user.

3.Another decimal variable itemPrice is declared and initialized to 1.5. This variable represents the price of the item being sold by the machine.

4.The program displays a welcome message to the user, as well as information about the item price and the denominations of coins that the machine accepts.

5.A while loop is used to repeatedly prompt the user to insert coins until the total amount equals or exceeds the item price.

6.Inside the loop, the program displays the current balance (i.e., the total Amount variable) and asks the user to insert a coin.

7.The inserted coin is read from the user input and stored in a decimal variable inserted Coin.

8.The program then uses an if statement to check if the inserted coin is one of the accepted denominations (0.25, 0.5, 1, or 2). If the coin is valid, the totalAmount is increased by the value of the inserted coin.

9.If the inserted coin is not valid, an error message is displayed to the user, telling them to insert a coin of a valid denomination.

10.The loop continues until the total amount equals or exceeds the item price, at which point the program displays a message indicating that the transaction is complete and the item can be collected.


I hope this explanation helps your readers understand how the C# program for a coin vending machine works. If you have any questions or need additional clarification, feel free to ask!

Comments

Popular posts from this blog

General C# Programs for Beginners

How to make a vertical text box in Microsoft visio?