Home > Net >  Showing the name of the most expensive product in vb
Showing the name of the most expensive product in vb

Time:12-28

i'm pretty new to programming and i got stuck trying to write a program where you type in the name and prices of products and you get back the total, the name prices and the most expensive product. Everything works fine except showing the name of the most expensive product. Here's what i've done

""

    Public Class Mrj
    Shared Sub main()
    Dim i, n As Integer

    Console.WriteLine("Enter the number of products")
    n = Console.ReadLine()

    Dim Products_name(n) As String
    Dim Products_price(n), HT, TTC, TVA, max As Decimal

    For i = 1 To n

        Console.WriteLine("Enter the name of the product " & i)
        Products_name(i - 1) = Console.ReadLine()

        Console.WriteLine("Enter the price of the product " & i)
        Products_price(i - 1) = Console.ReadLine()

        HT = HT   Products_price(i - 1)

    Next

    For i = 1 To n

        Console.WriteLine(Products_name(i - 1) & "   " & Products_price(i - 1))

    Next

    TVA = 0.2 * HT
    TTC = HT   TVA

    Console.WriteLine("Total to pay " & TTC)

    max = Products_price(0)

    For i = 1 To n - 1

        If max > Products_price(i) Then

        Else

            max = Products_price(i)

        End If

    Next

    Console.WriteLine("The product the most expensive is" & max & Products_name(i))

End Sub
End Class 

""

CodePudding user response:

I think the problem is that you are using i to get the name of the most expensive product, but that index i is always i = n since you don't save the index of the maximum value.

You should add a new variable where you store the index everytime you get a new maximum value, and use it in the last line.

Your for loop should be something like this:

Dim max_index As Integer
    
For i = 1 To n - 1
    
If max > Products_price(i) Then
    
 Else
   max = Products_price(i)
   max_index = i
 End If
    
Next
    
Console.WriteLine("The product the most expensive is" & max & Products_name(max_index))

Try this out and check if it works.

CodePudding user response:

Turn on Option Strict now and forever. Project Properties -> Compile tab. Also for future projects Tools -> Options -> Projects and Solutions -> VB Defaults

You cannot assume that a user will actually enter a number. Test with TryParse.

Arrays in vb.net are declared Products_name(upper bound). In this case that would be Products_name(n-1)

Instead of doing i - 1 for the indexes in the For loop, start our with For i = 0 to n-1

I decided to not use the parallel arrays. Instead I made a tiny class and declared a List(Of Product). I filled the list with the user input setting the Properties of the Product.

I used Linq instead of loops for sums and max. Not necessarily faster but can be accomplished in a single line of code.

I use interpolated strings to display results. When your string is preceded by a $, you can insert variables directly in the text surrounded by braces. The colon following Price indicates a formatting character. Here, I used a C for currency.

Public Class Product Public Property Name As String Public Property Price As Decimal End Class

Sub main()
    Dim ProductList As New List(Of Product)
    Dim n As Integer

    Console.WriteLine("Enter the number of products")
    Integer.TryParse(Console.ReadLine, n)
    For i = 1 To n
        Dim p As New Product
        Dim pr As Decimal
        Console.WriteLine("Enter the name of the product " & i)
        p.Name = Console.ReadLine()
        Console.WriteLine("Enter the price of the product " & i)
        Decimal.TryParse(Console.ReadLine, pr)
        p.Price = pr
        ProductList.Add(p)
    Next
    For Each p In ProductList
        Console.WriteLine($"{p.Name} {p.Price:C}")
    Next
    Dim SubTotal As Decimal = ProductList.Sum(Function(item) item.Price)
    Dim Tax As Decimal = 0.2D * SubTotal
    Dim Total = SubTotal   Tax
    Console.WriteLine($"Total to pay  {Total:C}")
    Dim Prod = ProductList.OrderByDescending(Function(p) p.Price).FirstOrDefault()
    Console.WriteLine($"The product the most expensive is {Prod.Name} at {Prod.Price:C}")
    Console.ReadKey()
End Sub
  • Related