// Reduces the amount of the given item by one or deletes the last remaining one from the cart private void TakeOutOfCart(ShoppingCartEntry cartEntry) { foreach (var item in _listArticles) { if (item.GetArticleNumber() != cartEntry.GetArticleNumber()) { continue; // If the item doesn't match -> continue searching } if (item.Amount > 1) { item.DeleteOne(); // If there is more than 1, reduce the amount by 1 OnEntryChanged(new NewEntryEventArgs(NewEntryEventArgs.ShoppingCartAction.Remove, cartEntry)); // Trigger event Logger.GetInstance().Log("Item removed from ShoppingCart"); } else { _listArticles.Remove(item); // If there is only one remove the whole item from the list OnEntryChanged(new NewEntryEventArgs(NewEntryEventArgs.ShoppingCartAction.Remove, cartEntry)); // Trigger event Logger.GetInstance().Log("Item removed from ShoppingCart"); } return; // We can jump out, because we have already found the item (only distinct items in the cart) } }
// Add 1 item to the cart (increase amount if article is already inside or private void AddToCart(ShoppingCartEntry newEntry) { try { // Search in _listArticles, if article is already in the cart foreach (var item in _listArticles) { if (item.GetArticleNumber() == newEntry.GetArticleNumber()) { item.AddOne(); // If found, add one item (automatically recalculates the price too) OnEntryChanged( new NewEntryEventArgs(NewEntryEventArgs.ShoppingCartAction.Add, newEntry)); // Trigger event Logger.GetInstance().Log($"Amount changed for '{item.GetArticleNumber()}'"); return; } } _listArticles.Add(newEntry); OnEntryChanged(new NewEntryEventArgs(NewEntryEventArgs.ShoppingCartAction.Add, newEntry)); // Trigger event } catch (Exception e) { Logger.GetInstance().Log("--Exception caught in SC: " + e.Message); } }