user6424058
user6424058

Reputation:

Vb.net Method not returning value

I'm having an issue with my code returning -1 if isPicked is False in my GetWoodTypes method, my if statement is being completely ignored and I can't find the issue within the code, if anyone can help that would be great

Public Class Class1

    Shared Sub Main()
        Dim woodType() As String = {"Pine", "Oak", "Elm"}
        Dim woodPrice() As Integer = {100, 140, 200}
        Dim nameIndex As Integer = 0
        nameIndex = GetWoodTypes(woodType, woodPrice)
        Dim Quantity As Integer = 0
        Dim basePrice As Integer = 0
        Quantity = GetDrawerQtys()
        basePrice = GetPrices(woodType, woodPrice, nameIndex, Quantity)


        If nameIndex = -1 Then
            Console.Write("You have made the wrong selection, Please try Again")
            Console.ReadLine()
        Else
            DisplayResults(Quantity, woodType, woodPrice, basePrice, nameIndex)
        End If
    End Sub

    Public Shared Function GetWoodTypes(values() As String, Price() As Integer)
        Console.Write("Please select the type of wood you would like to purchase: Pine,Oak,Elm ")
        Dim Selectedwood As String = Console.ReadLine()
        Dim ispicked As Boolean = False
        Dim location As Integer = 0
        For i As Integer = 0 To values.Count - 1
            If Selectedwood.ToLower = values(i).ToLower Then
                ispicked = True
                location = i
            End If
        Next i
        If ispicked  Then
            Console.WriteLine("Thank you for selection, you have picked {0} at {1} ", values(location), Price(location))
            Return location
        Else
            Console.Write("Sorry")
            Return -1
        End If
    End Function

    Public Shared Function GetDrawerQtys()
        Console.WriteLine(" How Many Drawers do you want ")
        Dim DrawerQty As Integer = Console.ReadLine()
        Return DrawerQty
    End Function

    Public Shared Function GetPrices(types() As String, price() As Integer, location As Integer, Quantity As Integer)
        Console.Write("You have ordered a {0} with {1} drawers at {2} each ", types(location), Quantity, price(location))
        Dim Amount As Integer = (Quantity * 30) + price(location)
        Return Amount
    End Function

    Public Shared Sub DisplayResults(Quantity As Integer, type() As String, price() As Integer, basePrice As Integer, location As Integer)

        Console.WriteLine("Hello you have purchased {0} {1} at {2} your total is {3} ", Quantity, type(location), price(location), basePrice)
        Console.ReadLine()
    End Sub
End Class

Upvotes: 0

Views: 728

Answers (2)

Gabor
Gabor

Reputation: 3246

I would approach this in a different way.

First, I would define a separate class for holding the type and price of a specific wood:

Public Class Wood
    Public Property Type As String
    Public Property Price As Integer
End Class

Then I would create a separate class for doing wood and quantity selection, calculating the price and displaying the result of order.

Note: the return type of a function should be also declared for the sake of clarity, e.g.
Public Function GetSomeNumber() As Integer

Also I used .Select() and .FirstOrDefault() methods. They are part of LINQ. They accept a lambda expression as parameter. Just in case you have not met with them yet.

Public Class Shop

    Dim woods As List(Of Wood)

    Public Sub New()

        ' Initialize the "product catalogue" using collection initializer combined with object initializer.
        woods = New List(Of Wood) From
        {
            New Wood() With {.Type = "Pine", .Price = 100},
            New Wood() With {.Type = "Oak", .Price = 140},
            New Wood() With {.Type = "Elm", .Price = 200}
        }

    End Sub

    Public Function SelectWood() As Wood

        Dim selectedWood As Wood = Nothing
        Dim woodTypes As String = String.Join(" / ", woods.Select(Function(wood) wood.Type).ToArray())

        While True

            Console.Write("Please select the type of wood you would like to purchase [{0}]: ", woodTypes)

            Dim selectedWoodType As String = Console.ReadLine()

            ' Instead of ToLower() you may use String.Equals() so that you can specify if you want to compare ignoring case.
            ' FirstOrDefault() means that if the user typed a wood type correctly then returns the correct Wood object; otherwise, Nothing.
            selectedWood = woods.FirstOrDefault(Function(wood) String.Equals(wood.Type, selectedWoodType, StringComparison.OrdinalIgnoreCase))

            If (selectedWood IsNot Nothing) Then
                Exit While
            End If

            Console.WriteLine("Sorry.")

        End While

        Console.WriteLine("Thank you for selection, you have picked {0} at {1}", selectedWood.Type, selectedWood.Price)

        Return selectedWood

    End Function

    Public Function GetDrawerQtys() As Integer

        Dim drawerQty As Integer
        Dim rawQty As String = Nothing

        Do
            Console.Write("How many drawers do you want? ")
            rawQty = Console.ReadLine()
        Loop Until Int32.TryParse(rawQty, drawerQty)

        Return drawerQty

    End Function

    Public Function CalculatePrice(wood As Wood, quantity As Integer) As Integer
        Console.WriteLine("You have ordered a {0} with {1} drawers at {2} each.", wood.Type, quantity, wood.Price)
        Return wood.Price + 30 * quantity
    End Function

    Public Sub DisplayOrder(wood As Wood, quantity As Integer, basePrice As Integer)
        Console.WriteLine("Hello, you have purchased {0} {1} at {2} your total is {3}.", quantity, wood.Type, wood.Price, basePrice)
        Console.ReadLine()
    End Sub

End Class

Then you can have a module where you can use these classes.

Module Module1

    Sub Main()

        Dim shop As New Shop()

        Dim selectedWood As Wood = shop.SelectWood()
        Dim quantity As Integer = shop.GetDrawerQtys()
        Dim basePrice As Integer = shop.CalculatePrice(selectedWood, quantity)

        shop.DisplayOrder(selectedWood, quantity, basePrice)

    End Sub

End Module

Upvotes: 1

Jim Hewitt
Jim Hewitt

Reputation: 1792

You weren't checking nameIndex immediately after your call to GetWoodTypes. This will get you further, but there is still an issue with GetDrawerQtys if you don't enter an integer.

   Shared Sub Main()

        Dim woodType() As String = {"Pine", "Oak", "Elm"}
        Dim woodPrice() As Integer = {100, 140, 200}
        Dim nameIndex As Integer = 0
        nameIndex = -1
        Dim Quantity As Integer = 0
        Dim basePrice As Integer = 0
        'Quantity = GetDrawerQtys()
        'basePrice = GetPrices(woodType, woodPrice, nameIndex, Quantity)

        nameIndex = GetWoodTypes(woodType, woodPrice)
        While nameIndex = -1
            Console.WriteLine("You have made the wrong selection, Please try Again")
            nameIndex = GetWoodTypes(woodType, woodPrice)
        End While

        Quantity = GetDrawerQtys()
        basePrice = GetPrices(woodType, woodPrice, nameIndex, Quantity)
        DisplayResults(Quantity, woodType, woodPrice, basePrice, nameIndex)    
    End Sub


   Public Function GetDrawerQtys()

        Dim validQty As Boolean = False
        Dim DrawerQty As Integer

        While Not validQty
            Console.WriteLine(" How Many Drawers do you want ")
            validQty = Int32.TryParse(Console.ReadLine(), DrawerQty)
        End While

        Return DrawerQty

    End Function

Upvotes: 0

Related Questions