Cags
Contributor
Hi, I've just started working on an application to help me easily work out nutritional information about the foods I am eating in a day. I've come up with a basic data structure and just wondered if anybody had any input on the solution I have currently come up with.
The application will store collections of Consumable's and Recipe's (in either xml or database, i'll cross that bridge when I come to it). Then the user can easily create a list of what they have eaten in a day and the application will generate a list of how many calories it was etc.
C#:
public struct Nutrition {
public int Fat, Protein, Carbs, Calories;
public Nutrition(int myFat, int myProtein, int myCarbs, int myCalories)
{
Fat = myFat;
Protein = myProtein;
Carbs = myCarbs;
Calories = myCalories;
}
}
public class Consumable {
private Nutrition _nutritionInfo;
private string _name;
public Nutrition NutritionInfo {
get { return _nutritionInfo; }
}
public string Name {
get { return _name; }
}
public Consumable(Nutrition pNutritionInfo, string pName) {
_nutritionInfo = pNutritionInfo; // per 100g
_name = pName;
}
}
public class Ingredient {
private Consumable _foodType;
private int _amount;
public Consumable FoodType {
get { return _foodType; }
}
public int Amount {
get { return _amount; }
}
public Ingredient(Consumable pFoodType, int pAmount) {
_foodType = pFoodType;
_amount = pAmount;
}
public Nutrition GetNutritionalInfo() {
int fat = (FoodType.NutritionInfo.Fat / 100) * Amount;
int carbs = (FoodType.NutritionInfo.Carbs / 100) * Amount;
int protein = (FoodType.NutritionInfo.Protein / 100) * Amount;
int calories = (FoodType.NutritionInfo.Calories / 100) * Amount;
return new Nutrition(fat, protein, carbs, calories);
}
}
public class Recipe {
private ArrayList _myIngredients; // will be converted to typed array
private string _name;
public ArrayList Ingredients {
get { return _myIngredients; }
set { _myIngredients = value; }
}
public string Name {
get { return _name; }
set { _name = value; }
}
public Recipe(string pName) {
_myIngredients = new ArrayList();
_name = pName;
}
public Nutrition GetNutritionalInfo() {
int fat = 0, carbs = 0, protein = 0, calories = 0;
for(int i = 0; i < _myIngredients.Count; i++) {
fat += ((Ingredient)_myIngredients[i]).FoodType.NutritionInfo.Fat;
carbs += ((Ingredient)_myIngredients[i]).FoodType.NutritionInfo.Carbs;
protein += ((Ingredient)_myIngredients[i]).FoodType.NutritionInfo.Protein;
calories += ((Ingredient)_myIngredients[i]).FoodType.NutritionInfo.Calories;
}
return new Nutrition(fat, protein, carbs, calories);
}
}