Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.             'All non-integer values are defined as decimals rather than floats otherwise calculations don't work properly.
  15.             Dim money As decimal
  16.             Dim choice As integer
  17.             Dim price as decimal
  18.             'this array is to store the number of each coin used, with 0 being £2 and 7 being 1p
  19.            Dim counter(7) as Integer
  20.             Dim change As decimal
  21.  
  22.  
  23.             '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
  24.            Console.WriteLine("Enter the value you wish to insert, as a decimal number.")
  25.             money = Console.ReadLine()
  26.  
  27.             Console.WriteLine("You have inserted £" & money)
  28.             Console.WriteLine("Please select your choice")
  29.  
  30.  
  31.             'loops through the 2 dimensional array to output a list of all the options in the vending machine
  32.            For index As Integer = 0 To 9
  33.                 Console.write(index + 1 & ". ")
  34.                 Console.Write(choices(index)(0))
  35.                 console.write("----£")
  36.                 console.writeline(choices(index)(1))
  37.  
  38.             Next
  39.  
  40.             'takes the users choice and then subtracts one to deal with 0-base array indexing
  41.            choice = Console.readline()
  42.             choice = choice - 1
  43.             'checks if the choice is within the allowed range
  44.            If choice > 9 Or choice < 0 Then
  45.                 console.WriteLine("Invalid Choice")
  46.  
  47.             Else
  48.                 'checks to see if they have entered enough money for their choice
  49.                price = decimal.Parse(choices(choice)(1))
  50.                 If price > money Then
  51.                     Console.WriteLine("Not enough money entered")
  52.                 Else
  53.  
  54.                     'calculates the amount of change that needs to be given
  55.                    change = money - price
  56.  
  57.                     '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
  58.                    While change <> 0
  59.                         Select case change
  60.                             Case is >= 2
  61.                                 change = change - 2.00
  62.                                 counter(7) = counter(7) + 1
  63.                             Case 1 To 1.99
  64.                                 change = change - 1.00
  65.                                 counter(6) = counter(6) + 1
  66.                             Case 0.5 to 0.99
  67.                                 'the numbers must be defined as decimal otherwise calculations do not work due to rounding errors
  68.                                change = change - new Decimal(0.5)
  69.                                 counter(5) = counter(5) + 1
  70.                             Case 0.2 To 0.49
  71.                                 change = change - New Decimal(0.2)
  72.                                 counter(4) = counter(4) + 1
  73.                             Case 0.1 to 0.19
  74.                                 change = change - new Decimal(0.1)
  75.                                 counter(3) = counter(3) + 1
  76.                             case 0.05 To 0.09
  77.                                 change = change - New Decimal(0.05)
  78.                                 counter(2) = counter(2) + 1
  79.                             case 0.02 to 0.04
  80.                                 change = change - New decimal(0.02)
  81.                                 counter(1) = counter(1) + 1
  82.                             case 0.01
  83.                                 change = change - New decimal(0.01)
  84.                                 counter(0) = counter(0) + 1
  85.  
  86.  
  87.                         End Select
  88.                     End While
  89.  
  90.                     'outputs how many of each coin was used for change
  91.                    console.WriteLine("---------Change--------)")
  92.                     Console.WriteLine(counter(7) & "- £2.00")
  93.                     Console.WriteLine(counter(6) & "- £1.00")
  94.                     Console.WriteLine(counter(5) & " - 50p")
  95.                     Console.WriteLine(counter(4) & " - 20p")
  96.                     Console.WriteLine(counter(3) & "- 10p")
  97.                     Console.WriteLine(counter(2) & "- 5p")
  98.                     Console.WriteLine(counter(1) & "- 2p")
  99.                     Console.WriteLine(counter(0) & "- 1p")
  100.  
  101.  
  102.                 End If
  103.  
  104.  
  105.             End If
  106.  
  107.  
  108.             Console.ReadLine()
  109.         end while
  110.     End Sub
  111. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement