-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulti-function.simplex
More file actions
66 lines (48 loc) · 1.33 KB
/
multi-function.simplex
File metadata and controls
66 lines (48 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
DATA: Item
id: string
name: string
price: number
quantity: number
DATA: Cart
items: list of Item
total: number
FUNCTION: add_to_cart(cart, item) → Cart
RULES:
- if item already in cart, increase quantity
- otherwise add item to cart
- recalculate total
DONE_WHEN:
- item is in cart
- total reflects all items
EXAMPLES:
(empty_cart, item_a) → { items: [item_a], total: 10.00 }
(cart_with_a, item_a) → { items: [item_a(qty:2)], total: 20.00 }
(cart_with_a, item_b) → { items: [item_a, item_b], total: 25.00 }
ERRORS:
- invalid item → "Item must have id, name, and price"
- negative quantity → "Quantity cannot be negative"
FUNCTION: remove_from_cart(cart, item_id) → Cart
RULES:
- find item by id
- remove it from cart
- recalculate total
DONE_WHEN:
- item no longer in cart
- total is updated
EXAMPLES:
(cart_with_a_and_b, "a") → { items: [item_b], total: 15.00 }
(cart_with_a, "a") → { items: [], total: 0.00 }
(empty_cart, "a") → { items: [], total: 0.00 }
ERRORS:
- item not found → return cart unchanged (not an error)
FUNCTION: calculate_total(cart) → number
RULES:
- sum price × quantity for all items
DONE_WHEN:
- total is accurate
EXAMPLES:
(empty_cart) → 0.00
(cart_with_a) → 10.00
(cart_with_a_and_b) → 25.00
ERRORS:
- invalid cart → 0.00