Reputation:
In the program I am working on for my Visual Basic class, I am to write a program that will calculate how many calories are in a recipe.
My form has 2 list boxes (lstIngredients and lstRecipe) and a text box for the quantity (txtQuantity), and a button to calculate the calories (btnCalculate). There are other things, but these that I've listed are the only ones pertinent to this question.
I have already written the code to add the selected item with it's appropriate quantity to the Recipe list box, but am stuck as to how to calculate the calories.
In the Ingredients list box are the items: eggs(each), flour(cups), milk(cups), sugar(cups), and butter(tablespoons). With the instructions, we were given the calories for each of these items: 1 egg= 72 calories, 1 cup flour= 455 calories, 1 cup milk= 86 calories, 1 cup sugar= 774 calories, and 1 tablespoon butter= 102 calories. Using these values, the items added to the recipe list, and their quantities, the program is supposed to calculate the total number of calories in that recipe when the user clicks the Calculate Calories button.
I understand the math that would be done in this, if the recipe calls for 2 eggs, 3 cups flour, and 2 cups milk, I'd have to multiply each of the ingredients' calories by the quantity, and then add all those values together to get the total calories for the recipe. But I do not know how to connect the caloric values to the individual items.
Here's the code I've written so far.
Public Class Form1
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim i As Integer = lstIngredients.SelectedIndex
Dim Quantity As Integer
If Trim(txtQuantity.Text = "") Then
Quantity = 1
Else
Quantity = Me.txtQuantity.Text
End If
Dim intCount As Integer = 0
While intCount < Quantity
lstRecipe.Items.Add(lstIngredients.Text)
intCount += 1
End While
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
lstRecipe.Items.Clear()
txtQuantity.Clear()
txtAnswer.Clear()
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim dblCalories As Double
If txtQuantity.Text = "" Then
txtAnswer.Text = "0 Calories"
End If
End Sub
End Class
Also, would this be an instance where I could use a data set? I've never used them before, so have no idea how that works, but if it could make this simpler I"m all for it ha.
Upvotes: 0
Views: 2059
Reputation: 6948
But I do not know how to connect the caloric values to the individual items.
Since the amount of calories will be dependent on the amount of each ingredient, a class to represent an ingredient would be helpful:
Class Ingredient
Public Property Name As String = ""
Public Property Amount As Double = 0
Public Property Measure As Measurements = Measurements.Tsp
Private Property Calories As Double = 0
Private Property CalMeas As Measurements = Measurements.Tsp
Public Sub New(_name As String, _calories As Double, _caloriemeasure As Measurements, Optional _amount As Double = 0, Optional _measure As Measurements = Measurements.Tsp)
Name = _name
Calories = _calories
CalMeas = _caloriemeasure
Amount = _amount
Measure = _measure
End Sub
Public Function GetCalories() As Double
Return (Amount * Measure) * (Calories / CalMeas)
End Function
Public Overrides Function ToString() As String
Return Join({Name.PadRight(10, " "c), (Amount.ToString().PadRight(4, " "c) + [Enum].GetName(GetType(Measurements), Measure)).PadRight(10, " "c), GetCalories.ToString.PadLeft(5, " "c)}, vbTab)
End Function
End Class
Enum Measurments
Unit = 1
Tsp = 1
Tbsp = 3
Cup = 48
End Enum
Now if you have a List(Of Ingredient)
called Ingredients
then a simple lambda in the Sum method will give you the calorie total, Ingredients.Sum(Function(x) x.GetCalories)
.
Dim Ingredients As New List(Of Ingredient)(
{
New Ingredient("Flour", 455, Measurements.Cup, 3, Measurements.Cup),
New Ingredient("Milk", 86, Measurements.Cup, 2, Measurements.Cup),
New Ingredient("Sugar", 774, Measurements.Cup, 0.5, Measurements.Cup),
New Ingredient("Egg", 72, Measurements.Unit, 2, Measurements.Unit),
New Ingredient("Butter", 102, Measurements.Tbsp, 1, Measurements.Cup)
})
Dim total = Ingredients.Sum(Function(x) x.GetCalories)'3700.0
The constructor is set up so that you pass the name of the ingredient, the number of calories per the measure and optionally the amount and the measure.
By setting the list as the datasource for the listbox, the listbox will use the ToString override to display the data, then it's a simple matter to show the calorie total.
This approach has the advantage of the listbox item actually being the Ingredient object. So changing the amount of one ingredient is a simple matter of casting the selecteditem to an Ingredient type and changing the amount.
Upvotes: 1
Reputation: 13106
You could create a
Dictionary<string,int>
(sorry for the c#) and then add each ingredient to the dictionary using the name of the ingredient as the key and the calories as the value. Then you can easily match up by looking up the key.
Edit (thanks to comment from davidsbro):
Something like:
Dictionary<string,int> dIngredients = new Dictionary<string, int>();
// Add the code here to add the ingredients
foreach(string s in lstIngredients)
{
dblCalories += dIngredients[s];
}
Upvotes: 1