namespace SimpleVendingMachine
static Product[] machine = new Product[10];
public static void Main(string[] args)
machine[0] = new Product { Name = "Twix", Price = 1.50, Stock = 5 };
machine[1] = new Product { Name = "Crisps", Price = 0.80, Stock = 5 };
machine[2] = new Product { Name = "Soda", Price = 1.20, Stock = 5 };
machine[3] = new Product { Name = "Chocolate Bar", Price = 1.00, Stock = 5 };
machine[4] = new Product { Name = "Gum", Price = 0.50, Stock = 5 };
machine[5] = new Product { Name = "Juice", Price = 1.50, Stock = 5 };
machine[6] = new Product { Name = "Candy", Price = 0.60, Stock = 5 };
machine[7] = new Product { Name = "Water", Price = 0.90, Stock = 5 };
machine[8] = new Product { Name = "Granola Bar", Price = 1.10, Stock = 5 };
machine[9] = new Product { Name = "Chips", Price = 1.30, Stock = 5 };
static void RunVendingMachine()
Console.WriteLine("1) Insert Coins \n2) Select Product \n3) Exit");
string choice = Console.ReadLine();
totalMoney += InsertCoins();
totalMoney = SelectProduct(totalMoney);
Console.WriteLine("Thank you for using the vending machine!");
Console.WriteLine("Invalid selection. Try again.");
static void Displayproducts()
Console.WriteLine("Available Products:");
for (int i = 0; i < machine.Length; i++)
if (machine[i].Stock > 0)
Console.WriteLine(i + ") " + machine[i].Name + " - £" + machine[i].Price + " (Stock: " + machine[i].Stock + ")");
Console.WriteLine(i + ") " + machine[i].Name + " - Out of stock");
static double InsertCoins()
double totalInserted = 0;
Console.WriteLine("Insert a coin: \n1) 5p \n2) 10p \n3) 20p \n4) 50p \n5) £1 \n6) £2 \nX) Quit");
string input = Console.ReadLine();
if (input.ToUpper() == "X") break;
if (input == "1") coinValue = 0.05;
else if (input == "2") coinValue = 0.10;
else if (input == "3") coinValue = 0.20;
else if (input == "4") coinValue = 0.50;
else if (input == "5") coinValue = 1.00;
else if (input == "6") coinValue = 2.00;
Console.WriteLine("Invalid coin. Please try again.");
totalInserted += coinValue;
Console.WriteLine("Total Money Inserted: £" + totalInserted.ToString("F2"));
static double SelectProduct(double totalmoney)
Console.WriteLine("Enter product code:");
if (int.TryParse(Console.ReadLine(), out productCode) && productCode >= 0 && productCode < machine.Length)
if (machine[productCode].Stock > 0)
if (totalmoney >= machine[productCode].Price)
totalmoney -= machine[productCode].Price;
machine[productCode].Stock--;
Console.WriteLine("Dispensed " + machine[productCode].Name + ". Remaining balance: £" + totalmoney.ToString("F2"));
Console.WriteLine("Insufficient funds. Please insert more coins.");
Console.WriteLine("Sorry, this product is out of stock.");
Console.WriteLine("Invalid product code. Please try again.");