Question

Before this gets flagged, let me admit that it is fairly similar to a question I asked a couple days ago, about a couple of other issues, but after using answers for both of the problems I was having, I am now running into another issue. Also, not sure that the title actually fits the problem I'm having, but it seems like it could be an issue of how the code is ordered in the program.

The eventual goal of this program is to calculate how many calories would be in a recipe based on ingredients added to a Recipe list box from an Ingredients list box, and the quantities of each selected ingredient. I am also supposed to perform a couple of different tests to prevent the program from crashing.

  1. If the quantity text box is left empty, the program is supposed to default to adding 1 of that selected ingredient.
  2. If anything other than a number is entered into the text box, the program is supposed to display a message box asking the user to please enter a numeric quantity.

My main problem now is that no matter how I try to tweak the code to make it work, I cannot get the TotalCalories to increment, so it keeps giving me a value of 0 as the answer. I am kind of on a time crunch, so if it's at all possible to fix this without having to re-write a big portion of the code I already have, that'd be great.

Here's the code I have written

Public Class Form1

    Private TotalCalories As Integer = 0

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim i As Integer = lstIngredients.SelectedIndex
        Dim Quantity As Double
        Dim intCount As Integer = 0

        If Trim(txtQuantity.Text) = "" Then
            Quantity = 1
        Else
            Quantity = Me.txtQuantity.Text
        End If

        If IsNumeric(txtQuantity.Text) = False Then
            MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
        End If

        If intCount < Quantity Then
            lstRecipe.Items.Add(Quantity & " " & lstIngredients.Text)
            intCount += 1
        End If

        If lstRecipe.Text = "Eggs(each)" Then
            TotalCalories += Quantity * 72
        ElseIf lstRecipe.Text = "Flour(cups)" Then
            TotalCalories += Quantity * 455
        ElseIf lstRecipe.Text = "Milk(cups)" Then
            TotalCalories += Quantity * 86
        ElseIf lstRecipe.Text = "Sugar(cups)" Then
            TotalCalories += Quantity * 774
        ElseIf lstRecipe.Text = "Butter(tablespoons)" Then
            TotalCalories += Quantity * 102
        End If
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lstRecipe.Items.Clear()
        txtQuantity.Clear()
        txtAnswer.Clear()
        TotalCalories = 0
    End Sub

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        txtAnswer.Text = TotalCalories

    End Sub
End Class

I've also tried this using If lstRecipe.text= (Quantity & " " & "Eggs" & " " "(each)") Then ... But that didn't work either.

Was it helpful?

Solution

lstRecipe.Text will reference the SELECTED item in the recipe list box, which is not the same as the one you just added. Even if there was something selected, you would just add that many calories each time some other item was added. Since you are posting (Quantity & " " & lstIngredients.Text) to Recipe, the text would never match the Ingredient only text (and should be Quantity.ToString).

    lstRecipe.Items.Add(Quantity & " " & lstIngredients.Text)

    ' this assumes the quoted text is exactly 
    ' what they contain.  we cant see that, but
    ' lstRECIPE would be something like '4 Eggs(each)'
    ' this will also break if you change the text like
    ' add a space before the parens 'Eggs (each)'
    ' which is another reason a class is a much better way
    If lstIngredients.Text = "Eggs(each)" Then
        TotalCalories += Quantity * 72
    ElseIf lstIngredients.Text = "Flour(cups)" Then
        TotalCalories += Quantity * 455
    ElseIf lstIngredients.Text = "Milk(cups)" Then
        TotalCalories += Quantity * 86
    ElseIf lstIngredients.Text = "Sugar(cups)" Then
        TotalCalories += Quantity * 774
    ElseIf lstIngredients.Text = "Butter(tablespoons)" Then
        TotalCalories += Quantity * 102
    End If

Cleaner:

    lstRecipe.Items.Add(Quantity & " " & lstIngredients.Text)

    Select Case lstIngredients.Text
        Case "Eggs(each)" 
            TotalCalories += Quantity * 72
        Case "Flour(cups)" 
            TotalCalories += Quantity * 455
        Case "Milk(cups)" Then
            TotalCalories += Quantity * 86
        Case "Sugar(cups)" 
            TotalCalories += Quantity * 774
        Case "Butter(tablespoons)" 
            TotalCalories += Quantity * 102
    End Select

Since the Item, Quantity and Calories are all needed in different spots, this is ideal for a Class. Have these come up yet, because this the solution screams 'use a class'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top