private void RemoveSingleFromCart(Purchase toRemove) { purchasesList.Remove(toRemove); // update the stock of the product foreach (Buyable b in currentList) { if (b.Prod.ProductID == toRemove.PrdID) { b.LeftInStock = b.LeftInStock + toRemove.Amount; } } ProductGrid.CancelEdit(); ProductGrid.Items.Refresh(); String total = Convert.ToString(int.Parse(totalPrice1.Text) - (toRemove.Price) * (toRemove.Amount)); totalPrice1.Text = total; totalPrice2.Text = total; totalAmount.Text = Convert.ToString(int.Parse(totalAmount.Text) - toRemove.Amount); if (!purchasesList.Any()) { emptyCart.Visibility = Visibility.Visible; removals.Visibility = Visibility.Collapsed; } // update product and amount selected in a dictionary if (currentCart.Any()) { currentCart.Remove(toRemove.PrdID); } }
private void RemoveManyFromCart(object sender, RoutedEventArgs e) { if (!purchasesList.Any()) { MessageBox.Show("The shopping cart is empty"); } else { // update the stock of the products foreach (Purchase p in purchasesList) { if (currentList.Any()) { foreach (Buyable b in currentList) { if (p.PrdID == b.Prod.ProductID) { b.LeftInStock = b.LeftInStock + p.Amount; } } } } ProductGrid.CancelEdit(); ProductGrid.Items.Refresh(); purchasesList.Clear(); // zero the total price and total amount of products in the shopping cart totalPrice1.Text = Convert.ToString(0); totalPrice2.Text = Convert.ToString(0); totalAmount.Text = Convert.ToString(0); currentCart.Clear(); } }
private void AddSingleToCart(Buyable b) { Purchase toBuy = new Purchase(b.Prod.ProductID, b.Prod.Name, b.Prod.Price, b.Amount); if (b.Amount < 0) { MessageBox.Show("Invalid product amount"); } else if (b.Amount == 0) { MessageBox.Show("please choose an amount to buy"); } else if (b.Amount > b.LeftInStock) { MessageBox.Show("We don't have so many " + b.Prod.Name + "s at E-MART!"); } else { // check if the same product is already in the shopping cart Purchase identical = null; if (purchasesList.Any()) { foreach (Purchase p in purchasesList) { if (p.PrdID == toBuy.PrdID) { identical = p; } } } if (identical == null) { purchasesList.Add(toBuy); } else { identical.Amount += toBuy.Amount; } // refresh the shopping cart datagrid purchaseGrid.CancelEdit(); purchaseGrid.Items.Refresh(); // update the stock of the product b.LeftInStock = b.LeftInStock - b.Amount; // update the total price and total amount of products in the shopping cart String total = Convert.ToString(int.Parse(totalPrice1.Text) + (b.Prod.Price) * (b.Amount)); totalPrice1.Text = total; totalPrice2.Text = total; totalAmount.Text = Convert.ToString(int.Parse(totalAmount.Text) + b.Amount); emptyCart.Visibility = Visibility.Collapsed; removals.Visibility = Visibility.Visible; // update product and amount selected in a dictionary bool flag = false; if (currentCart.Any()) { foreach (int inCart in currentCart.Keys) { if (b.Prod.ProductID == inCart && !flag) { flag = true; } } } if (flag) { currentCart[b.Prod.ProductID] = b.LeftInStock; } else { currentCart.Add(b.Prod.ProductID, b.LeftInStock); } // zero the amount in main table b.ZeroAmount(); // refresh the products datagrid ProductGrid.CancelEdit(); ProductGrid.Items.Refresh(); } }