c# realize the basic action function of a vending machine

Posted by nyk on Fri, 14 Jan 2022 06:39:17 +0100

VS2019(c#) vending machine

  1. thinking
    Suppose there is only one product in the vending machine, and the price is 8 $;
    First, the vending opportunity displays the price, balance, total consumption, quantity of food that can be purchased, quantity of food that has been purchased... Etc.
    The second is to input relevant actions to the vending machine, recharge, select the purchase quantity... And so on.
  2. initialization
    Create a new vending machine related variable and declare the assignment.
public int price = 8;//Suppose there is only one kind of goods in the vending machine
public double balance=0.0;
public int canTimes,buyTimes;
  1. Finishing method
    Create a new 1) method for displaying and operating the balance (inShow());
    New 2) display the method times(double balance) of the quantity that can be purchased;
    New 3) method for purchasing quantity (wantbuy);
    New 4) Main integrates the above methods to realize the function of complete vending machine.
  2. inShow() method
    c #, if you want to make keyboard input, you must have convert ToDouble(Console.ReadLine());
    ToDouble can be changed according to the input format, and ToString,ToInt32... Etc.
    The new double variable t is used to replace and add the entered balance;,
    The for loop is to determine the balance and price, type the total balance < price to continue the loop, and break and continue the subsequent exit loop operation;
public double inShow()
        {
            double t = balance;
            Console.WriteLine("your balance is:{0}", balance);
            Console.WriteLine("plesea enter your money:");
            balance = Convert.ToDouble(Console.ReadLine());
            balance = t + balance;
            for (int i =0; ; i++)
            {
                double t1 = balance;
                if (balance < price)
                {
                    Console.Write("your balance was not enough,enter again:");
                    balance = Convert.ToDouble(Console.ReadLine());
                    balance = t1 + balance;
                    if (balance>price)
                    {
                        Console.WriteLine("balance is enough:{0}", balance);
                        break;
                    }
                    continue;                   
                }
                else
                {
                    Console.WriteLine("balance is enough:{0}", balance);
                    break;
                }
            }           
            return balance;
        }
  1. times(double balance) method

This method has one parameter, which is the final value balance returned by the inShow() method,
The parameter of balance is passed into the method, and the value of the quantity of food that can be purchased is calculated,
Because balance is of double type and cannot be calculated with int type, it is cast (int).

public int times(double balance)
        {
            int t = (int)balance;
            canTimes = t / price;
            Console.WriteLine("you can buy this food {0} times", canTimes);
            return canTimes;
        }
  1. wantBuy() method

The for loop is the same as above, and its operation function can be easily known by reading print (my English is not good, so it's probably OK to understand QAQ)

public int wantBuy()
        {            
            for(int i =0; ; i++)
            {
                Console.WriteLine("please enter you want buyTimes:");
                buyTimes = Convert.ToInt32(Console.ReadLine());
                if (buyTimes <= canTimes)
                {
                    Console.WriteLine("you buy this food {0} times", buyTimes);
                    balance = balance - price * buyTimes;
                    Console.WriteLine("now you have {0} doller", balance);break;
                }
                else
                {
                    Console.WriteLine("your money is not enough,please enter following {0} times", canTimes+1);
                    break;
                }                
            }
            return buyTimes;
        }
  1. Main integration
    New object Class1 c1 = new Class1();
    First call the inShow() method, and then write a switch loop to ask whether to recharge again,
    The second is to call the times(double balance) method and the wanBuy() method.
    Complete the basic functions of a vending machine in sequence.
static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.inShow();
            Console.WriteLine("would you want to enter one more time for recharge more money? enter 'yes' continue recharge,enter 'no' out");
            string word = Convert.ToString(Console.ReadLine());
            switch (word)
            {
                case "yes":
                    c1.inShow(); break;
                case "no":
                    break;
                default:
                    Console.WriteLine("enter error!");
                    break;
            }
            double a = c1.balance;
            c1.times(a);
            c1.wantBuy();
            Console.WriteLine("thank you for your patronage!");
            Console.WriteLine("you buy the food for {0}$", c1.price * c1.buyTimes);
            Console.ReadKey();
        }
  1. Complete code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/**zohing Original**/

namespace Project1
{
    public class Class1
    {
        //vending machine
        public int price = 8;//Suppose there is only one kind of goods in the vending machine
        public double balance=0.0;
        public int canTimes,buyTimes;
        public double inShow()
        {
            double t = balance;
            Console.WriteLine("your balance is:{0}", balance);
            Console.WriteLine("plesea enter your money:");
            balance = Convert.ToDouble(Console.ReadLine());
            balance = t + balance;
            for (int i =0; ; i++)
            {
                double t1 = balance;
                if (balance < price)
                {
                    Console.Write("your balance was not enough,enter again:");
                    balance = Convert.ToDouble(Console.ReadLine());
                    balance = t1 + balance;
                    if (balance>price)
                    {
                        Console.WriteLine("balance is enough:{0}", balance);
                        break;
                    }
                    continue;                   
                }
                else
                {
                    Console.WriteLine("balance is enough:{0}", balance);
                    break;
                }
            }           
            return balance;
        }
        public int times(double balance)
        {
            int t = (int)balance;
            canTimes = t / price;
            Console.WriteLine("you can buy this food {0} times", canTimes);
            return canTimes;
        }
        public int wantBuy()
        {            
            for(int i =0; ; i++)
            {
                Console.WriteLine("please enter you want buyTimes:");
                buyTimes = Convert.ToInt32(Console.ReadLine());
                if (buyTimes <= canTimes)
                {
                    Console.WriteLine("you buy this food {0} times", buyTimes);
                    balance = balance - price * buyTimes;
                    Console.WriteLine("now you have {0} doller", balance);break;
                }
                else
                {
                    Console.WriteLine("your money is not enough,please enter following {0} times", canTimes+1);
                    break;
                }                
            }
            return buyTimes;
        }
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.inShow();
            Console.WriteLine("would you want to enter one more time for recharge more money? enter 'yes' continue recharge,enter 'no' out");
            string word = Convert.ToString(Console.ReadLine());
            switch (word)
            {
                case "yes":
                    c1.inShow(); break;
                case "no":
                    break;
                default:
                    Console.WriteLine("enter error!");
                    break;
            }
            double a = c1.balance;
            c1.times(a);
            c1.wantBuy();
            Console.WriteLine("thank you for your patronage!");
            Console.WriteLine("you buy the food for {0}$", c1.price * c1.buyTimes);
            Console.ReadKey();
        }
    }
}
  1. test

  2. Tips
    Read print to know its operation function (my English is not good, I can understand it, QAQ)
    The code can also continue to add functions and optimization. Please correct the bad places,
    Come on, let's rush together!!!

Topics: C# vs2015