Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.85 KB | None | 0 0
  1. Module Module1
  2.     Sub Main()
  3.         'while loop keeps the program running without need to restart for testing purposes
  4.         While true
  5.             'Variable Definitions
  6.  
  7.             'defines the vending machine choices as a 2-dimensional string array
  8.             'defined as a string array to make it easier to print, as the prices can be changed into decimals for calculations using decimal.parse()
  9.             Dim choices = New String()() _
  10.                     {({"KitKat", "1.51"}), ({"Yorkie", "1.00"}), ({"Crunchie", "1.20"}), ({"Coke", "1.30"}),
  11.                      ({"Fanta", "0.90"}),
  12.                      ({"Malteasers", "1.50"}), ({"Granola Bar", "1.51"}), ({"Dr. Pepper", "0.85"}),
  13.                      ({"Double Decker", "1.10"}), ({"Lemonade", "1.29"})}
  14.             Dim money As decimal
  15.             Dim choice As integer
  16.             Dim price as decimal
  17.             Dim counter(7) as Integer
  18.             Dim change As decimal
  19.  
  20.  
  21.             'tells them to enter the value, asks for it as a decimal number as using the pound sign will throw an exception as it is expecting a decimal
  22.             Console.WriteLine("Enter the value you wish to insert, as a decimal number.")
  23.             money = Console.ReadLine()
  24.  
  25.             Console.WriteLine("You have inserted £" & money)
  26.             Console.WriteLine("Please select your choice")
  27.  
  28.  
  29.             'loops through the 2 dimensional array to output a list of all the options in the vending machine
  30.             For index As Integer = 0 To 9
  31.                 Console.write(index + 1 & ". ")
  32.                 Console.Write(choices(index)(0))
  33.                 console.write("----£")
  34.                 console.writeline(choices(index)(1))
  35.  
  36.             Next
  37.  
  38.             'takes the users choice and then subtracts one to deal with 0-base array indexing
  39.             choice = Console.readline()
  40.             choice = choice - 1
  41.             If choice > 9 Or choice < 0 Then
  42.                 console.WriteLine("Invalid Choice")
  43.  
  44.             Else
  45.                 price = decimal.Parse(choices(choice)(1))
  46.                 If price > money Then
  47.                     Console.WriteLine("Not enough money entered")
  48.                 Else
  49.  
  50.                     'calculates the amount of change that needs to be given
  51.                     change = money - price
  52.  
  53.                     'While the amount of change left to give is not 0, it will find the largest coin it can use and subtract that from the amount of change left, making a note of the coin used
  54.                     While change <> 0
  55.                         Select case change
  56.                             Case is >= 2
  57.                                 change = change - 2.00
  58.                                 counter(7) = counter(7) + 1
  59.                             Case 1 To 1.99
  60.                                 change = change - 1.00
  61.                                 counter(6) = counter(6) + 1
  62.                             Case 0.5 to 0.99
  63.                                 'the numbers must be defined as decimal otherwise calculations do not work due to rounding errors
  64.                                 change = change - new Decimal(0.5)
  65.                                 counter(5) = counter(5) + 1
  66.                             Case 0.2 To 0.49
  67.                                 change = change - New Decimal(0.2)
  68.                                 counter(4) = counter(4) + 1
  69.                             Case 0.1 to 0.19
  70.                                 change = change - new Decimal(0.1)
  71.                                 counter(3) = counter(3) + 1
  72.                             case 0.05 To 0.09
  73.                                 change = change - New Decimal(0.05)
  74.                                 counter(2) = counter(2) + 1
  75.                             case 0.02 to 0.04
  76.                                 change = change - New decimal(0.02)
  77.                                 counter(1) = counter(1) + 1
  78.                             case 0.01
  79.                                 change = change - New decimal(0.01)
  80.                                 counter(0) = counter(0) + 1
  81.  
  82.  
  83.                         End Select
  84.                     End While
  85.  
  86.                     'outputs how many of each coin was used for change
  87.                     console.WriteLine("---------Change--------)")
  88.                     Console.WriteLine(counter(7) & "- £2.00")
  89.                     Console.WriteLine(counter(6) & "- £1.00")
  90.                     Console.WriteLine(counter(5) & " - 50p")
  91.                     Console.WriteLine(counter(4) & " - 20p")
  92.                     Console.WriteLine(counter(3) & "- 10p")
  93.                     Console.WriteLine(counter(2) & "- 5p")
  94.                     Console.WriteLine(counter(1) & "- 2p")
  95.                     Console.WriteLine(counter(0) & "- 1p")
  96.  
  97.  
  98.                 End If
  99.  
  100.  
  101.             End If
  102.  
  103.  
  104.             Console.ReadLine()
  105.         end while
  106.     End Sub
  107. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement