private void PlusBtn_Click(object sender, EventArgs e) { //Finding the required parent controls Button btnSender = (Button)sender; GroupBox gbSender = (GroupBox)btnSender.Parent; BasketGBControl endParent = (BasketGBControl)gbSender.Parent; Product prod = endParent.paramProd; //Looping through the available products o find the right one //Decreasing its quantity, increasing the quantity in the basket //Or showing the error message if no more items can be added to the basket foreach (Product productAv in ShopForm.AvailableProducts) { if (productAv.Equals(prod)) { if (productAv.Quantity > 0) { prod.Quantity++; endParent.TrueQtyLbl.Text = prod.Quantity.ToString(); productAv.Quantity--; ShopGBControl.QtyChange(endParent, productAv); ShopForm.ChangePriceWeightLbls(endParent, Basket.BasketList); return; } else { MessageBox.Show($"Sorry, {prod.Name.ToLower()} is out of stock."); return; } } } }
private void MinusBtn_Click(object sender, EventArgs e) { //Finding the required parent controls Button btnSender = (Button)sender; GroupBox gbSender = (GroupBox)btnSender.Parent; BasketGBControl endParent = (BasketGBControl)gbSender.Parent; FlowLayoutPanel flpParent = (FlowLayoutPanel)endParent.Parent; Product prod = endParent.paramProd; //Checking the quantity of item in the basket and performing necessary actions accordingly //Decreasing quantity in basket, increasing quantity in available products, rerendering the labels //And, if necessary, removing the item from the basket if (prod.Quantity > 1) { prod.Quantity--; endParent.TrueQtyLbl.Text = prod.Quantity.ToString(); foreach (Product productAv in ShopForm.AvailableProducts) { if (productAv.Equals(prod)) { productAv.Quantity++; ShopGBControl.QtyChange(endParent, productAv); ShopForm.ChangePriceWeightLbls(endParent, Basket.BasketList); return; } } } else if (prod.Quantity == 1) { foreach (Product productAv in ShopForm.AvailableProducts) { if (productAv.Equals(prod)) { productAv.Quantity++; ShopGBControl.QtyChange(endParent, productAv); ShopForm.ChangePriceWeightLbls(endParent, Basket.BasketList); Basket.BasketList.Remove(prod); flpParent.Controls.Remove(endParent); return; } } } }
public static void AddToBasket(Object obj, EventArgs e) { //Adds a product to the basket Button addBtn = (Button)obj; GroupBox groupBox = (GroupBox)addBtn.Parent; ShopGBControl endParent = (ShopGBControl)groupBox.Parent; int prodQty = endParent.paramProd.Quantity; string boxName = groupBox.Text; //Loops through available products and checks if the name of ShopGBControl (the sender) //matches with the neame of the product, the quantity is more than 0 and the product //is not yet in the Basket and then adds it to the temporary basket. //Displays the appropriate error messages if a certain criterion is not met. foreach (Product prod in ShopForm.AvailableProducts) { if (prod.Name == boxName && prod.Quantity > 0 && !Basket.BasketList.Contains(prod)) { prodQty--; endParent.TrueQtyLbl.Text = prodQty.ToString(); Basket.TempBasketList.Add(prod); Basket.RenderBasket(groupBox); ShopForm.ChangePriceWeightLbls(endParent, Basket.BasketList); return; } else if (prod.Name == boxName && prod.Quantity == 0) { MessageBox.Show($"Sorry, the {prod.Name.ToLower()} is out of stock."); return; } else if (prod.Name == boxName && Basket.BasketList.Contains(prod)) { MessageBox.Show("The product is already in the basket!"); return; } } MessageBox.Show("An error has occured."); }
public static double ChangePriceWeightLbls(Object obj, List <Product> basket) { //Changes the Weight Category and Basket price labels upon adding the item to basket //Or plusing/minusing the item in the basket double basketPrice = 0; double weight = 0; string weightCat; foreach (Product prod in basket) { basketPrice += prod.Price * prod.Quantity; weight += prod.Weight * prod.Quantity; } if (weight < 2) { weightCat = "S"; } else if (weight < 5) { weightCat = "M"; } else { weightCat = "L"; } //Receives either ShopGBControl or BasketGBControl as a sender object Control objSender = (Control)obj; FlowLayoutPanel flpSender = (FlowLayoutPanel)objSender.Parent; ShopForm endParent = (ShopForm)flpSender.Parent; endParent.TrueBasketPriceLbl.Text = basketPrice.ToString(); endParent.TrueWeightCatLbl.Text = weightCat; return(basketPrice); }