Skip to content
C++ project #04Beginner

Shopping Cart with Receipts

A store catalogue, a cart that respects stock levels, two kinds of coupons, sales tax and printed receipts — all in exact cents.

Estimated time
~3 hours to build
Files
5 files · 352 lines
Technologies
C++17, <map>, <iomanip>, <sstream>

Introduction

E-commerce logic is full of rules: you can't buy more than is in stock, coupons have conditions, tax applies after discounts. This project puts those rules in a Cart class backed by std::map, and prints an itemised receipt like a real till.

Problem statement

Simulate a small shop. Show a catalogue with prices and stock; let the user add and remove items by product code, apply a coupon (SAVE10 for 10% off, FLAT5 for $5 off orders of $40 or more), view the cart, and check out. Checkout prints a receipt with line totals, subtotal, discount, 8% tax and total, reduces the stock, and empties the cart.

Objectives

  • Encapsulate business rules in a class
  • Use std::map for a catalogue and for cart quantities
  • Hold a reference to shared data (the catalogue) inside an object
  • Format money and tables with <iomanip>

Features

  • Stock-aware add (shows how many more are available)
  • Partial or full removal of an item
  • Two coupon types with eligibility rules
  • Receipts with tax rounded to the nearest cent; stock updated after checkout

Concepts demonstrated

  • Classes
  • std::map
  • References as members
  • Business rules
  • Integer arithmetic for money

System architecture

The catalogue is a std::map<std::string, Product> created in main. Cart keeps a reference to it plus its own map<code, quantity> and the active coupon. add checks the code exists and that cart quantity plus the new quantity doesn't exceed stock. subtotal, discount and the tax calculation are separate functions so each rule is easy to read. checkout subtracts quantities from the catalogue's stock and clears the cart. The command loop in main only parses input and prints the messages returned by Cart.

Folder structure

TextProject tree
shopping-cart/
├── README.md
├── Makefile
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.cpp
Product, money, the Cart class (add/remove/coupons/totals/receipt/checkout) and the command loop.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Shopping Cart ===
Commands: catalog | add <code> <qty> | remove <code> [qty] | cart | coupon <code> | checkout | quit
> P01 Notebook $3.49 stock 50
P02 Gel pen (pack) $5.99 stock 30
P03 USB-C cable $12.99 stock 10
P04 Headphones $24.99 stock 3
P05 Water bottle $8.75 stock 20
P06 Backpack $39.99 stock 5
> Added 4 x Notebook
> Only 3 more Headphones available
> Added 2 x Headphones
> No product with code P09
> Added 1 x Water bottle
> Code Item Qty Price Line
P01 Notebook 4 $3.49 $13.96
P04 Headphones 2 $24.99 $49.98
P05 Water bottle 1 $8.75 $8.75
Subtotal: $72.69
> Unknown coupon WELCOME
> Coupon SAVE10 applied
> Cart now has 3 x Notebook
> ----------------- RECEIPT #1 -----------------
Code Item Qty Price Line
P01 Notebook 3 $3.49 $10.47
P04 Headphones 2 $24.99 $49.98
P05 Water bottle 1 $8.75 $8.75
Subtotal: $69.20
Discount (SAVE10): -$6.92
Tax 8%: $4.98
TOTAL: $67.26
Thank you for your order!
> Added 1 x Backpack
> Coupon FLAT5 applied
> ----------------- RECEIPT #2 -----------------
Code Item Qty Price Line
P06 Backpack 1 $39.99 $39.99
Subtotal: $39.99
Coupon FLAT5: not eligible
Tax 8%: $3.20
TOTAL: $43.19
Thank you for your order!
> P01 Notebook $3.49 stock 47
P02 Gel pen (pack) $5.99 stock 30
P03 USB-C cable $12.99 stock 10
P04 Headphones $24.99 stock 1
P05 Water bottle $8.75 stock 19
P06 Backpack $39.99 stock 4
>
Orders placed: 2
Input
catalog
add P01 4
add P04 5
add P04 2
add P09 1
add P05
cart
coupon WELCOME
coupon SAVE10
remove P01 1
checkout
add P06 1
coupon FLAT5
checkout
catalog
quit

Testing

  • Compare the scripted session with tests/expected-output.txt.
  • Receipt 1: subtotal $69.20, SAVE10 discount $6.92, tax 8% of $62.28 = $4.98, total $67.26.
  • Receipt 2: FLAT5 is not applied because $39.99 is below $40.
  • After both orders the catalogue shows reduced stock (Notebook 47, Headphones 1, Backpack 4).

Common errors

std::out_of_range from map::at

Only call catalog_.at(code) for codes that are known to exist; add checks with find first.

Total is off by one cent

Round tax once, at the end: (amount * 8 + 50) / 100 rounds to the nearest cent in integer arithmetic.

Possible improvements

  • Load the catalogue from a CSV file
  • Support multiple coupons with different priorities
  • Save receipts to numbered text files

Additional challenges

  1. 1Add a 'buy 2 get 1 free' offer on one product.
  2. 2Make the tax rate depend on the product category.
  3. 3Add a restock <code> <qty> admin command.

Built and tested it yourself? Mark it to update your dashboard.