• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

VB.Net Console Application

I am trying to create a console application that will take an inputted dollar amount and tell you how much of each item you can by for that amount. like this example:

 Enter dollar amount: $10
You can buy any one of the following items with $10.

22.7273 pounds of Bananas
10.101 pounds of Apples
8.4034 pounds of Cucumbers
11.236 pounds of Carrots
12.6582 pounds of Oranges

Do you want to try for another amount? ...

Here is what I have so far.


Open in new window

'Project:       How Much Can I Buy Console App


    Private promptMessage As String = "Enter a dollar amount"
    Private welcomeMessage As String = " Welcome to How Much Can I Buy"


    Sub Main()

        Dim strInput As String
        Dim DollarAmt As Double = 0
        Dim ProductPrice As Double
        Dim ProductAmt As Double
        Dim ProductName As String

        Console.WriteLine(welcomeMessage)
        Console.WriteLine(promptMessage)

    ProductPrice = _{{0.44, 0.99, 1.19, 0.89, 0.79}}
        'ProductName =_ {{Bananas, Apples, Cucumbers, Carrots, Oranges}}
    While
            ProductAmt = DollarAmt / ProductPrice

    then print strInput

            If


                Console.WriteLine("You can buy any one of the following items with" & DollarAmt)
                strInput = Console.ReadLine()
        case bananas
                Console.WriteLine(ProductAmt & "pounds of Bananas")
        case apples
                Console.WriteLine(ProductAmt & "pounds of Apples")
        case cucumbers
                Console.WriteLine(ProductAmt & "pounds of Cucumbers")
        case carrots
                Console.WriteLine(ProductAmt & "pounds of Carrots")
        case oranges
                Console.WriteLine(ProductAmt & "pounds of Oranges")
End Module

Open in new window

0
IchigoMD
Asked:
IchigoMD
  • 2
1 Solution
 
Tom BeckCommented:
Your code seems overly complicated. Why the rectangular arrays? Why the "case"? Is this a student assignment?
0
 
IchigoMDAuthor Commented:
Yes I have trouble starting but once i know how to get it started i think I know the correct direction to go.
0
 
Tom BeckCommented:
Ok, student assignment. I can provide suggestions. You write all the code.

Your code seems unnecessarily complex. Unless the assignment calls for the use of rectangular arrays or Switch statements, I would take those components out. As I understand the assignment, you need four variables with assigned values (essentially constants) and one variable for user input that actually is "variable".

On a side note, since you only have one sub, it will not matter if the variables are declared globally (at the class level, outside of any sub routine) or locally (inside a sub routine).

You have two strings declared globally and this is working.

You need to store the products and prices in variables, and arrays are a good choice, but rectangular arrays are not necessary. As long as you keep the Product Prices and the Product Names in the same order in two separate, simple (one dimensional) arrays, the indexes will always be the same. In other words ProductPrice(0) will be the price per pound for ProductName(0), ProductPrice(1) will be the price per pound for ProductName(1), etc.

Here's some more information on the different types of arrays: http://www.startvbdotnet.com/language/arrays.aspx

If you want to allow the user to repeatedly try different dollar amounts to see the different results, then the While loop was a good choice. While loops continue until the condition being evaluated becomes "true". However inside the while loop you simply need to write to the console the result of the calculation and the product name for each product and each price per pound in the arrays, one result per line. No need for "case" statements. But what condition should evaluate for the While loop? If you want the application to continue until the user closes it, then you essentially want an endless loop. Create a condition to evaluate based on that.

Feel free to post the latest version of you code if you get stuck.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now