Updating quantity
Learn to manage the cart with UpdateQuantity and RemoveProduct.
Updating quantity
To change a product's quantity in the cart we will write an UpdateQuantity method. Signature: public void UpdateQuantity(string name, int quantity). In the body, walk the products with foreach and update the Quantity property of the matching one.
We will also add a RemoveProduct method to drop a product from the cart. This uses a for loop that walks indices backward and calls items.RemoveAt(i); going backward keeps removal safe from index shifts.
Backward removal
Deleting elements while walking a list with foreach is dangerous; the compiler raises an error. Instead, a for loop should walk backward from the last index: for (int i = items.Count - 1; i >= 0; i--).
Going backward handles the shift of subsequent indices smoothly when a removal happens. This is the only safe way to walk and modify a list; RemoveAll with LINQ is an alternative, but here we learn the explicit loop.
Exercise Write UpdateQuantity and RemoveProduct
Add two methods to the Cart class. public void UpdateQuantity(string name, int quantity): walk products with foreach and update the Quantity of the matching one. public void RemoveProduct(string name): walk backward with for (int i = items.Count - 1; i >= 0; i--) and remove matching products with items.RemoveAt(i). Main adds a Notebook, sets its quantity to 5 and tries to remove a Pen; GetSubtotal should return 25*5 = 125.
Show example solution
public void UpdateQuantity(string name, int quantity) { foreach (var p in items) { if (p.Name == name) p.Quantity = quantity; } }
public void RemoveProduct(string name) { for (int i = items.Count - 1; i >= 0; i--) { if (items[i].Name == name) items.RemoveAt(i); } }
Example solution:
using System;
using System.Collections.Generic;
class Product
{
public string Name { get; set; }
public double Price { get; set; }
public int Quantity { get; set; }
}
class Cart
{
private List<Product> items = new List<Product>();
public void AddProduct(Product product) { items.Add(product); }
public double GetSubtotal()
{
double sum = 0;
foreach (var p in items) sum += p.Price * p.Quantity;
return sum;
}
public void UpdateQuantity(string name, int quantity)
{
foreach (var p in items)
{
if (p.Name == name) p.Quantity = quantity;
}
}
public void RemoveProduct(string name)
{
for (int i = items.Count - 1; i >= 0; i--)
{
if (items[i].Name == name) items.RemoveAt(i);
}
}
}
class Program
{
static void Main()
{
var cart = new Cart();
cart.AddProduct(new Product { Name = "Notebook", Price = 25, Quantity = 2 });
cart.UpdateQuantity("Notebook", 5);
cart.RemoveProduct("Pen");
Console.WriteLine(cart.GetSubtotal());
}
}
Test yourself
-
Match each List member to its job
- items.Count
- Element count
- items.RemoveAt
- Remove by index
- items.Add
- Append to end
- items[i]
- Access by index
-
What does cart.UpdateQuantity("Notebook", 5) do?
cart.UpdateQuantity("Notebook", 5);- Sets the Notebook quantity to 5
- Removes the Notebook
- Adds a new Notebook
- Sets the total to 5
Show answer
Sets the Notebook quantity to 5 — Correct. UpdateQuantity sets the Quantity property of the matching product.
-
Write the items.RemoveAt call
for (int i = items.Count - 1; i >= 0; i--) { if (items[i].Name == name) ____(i); }Show answer
for (int i = items.Count - 1; i >= 0; i--) { if (items[i].Name == name) items.RemoveAt(i); }