I have created kisok program in C#, Windows Forms that adds a sandwich name, price, and number of selected fillings to a listbox when that specific sandwiches picturebox has been clicked on. The problem im having is that for example, if the user selects 2 fillings for a cheese sandwich and adds it to the shopping cart, then chooses the cheese sandwich again but this time with 3 fillings, the number of fillings for the first added sandwich (within the listbox) will also be changed to having 3 fillings. (The fillings are added to each sandwich by selecting fillings from a textbox and then adding this to a separate listbox next to the sandwich image. The number of fillings for each sandwich is then calculated by assigning the number of items in the listbox to an objects parameter variable called fillingsAmount.)
This is the click event handler for the cheese sandwich button, which adds the sandwich name, price and amount of fillings to a listbox called shopping cart:
private void picBoxCheese_Click(object sender, EventArgs e)
{
btnRemove.Enabled = true;
btnCheckout.Enabled = true;
sandwiches[(int)sandwichNames.Cheese].fillingAmount = lstbCheeseDesc.Items.Count - 1;
cart.AddToCart(sandwiches[(int)sandwichNames.Cheese]);
lblTotal.Text = "Total: " + cart.Total().ToString();
ShoppingCartBindingSource.ResetBindings(false);
}
This works but whenever the same item is clicked and added to the shopping cart, however many fillings that is selected for that newly added item is the amount of fillings that is displayed for the other added items. For example i can select a cheese sandwich with 3 fillings and a ham sandwich with 0 fillings, which will work fine and display “Cheese Sandwich + 3 toppings” and “Ham Sandwich + 0 toppings” in the shopping cart. If i was to then click on the Cheese sandwich again but this time with 1 topping, the shopping cart would display “Cheese Sandwich + 1 toppings”, “Ham Sandwich + 0 toppings”, and “Cheese Sandwich + 1 toppings”.
This is accessing the variable parameter of the object called sandwiches: sandwiches[(int)sandwichNames.Cheese].fillingAmount;
This is accessing the items in the listbox that displays fillings that are selected in a checkbox:
lstbCheeseDesc.Items.Count - 1;
This is a method of a Cart class, which adds the variable parameters of the object called sandwiches. Which includes the name, price and filligns amount:
cart.AddToCart(sandwiches[(int)sandwichNames.Cheese]);
This is the object called sandwiches:
sandwiches = new List<Sandwich> { new Sandwich(1, "Cheese Sandwich", 2.99m, Image.FromFile("CheeseSandwich.jpg"), new string[]{"Extra cheese", "Ketchup", "Mayo", "Onion"}, new decimal[]{0.50m, 0.20m, 0.20m, 0.50m}, 0),<br/> new Sandwich(2, "Tuna Sandwich", 4.00m, Image.FromFile("TunaSandwich.jpg"), new string[]{"Extra cheese", "Ketchup", "mayo"}, new decimal[]{0.50m, 0.20m, 0.20m, 0.50m}, 0), new Sandwich(3, "Ham Sandwich", 3.50m, Image.FromFile("HamSandwich.jpg"), new string[]{"Extra cheese", "Ketchup", "mayo"}, new decimal[]{0.50m, 0.20m, 0.20m, 0.50m}, 0), new Sandwich(4, "Steak Sandwich", 5.00m, Image.FromFile("SteakSandwich.jpg"), new string[]{"Extra cheese", "Ketchup", "mayo"}, new decimal[]{0.50m, 0.20m, 0.20m, 0.50m}, 0) };
/************************************************************************************************/
This binds the products to the listbox called shopping cart
BindingSource ShoppingCartBindingSource = new BindingSource();
ShoppingCartBindingSource.DataSource = cart.GetAllProducts();
lstCart.DataSource = ShoppingCartBindingSource;
lstCart.DisplayMember = ToString();
Code explanation:
The “cart.GetAllProducts();” is a method from the Cart class. This is the method:
public List<Product> GetAllProducts() { return products; }
This returns all products added to it through the following method, also in the Cart class:
public void AddToCart(Product product) { products.Add(product); }
The ToString() method is from the Sandwich Class:
public override string ToString() { return $"{name}: {price.ToString()} + {fillingAmount.ToString()} fillings"; }
This is the properties and constructor in the Sandwich class, which gets the parameters of a object to use in a ToString() method, also in the Sandwich class:
Sandwich class
decimal[] FillingPrice { get; set; }
public int fillingAmount { get; set; }
public Sandwich(int _id, string _name, decimal _price, Image _image, string[] _filling,
decimal[] _fillingPrice, int _fillingAmount)
: base(_id, _name, _price, _image)
{
Filling = _filling;
FillingPrice = _fillingPrice;
fillingAmount = _fillingAmount;
}
public override string ToString()
{
return $"{name}: {price.ToString()} + {fillingAmount.ToString()} fillings";
}
Dan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.