Advertisement
Exilepilot

Vending machine (Console)

Sep 4th, 2014
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit Off
  2. Module Module1
  3.  
  4.     '' DEBUG-MODE 1 (True), 0 (False)
  5. #Const DEBUG = 1
  6.  
  7.     Dim credit As Integer = -1 '' Credit with dummy value
  8.    Dim S_product As String() = {"Cheese 'n onion walkers", "Rockstar Energy drink", "Twix", "Snickers", "Ready salted"}
  9.     Dim N_product As Integer() = {50, 99, 69, 67, 50}
  10.     Dim list(5) As String   '' List of products (5 values maximum)
  11.    Dim itemsBought As Integer = 0
  12.  
  13.     '' Adds a product to the list of bought items.
  14.    '' n (int) - The position of the list
  15.    '' name (string) - Product name
  16.    Sub addProduct(n As Integer, name As String)
  17.         list(n) = name
  18.         Debug.Print("Added product: " & name & " to list at position {0}", n)
  19.     End Sub
  20.  
  21.     '' Checks if you have enough to buy an item by comparing
  22.    '' an integer with the amount of credit the user has.
  23.    ''Params: n (int) the integer to compare with credit
  24.    Function isEnough(ByVal n As Integer) As Boolean
  25.         If credit >= n Then
  26.             Return True
  27.         End If
  28.  
  29. #If Debug = 1 Then
  30.         Debug.Print("Credit is {0} however not enough.", credit)
  31. #End If
  32.  
  33.         Return False
  34.     End Function
  35.  
  36.     '' Buy product with the given product position.
  37.    '' Returns True if product is bought without problem otherwise
  38.    '' false which could be due to having not enough credit or
  39.    '' an incorrect given position.
  40.  
  41.     Function buyProduct(ByVal productPosition As Integer) As Boolean
  42.         If productPosition >= 1 And productPosition <= 5 Then
  43.             '' Check if user has enough money
  44.            Dim productPrice As Integer = N_product(productPosition - 1)
  45.             Debug.Assert(IsNumeric(productPrice))   '' is the position correct and will it return a numeric value?
  46.  
  47.             If isEnough(productPrice) Then
  48.                 credit = credit - productPrice
  49.  
  50.                 Dim prodName As String = S_product(productPosition - 1)
  51.                 Console.WriteLine("Bought item: {0} current credit: {1}p", prodName, credit)
  52.  
  53. #If Debug = 1 Then
  54.                 Debug.Print("Bought item: {0}", prodName)
  55.                 Debug.Print("Credit reduced to {0}.", credit)
  56. #End If
  57.  
  58.  
  59.                 '' Increment counter
  60.                itemsBought = itemsBought + 1
  61.                 '' Same case with list array.
  62.                list(itemsBought - 1) = prodName
  63.                 Return True
  64.             Else
  65.                 Console.WriteLine("Not enough credit")
  66.                 Return False
  67.             End If
  68.         Else
  69.  
  70. #If Debug = 1 Then
  71.             Debug.Print("Incorrect product position")   '' Kill the user.
  72. #End If
  73.             Return False
  74.         End If
  75.     End Function
  76.  
  77.     '' Loop until a valid amount of credit is input.
  78.    Sub getCoins()
  79.         Dim counter = 0
  80.         If itemsBought >= 1 Then
  81.             Console.WriteLine("Type 'exit' otherwise continue!")
  82.             Dim tmp As String = Console.ReadLine()
  83.             If tmp = "exit" Then
  84.                 getItemsBought()
  85.                 Stop
  86.             End If
  87.         End If
  88.  
  89.         Do
  90.             Console.WriteLine("Please enter the amount of credit you wish to input in pennies (£1 (100p), £2 (200p), 50p): ")
  91.             Dim tmp As String
  92.             tmp = Console.ReadLine()
  93.             If IsNumeric(tmp) Then
  94.                 credit = tmp
  95.             Else
  96.                 counter = counter + 1
  97.             End If
  98.  
  99.             '' Exit loop....
  100.            If counter = 5 Then
  101.                 Stop
  102.             End If
  103.         Loop Until (credit > -1)
  104.  
  105.     End Sub
  106.  
  107.     Sub getItemsBought()
  108.         For i = 0 To list.Length - 1
  109.             If Not IsNothing(list(i)) Then
  110.                 Console.WriteLine("#{0} Item bought: {1}", i + 1, list(i))
  111.             End If
  112.         Next
  113.     End Sub
  114.  
  115.     Sub getItemsForSale()
  116.         For i = 0 To S_product.Length - 1
  117.             Console.WriteLine("Number: {0} || Product name: {1}", i + 1, S_product(i))
  118.         Next
  119.     End Sub
  120.  
  121.     '' Use simple recursion to get the input of user.
  122.    Function handleItemList(Optional ByVal n As Integer = 0) As Integer
  123.         Dim counter = n
  124.         If counter >= 5 Then
  125.             Return -1
  126.         End If
  127.  
  128.         getItemsForSale()
  129.         Console.WriteLine("Type the number of the product you would like to buy otherwise type 'exit': ")
  130.         Dim S_tmp As String = Console.ReadLine()
  131.         Dim N_tmp As Integer = -1
  132.         If IsNumeric(S_tmp) Then
  133.             N_tmp = S_tmp
  134.  
  135.             If N_tmp >= 1 Or N_tmp <= 5 Then
  136.                 Return N_tmp
  137.             Else
  138.                 counter = counter + 1
  139.                 Return handleItemList(counter)
  140.             End If
  141.  
  142.  
  143.         ElseIf S_tmp = "exit" Then
  144.             Return -1
  145.         Else
  146.             Return handleItemList()
  147.         End If
  148.  
  149.     End Function
  150.  
  151.     Sub Main()
  152.         Dim stopProgram As Boolean = False
  153.         While stopProgram <> True
  154.             '' Ask for more coins.
  155.            If credit <= 20 Then
  156.                 getCoins()
  157.             End If
  158.  
  159.             Dim item As Integer = handleItemList()
  160.             If item <> -1 Then
  161.                 buyProduct(item)
  162.             Else
  163.                 getItemsBought()
  164.                 Exit While
  165.             End If
  166.  
  167.         End While
  168.         Console.ReadLine()
  169.     End Sub
  170.  
  171. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement