Putting it together
Combine the cart class with a console menu.
Putting it together
The Cart class is fully ready; in the final step we will handle a menu of commands with a switch. Main iterates over a command array with foreach; for each command the switch picks the right branch.
The commands are English keywords: list lists the products, total prints the subtotal, exit prints a goodbye message. This way a single loop combines every cart feature in one place.
foreach and switch together
Walking a list of commands with foreach and handling each with switch builds the core of a small console app. The cart.Items property lets you walk the cart contents from outside.
The list branch prints each product with foreach (var p in cart.Items); the total branch calls cart.GetSubtotal(); the exit branch prints a goodbye message. Each branch ends with break.
Exercise Write the switch menu
Inside Main's foreach (string command in commands) body, write a switch (command). Have three branches: case "list": walks cart.Items with foreach and prints each product as $"{p.Name} - {p.Quantity} x {p.Price} = {p.Quantity * p.Price}"; case "total": prints $"Total: {cart.GetSubtotal()}"; case "exit": prints "bye". Each branch ends with break.
Show example solution
...; break; case "total": ...; break; case "exit": ...; break; }
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 List<Product> Items => items;
public void AddProduct(Product product) { items.Add(product); }
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);
}
public double GetSubtotal()
{
double sum = 0;
foreach (var p in items) sum += p.Price * p.Quantity;
return sum;
}
}
class Program
{
static void Main()
{
var cart = new Cart();
cart.AddProduct(new Product { Name = "Notebook", Price = 25, Quantity = 2 });
cart.AddProduct(new Product { Name = "Pen", Price = 10, Quantity = 3 });
string[] commands = { "list", "total", "exit" };
foreach (string command in commands)
{
switch (command)
{
case "list":
foreach (var p in cart.Items)
Console.WriteLine($"{p.Name} - {p.Quantity} x {p.Price} = {p.Quantity * p.Price}");
break;
case "total":
Console.WriteLine($"Total: {cart.GetSubtotal()}");
break;
case "exit":
Console.WriteLine("bye");
break;
}
}
}
}
Test yourself
-
What does 'case "list":' mean inside switch (command)?
switch (command) { case "list": // ... break; }- The list is empty
- Only the list feature
- This branch runs when command is 'list'
- List all commands
Show answer
This branch runs when command is 'list' — Correct. case "list": matches only when command is 'list'.
-
Match each command to its job
- list
- Lists the products
- total
- Returns the subtotal
- exit
- Prints a goodbye message
- default
- Unknown command
-
Write the case label
switch (command) { ____ Console.WriteLine($"Total: {cart.GetSubtotal()}"); break; }Show answer
switch (command) { case "total": Console.WriteLine($"Total: {cart.GetSubtotal()}"); break; }