1class StandardShipping {
2 cost(order) {
3 return order.weight <= 2 ? 4.99 : 4.99 + (order.weight - 2) * 1.5;
4 }
5}
6
7class ExpressShipping {
8 cost(order) {
9 return 9.99 + order.weight * 2;
10 }
11}
12
13class StorePickup {
14 cost(order) {
15 return 0;
16 }
17}
18
19class Order {
20 constructor(weight) {
21 this.weight = weight;
22 }
23}
24
25class Checkout {
26 constructor(shipping) {
27 this.shipping = shipping;
28 }
29 setShipping(shipping) {
30 this.shipping = shipping;
31 }
32 total(order, subtotal) {
33 return subtotal + this.shipping.cost(order);
34 }
35}
36
37function setup() {
38 const standard = new StandardShipping();
39 return {
40 order: new Order(3),
41 standard,
42 express: new ExpressShipping(),
43 pickup: new StorePickup(),
44 checkout: new Checkout(standard),
45 };
46}
47
48const actions = {
49 total: (w) => w.checkout.total(w.order, 30),
50 useStandard: (w) => w.checkout.setShipping(w.standard),
51 useExpress: (w) => w.checkout.setShipping(w.express),
52 usePickup: (w) => w.checkout.setShipping(w.pickup),
53};
54