Advertisement
YORDAN2347

VendingMachineProgram

Jan 16th, 2021
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. using System;
  2.  
  3. namespace VendingMachine
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             // 1. read input
  10.             string input = Console.ReadLine();
  11.             double balance = 0;
  12.             double[] validCoins = {0.1, 0.2, 0.5, 1, 2};
  13.  
  14.             // 2. while not start, sum coins
  15.             while (input != "Start")
  16.             {
  17.                 double coin = double.Parse(input);
  18.                 bool isValidCoin = false;
  19.  
  20.                 // 2.1 only accept 0.1, 0.2, 0.5, 1, 2
  21.                 for (int i = 0; i < validCoins.Length; i++)
  22.                 {
  23.                     // 3. sum the money
  24.                     if (coin == validCoins[i])
  25.                     {
  26.                         isValidCoin = true;
  27.                         balance += coin;
  28.                     }                      
  29.                 }
  30.  
  31.                 if(!isValidCoin)
  32.                     Console.WriteLine($"Cannot accept {coin}");
  33.  
  34.                 input = Console.ReadLine();
  35.             }
  36.  
  37.             // 4. while not end, buy products
  38.             input = Console.ReadLine();
  39.             double[] productsPrice = {2,    0.7,   1.5,   0.8,  1.0 };
  40.             //Products =            {Nuts, Water, Crisps, Soda, Coke}
  41.             string[] products = { "Nuts", "Water", "Crisps", "Soda", "Coke" };      
  42.  
  43.             while (input != "End")
  44.             {
  45.                 bool isPurchased = false;
  46.                 bool isValidProduct = false;
  47.  
  48.                 for (int i = 0; i < products.Length; i++)
  49.                 {
  50.                     if (input == products[i])
  51.                     {
  52.                         isValidProduct = true;
  53.                         // 4.1 balance -= productPrice
  54.                         if (balance >= productsPrice[i])
  55.                         {
  56.                             balance -= productsPrice[i];
  57.                             Console.WriteLine($"Purchased {products[i]}");
  58.                             isPurchased = true;
  59.                         }                          
  60.                     }                      
  61.                 }
  62.                
  63.                 if(!isValidProduct)
  64.                     Console.WriteLine("Invalid product");                  
  65.  
  66.                 if (!isPurchased && isValidProduct)
  67.                     Console.WriteLine("Sorry, not enough money");
  68.  
  69.                 input = Console.ReadLine();
  70.             }
  71.  
  72.             // 5. Print(Change: {money left})
  73.             Console.WriteLine($"Change: {balance:f2}");
  74.         }
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement