Reputation: 29
Alright I have 8-10 check boxes and radio buttons, and i need to sum up the double values that are assigned to them. The only problem is that I only want to check some of the boxes not all of them. If you could help me out that would be great. Also any pointers on my code will also help
double SpringMix = 2.00;
double Avocado = 1.50;
double Beans = 2.00;
double Cheese = 2.00;
double Tomato = 1.50;
double HoneyMustard = 2.00;
double Ranch = 2.00;
double Italian = 2.00;
double BlueCheese = 2.00;
double FoodCost;
I'm using if statements to see if the check boxes are checked. I need a way to add them all up depending on if they are checked.
public double TotalOrderCost()
{
if (cbSpringMix.Checked)
{
FoodCost + 2.00;
}
if (cbAvocado.Checked)
{
FoodCost + 1.50;
}
if (cbBeans.Checked)
{
FoodCost + 2.00;
}
if (cbCheese.Checked)
{
Upvotes: 1
Views: 1471
Reputation: 2281
This should help. I wrote a simple Console Application to demonstrate how to do this. It looks a lot more complicated than it is, and that is only because I had to put extra stuff in there to simulate a Sql or what ever you might be using. I would just focus on the Food class and the Ingredient class. How they are set up mainly. Also, take a look at the bottom of the static void main to see how I called the methods that I made in the food class. I have updated this with regions and comments to make it easier to read.
class Program
{
static void Main(string[] args)
{
#region SimulatedDb
// Technically a Food object from the Database
var myFood = new Food() { Name = "Sub", Ingredients = new List<IngredientViewModel>(), Price = 8.00 };
// Technically Ingredients from the Database
var cbSpringMixIn = new Ingredient() { Name = "Spring Mix", Price = 2.00 };
var cbAvacodoIn = new Ingredient() { Name = "Avacodo", Price = 1.50 };
var cbBeansIn = new Ingredient() { Name = "Beans", Price = 2.00 };
#endregion
// This would actually be in your code
// You would probably just do a for each statement to turn all of these into
// Objects and add them to the list of available ingredients
var cbSpringMix = new IngredientViewModel(cbSpringMixIn);
var cbAvacodo = new IngredientViewModel(cbAvacodoIn);
var cbBeans = new IngredientViewModel(cbBeansIn);
#region SimulatedUserInterface
Console.WriteLine("What would you like on your {0} ({1:C})", myFood.Name, myFood.Price);
Console.WriteLine();
Console.WriteLine("Would you like {0} ({1:C})", cbSpringMix.Name, cbSpringMix.Price);
Console.WriteLine("yes or no");
var answer = Console.ReadLine();
if (answer == "yes")
{
cbSpringMix.Selected = true;
}
Console.WriteLine();
Console.WriteLine("Would you like {0} ({1:C})", cbAvacodo.Name, cbAvacodo.Price);
Console.WriteLine("yes or no");
var answer1 = Console.ReadLine();
if (answer1 == "yes")
{
cbAvacodo.Selected = true;
}
Console.WriteLine();
Console.WriteLine("Would you like {0} ({1:C})", cbBeans.Name, cbBeans.Price);
Console.WriteLine("yes or no");
var answer2 = Console.ReadLine();
if (answer2 == "yes")
{
cbBeans.Selected = true;
}
#endregion
// This would actually be in your code
// You would probably just do a for each statement to turn all of these into
// Objects and add them to the list of available ingredients
myFood.Ingredients.Add(cbAvacodo);
myFood.Ingredients.Add(cbBeans);
myFood.Ingredients.Add(cbSpringMix);
#region SimulatedUserInterfaceContinued
Console.WriteLine("You are ready to check out! Press any key to continue...");
var checkout = Console.ReadLine();
Console.Write("You have ordered a {0} with ", myFood.Name);
foreach (var ingredient in myFood.GetSelectedNames())
{
Console.Write(ingredient + " ");
}
Console.WriteLine();
Console.WriteLine("Your Final Price is:");
Console.WriteLine(String.Format("{0:C}", myFood.GetFullPrice()));
Console.ReadLine();
#endregion
}
public class Food
{
public Food()
{
}
public IEnumerable<string> GetSelectedNames()
{
return (
from f in this.Ingredients
where f.Selected
select f.Name).ToList();
}
public IEnumerable<double> GetSelectedValues()
{
return (
from f in this.Ingredients
where f.Selected
select f.Price).ToList();
}
public double GetFullPrice()
{
var selectedIngredients = GetSelectedValues();
return selectedIngredients.Sum() + this.Price;
}
public string Name { get; set; }
public double Price { get; set; }
public virtual List<IngredientViewModel> Ingredients { get; set; }
}
public class IngredientViewModel
{
public IngredientViewModel(Ingredient ingredient)
{
this.Name = ingredient.Name;
this.Price = ingredient.Price;
this.Selected = false;
}
public string Name { get; set; }
public double Price { get; set; }
public bool Selected { get; set; }
}
public class Ingredient
{
public string Name { get; set; }
public double Price { get; set; }
}
}
}
Upvotes: 0
Reputation: 2125
I have this version as a solution:
private readonly Dictionary<CheckBox, double> mapping;
public MainWindow()
{
InitializeComponent();
mapping = new Dictionary<CheckBox, double>
{
{cbBean, 2d},
{cbSpringMix, 2d}
//...
};
}
public double TotalOrderCost()
{
double foodCost = 0d;
foreach (KeyValuePair<CheckBox, double> keyValuePair in mapping)
{
if (keyValuePair.Key.Checked)
{
foodCost += keyValuePair.Value;
}
}
return foodCost;
}
LINQ version of TotalOrderCost:
public double TotalOrderCost()
{
return mapping.Where(keyValuePair => keyValuePair.Key.Checked).Sum(keyValuePair => keyValuePair.Value);
}
You can add more comboboxes without modifiing the code multiple places.
Upvotes: 1