You are on page 1of 3

Dim Price As Decimal 'global variable

Sub INPUT(ByRef ItemID As String)


Console.WriteLine("enter ID")
ItemID = Console.ReadLine

End Sub

Function Expensive() As Decimal


Dim count As Integer
Dim average As Decimal
Dim max As Decimal
Dim min As Decimal
Dim total As Decimal
Dim Prices As Decimal 'local variable is Prices, global is Price

min = 10000
max = 0
count = 0
While count < 10
count = count + 1

Console.WriteLine("enter price")
Prices = Console.ReadLine

If Prices > max Then


max = Prices
End If

If Prices < min Then


min = Prices
End If

total = total + Prices

End While

Console.WriteLine("Most expensive is" & max)


Console.WriteLine("Least expensive is " & min)

average = total / 10
Return average

End Function

Function Validate(ItemID As String) As Boolean


Dim last As String
Dim remaining As String
Dim valid As Boolean
valid = True

If Len(ItemID) <> 9 Then


valid = False
End If

If Left(ItemID, 3) <> "ABC" Then


valid = False
End If

last = Right(ItemID, 4)
If IsNumeric(last) = False Then
valid = False
End If

remaining = Mid(ItemID, 4, 2)
If remaining <> LCase(remaining) Then
valid = False
End If

Return valid

'note: this is a simple solution but if you use nested if else it would be more
efficient
End Function
Sub ApplyDiscount(ItemId As String)
Dim TodayDate As Date
Dim category As String
Dim DiscountedPrice As Decimal
'Price is global, hardcode a price to execute in vb
TodayDate = Now()
Dim ID As Char
ID = Right(ItemId, 1)

Select Case ID
Case "7"
category = "Decor"
DiscountedPrice = Price - Price * 10 / 100
Case "8"
category = "Stationery"
DiscountedPrice = Price - Price * 20 / 100
Case "9"
category = "Dairy"
DiscountedPrice = Price - Price * 15 / 100
Case Else
Console.WriteLine("Invalid ItemId")
End Select

Console.WriteLine("Discounted Price is" & DiscountedPrice & "Category is " &


category & "Date is" & TodayDate)

End Sub

Sub Main()
Dim ItemID As String
Dim result As Boolean

Call INPUT(ItemID)

result = Validate(ItemID)

If result = True Then 'you may call function in condition directly


Call ApplyDiscount(ItemID)
Else
Console.WriteLine("Incorrect ItemID")
End If

Console.ReadKey()
End Sub

You might also like