public void AddProduct(string name, int quantity = 0, double price = 0, int offerId = 0) { // Check if existing basket item using the product name as reference BasketItem basketItem = BasketItems.FirstOrDefault(item => item.ProductName == name); if (basketItem == null) { // Create new instance of Offer class with default parameters Offer linkedOffer = new Offer(); // If offerId is not set, ignore discount and discount group if (offerId != 0) { linkedOffer = Offers.Find(offer => offer.OfferId == offerId); } // if does not exist, instantiate new BasketItem with passed parameters BasketItem newProduct = new BasketItem(name, price, quantity, linkedOffer); BasketItems.Add(newProduct); } else { // If the product already exists in the basket, just increase the quantity basketItem.AddItems(quantity); } }
public void AddItemToBasket(ClothesShopContext db, int itemId) { var existantBasketItem = BasketItems.FirstOrDefault(b => b.ClothingItem == db.ClothingItems.First(c => c.ClothingItemId == itemId).Name&& b.Size == Size); if (existantBasketItem != null) { existantBasketItem.Quantity += Quantity; existantBasketItem.Amount = (int)db.ClothingItems.First(c => c.ClothingItemId == itemId).Price *existantBasketItem.Quantity; Console.Clear(); Console.WriteLine($"{existantBasketItem.ClothingItem} ({Size}) Qty:{Quantity} added to basket"); } else { BasketItems.Add(new BasketItem() { ClothingItem = db.ClothingItems.First(c => c.ClothingItemId == itemId).Name, Size = Size, Quantity = Quantity, Amount = (int)db.ClothingItems.First(c => c.ClothingItemId == itemId).Price *Quantity } ); Console.Clear(); Console.WriteLine($"{BasketItems[BasketItems.Count() - 1].ClothingItem} ({Size}) Qty:{Quantity} added to basket"); } }