Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit Off
- Module Module1
- '' DEBUG-MODE 1 (True), 0 (False)
- #Const DEBUG = 1
- Dim credit As Integer = -1 '' Credit with dummy value
- Dim S_product As String() = {"Cheese 'n onion walkers", "Rockstar Energy drink", "Twix", "Snickers", "Ready salted"}
- Dim N_product As Integer() = {50, 99, 69, 67, 50}
- Dim list(5) As String '' List of products (5 values maximum)
- Dim itemsBought As Integer = 0
- '' Adds a product to the list of bought items.
- '' n (int) - The position of the list
- '' name (string) - Product name
- Sub addProduct(n As Integer, name As String)
- list(n) = name
- Debug.Print("Added product: " & name & " to list at position {0}", n)
- End Sub
- '' Checks if you have enough to buy an item by comparing
- '' an integer with the amount of credit the user has.
- ''Params: n (int) the integer to compare with credit
- Function isEnough(ByVal n As Integer) As Boolean
- If credit >= n Then
- Return True
- End If
- #If Debug = 1 Then
- Debug.Print("Credit is {0} however not enough.", credit)
- #End If
- Return False
- End Function
- '' Buy product with the given product position.
- '' Returns True if product is bought without problem otherwise
- '' false which could be due to having not enough credit or
- '' an incorrect given position.
- Function buyProduct(ByVal productPosition As Integer) As Boolean
- If productPosition >= 1 And productPosition <= 5 Then
- '' Check if user has enough money
- Dim productPrice As Integer = N_product(productPosition - 1)
- Debug.Assert(IsNumeric(productPrice)) '' is the position correct and will it return a numeric value?
- If isEnough(productPrice) Then
- credit = credit - productPrice
- Dim prodName As String = S_product(productPosition - 1)
- Console.WriteLine("Bought item: {0} current credit: {1}p", prodName, credit)
- #If Debug = 1 Then
- Debug.Print("Bought item: {0}", prodName)
- Debug.Print("Credit reduced to {0}.", credit)
- #End If
- '' Increment counter
- itemsBought = itemsBought + 1
- '' Same case with list array.
- list(itemsBought - 1) = prodName
- Return True
- Else
- Console.WriteLine("Not enough credit")
- Return False
- End If
- Else
- #If Debug = 1 Then
- Debug.Print("Incorrect product position") '' Kill the user.
- #End If
- Return False
- End If
- End Function
- '' Loop until a valid amount of credit is input.
- Sub getCoins()
- Dim counter = 0
- If itemsBought >= 1 Then
- Console.WriteLine("Type 'exit' otherwise continue!")
- Dim tmp As String = Console.ReadLine()
- If tmp = "exit" Then
- getItemsBought()
- Stop
- End If
- End If
- Do
- Console.WriteLine("Please enter the amount of credit you wish to input in pennies (£1 (100p), £2 (200p), 50p): ")
- Dim tmp As String
- tmp = Console.ReadLine()
- If IsNumeric(tmp) Then
- credit = tmp
- Else
- counter = counter + 1
- End If
- '' Exit loop....
- If counter = 5 Then
- Stop
- End If
- Loop Until (credit > -1)
- End Sub
- Sub getItemsBought()
- For i = 0 To list.Length - 1
- If Not IsNothing(list(i)) Then
- Console.WriteLine("#{0} Item bought: {1}", i + 1, list(i))
- End If
- Next
- End Sub
- Sub getItemsForSale()
- For i = 0 To S_product.Length - 1
- Console.WriteLine("Number: {0} || Product name: {1}", i + 1, S_product(i))
- Next
- End Sub
- '' Use simple recursion to get the input of user.
- Function handleItemList(Optional ByVal n As Integer = 0) As Integer
- Dim counter = n
- If counter >= 5 Then
- Return -1
- End If
- getItemsForSale()
- Console.WriteLine("Type the number of the product you would like to buy otherwise type 'exit': ")
- Dim S_tmp As String = Console.ReadLine()
- Dim N_tmp As Integer = -1
- If IsNumeric(S_tmp) Then
- N_tmp = S_tmp
- If N_tmp >= 1 Or N_tmp <= 5 Then
- Return N_tmp
- Else
- counter = counter + 1
- Return handleItemList(counter)
- End If
- ElseIf S_tmp = "exit" Then
- Return -1
- Else
- Return handleItemList()
- End If
- End Function
- Sub Main()
- Dim stopProgram As Boolean = False
- While stopProgram <> True
- '' Ask for more coins.
- If credit <= 20 Then
- getCoins()
- End If
- Dim item As Integer = handleItemList()
- If item <> -1 Then
- buyProduct(item)
- Else
- getItemsBought()
- Exit While
- End If
- End While
- Console.ReadLine()
- End Sub
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement