Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Module Module1
- Sub Main()
- 'while loop keeps the program running without need to restart for testing purposes
- While true
- 'Variable Definitions
- 'defines the vending machine choices as a 2-dimensional string array
- 'defined as a string array to make it easier to print, as the prices can be changed into decimals for calculations using decimal.parse()
- Dim choices = New String()() _
- {({"KitKat", "1.51"}), ({"Yorkie", "1.00"}), ({"Crunchie", "1.20"}), ({"Coke", "1.30"}),
- ({"Fanta", "0.90"}),
- ({"Malteasers", "1.50"}), ({"Granola Bar", "1.51"}), ({"Dr. Pepper", "0.85"}),
- ({"Double Decker", "1.10"}), ({"Lemonade", "1.29"})}
- Dim money As decimal
- Dim choice As integer
- Dim price as decimal
- Dim counter(7) as Integer
- Dim change As decimal
- '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
- Console.WriteLine("Enter the value you wish to insert, as a decimal number.")
- money = Console.ReadLine()
- Console.WriteLine("You have inserted £" & money)
- Console.WriteLine("Please select your choice")
- 'loops through the 2 dimensional array to output a list of all the options in the vending machine
- For index As Integer = 0 To 9
- Console.write(index + 1 & ". ")
- Console.Write(choices(index)(0))
- console.write("----£")
- console.writeline(choices(index)(1))
- Next
- 'takes the users choice and then subtracts one to deal with 0-base array indexing
- choice = Console.readline()
- choice = choice - 1
- If choice > 9 Or choice < 0 Then
- console.WriteLine("Invalid Choice")
- Else
- price = decimal.Parse(choices(choice)(1))
- If price > money Then
- Console.WriteLine("Not enough money entered")
- Else
- 'calculates the amount of change that needs to be given
- change = money - price
- '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
- While change <> 0
- Select case change
- Case is >= 2
- change = change - 2.00
- counter(7) = counter(7) + 1
- Case 1 To 1.99
- change = change - 1.00
- counter(6) = counter(6) + 1
- Case 0.5 to 0.99
- 'the numbers must be defined as decimal otherwise calculations do not work due to rounding errors
- change = change - new Decimal(0.5)
- counter(5) = counter(5) + 1
- Case 0.2 To 0.49
- change = change - New Decimal(0.2)
- counter(4) = counter(4) + 1
- Case 0.1 to 0.19
- change = change - new Decimal(0.1)
- counter(3) = counter(3) + 1
- case 0.05 To 0.09
- change = change - New Decimal(0.05)
- counter(2) = counter(2) + 1
- case 0.02 to 0.04
- change = change - New decimal(0.02)
- counter(1) = counter(1) + 1
- case 0.01
- change = change - New decimal(0.01)
- counter(0) = counter(0) + 1
- End Select
- End While
- 'outputs how many of each coin was used for change
- console.WriteLine("---------Change--------)")
- Console.WriteLine(counter(7) & "- £2.00")
- Console.WriteLine(counter(6) & "- £1.00")
- Console.WriteLine(counter(5) & " - 50p")
- Console.WriteLine(counter(4) & " - 20p")
- Console.WriteLine(counter(3) & "- 10p")
- Console.WriteLine(counter(2) & "- 5p")
- Console.WriteLine(counter(1) & "- 2p")
- Console.WriteLine(counter(0) & "- 1p")
- End If
- End If
- Console.ReadLine()
- end while
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement