/** * AddItem() - Adds an item to the shopping */ public void AddItem(ENNewBook book) { // Create a new item to add to the cart ENNewBook b = new ENNewBook(book.Id); CartItem newItem = new CartItem(b); // If this item already exists in our list of items, increase the quantity // Otherwise, add the new item to the list foreach (CartItem item in Items) { if (item.BookIsbn == book.IdBook) { item.Quantity++; return; } } newItem.Quantity = 1; Items.Add(newItem); }
/** * Equals() - Needed to implement the IEquatable interface * Tests whether or not this item is equal to the parameter * This method is called by the Contains() method in the List class * We used this Contains() method in the ShoppingCart AddItem() method */ public bool Equals(CartItem item) { return item.BookIsbn == this.BookIsbn; }
/** * RemoveItem() - Removes an item from the shopping cart */ public void RemoveItem(ENNewBook book) { CartItem removedItem = new CartItem(book); foreach (var item in Items) { if (item.Equals(removedItem)) { Items.Remove(item); break; } } }