Start learning free

LESSON 411 OF 413 · PROJECTS: OBJECT-ORIENTED

Shopping Cart in C# in C#

Practice

A shop keeps its orders in a cart. Build two classes that work together: a LineItem is one row of the cart (a product, its price and how many were bought), and a Cart holds many rows and adds them up. The cart has line items (composition), it is not one.

LineItem

  • Public fields string name, double price, int quantity.
  • LineItem(string name, double price, int quantity) stores the three values with this..
  • double Subtotal() returns price * quantity.

Cart

  • It keeps its items in private List<LineItem> items = new List<LineItem>().
  • void Add(LineItem item) appends the item to the list.
  • double Total() returns the sum of the subtotals of all items: start double sum = 0, foreach over the items, return sum.

Rules:

  • The total is worked out every time it is asked for, from the items in the cart at that moment. Adding an item later changes the next total.
  • An empty cart has a total of 0.
  • An item with price 5 and quantity 3 has a subtotal of 15.
  • Prices in the driver are whole numbers.

Method names start with a capital letter in C#. The driver lives in Solution.MainFunction(); Console.WriteLine prints the double 75 as 75.

The driver (already in the starter code, don't change it) creates an empty cart, checks one subtotal, then fills a cart and prints the total twice:

class Solution {
    public void MainFunction() {
        Cart empty = new Cart();
        Console.WriteLine("Empty: " + empty.Total());
        LineItem apple = new LineItem("Apple", 5, 3);
        Console.WriteLine("Subtotal: " + apple.Subtotal());
        Cart cart = new Cart();
        cart.Add(apple);
        cart.Add(new LineItem("Bread", 20, 3));
        Console.WriteLine("Total: " + cart.Total());
        cart.Add(new LineItem("Milk", 10, 2));
        Console.WriteLine("Total: " + cart.Total());
    }
}

Expected output:

Empty: 0
Subtotal: 15
Total: 75
Total: 95

Write LineItem (fields, constructor, Subtotal) first, then Cart.

foreach (var item in items) sum += item.Subtotal();

class LineItem {
    public string name;
    public double price;
    public int quantity;

    public LineItem(string name, double price, int quantity) {
        this.name = name;
        this.price = price;
        this.quantity = quantity;
    }

    public double Subtotal() {
        return price * quantity;
    }
}

class Cart {
    private List<LineItem> items = new List<LineItem>();

    public void Add(LineItem item) {
        items.Add(item);
    }

    public double Total() {
        double sum = 0;
        foreach (var item in items) sum += item.Subtotal();
        return sum;
    }
}

class Solution {
    public void MainFunction() {
        Cart empty = new Cart();
        Console.WriteLine("Empty: " + empty.Total());
        LineItem apple = new LineItem("Apple", 5, 3);
        Console.WriteLine("Subtotal: " + apple.Subtotal());
        Cart cart = new Cart();
        cart.Add(apple);
        cart.Add(new LineItem("Bread", 20, 3));
        Console.WriteLine("Total: " + cart.Total());
        cart.Add(new LineItem("Milk", 10, 2));
        Console.WriteLine("Total: " + cart.Total());
    }
}

STEP 1 OF 18
    Ctrl + Enter to check

    Shopping Cart in C#

    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free