Final challenge: shipping and a parcel
After this: Name a pattern, predict a strategy and an observer, complete a line, and decide whether a factory is worth it.
The shop from lesson 3 now ships parcels. Shipping has two price rules, Standard and Express, and a parcel must tell the buyer's phone and the shop's dashboard each time its status changes.
This challenge has five steps, and each step is scored on its own. You will name the pattern a problem needs, predict the quotes from a shipping strategy, complete the line that adds a watcher to a parcel, predict what the parcel prints, and decide whether a factory is worth adding. Where a step shows code, it shows it in Python and in Java, and the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
A parcel must tell the buyer's phone and the shop's dashboard each time its status changes (packed, shipped, delivered). Either of them may stop listening at any time. Which pattern fits?
The shop has two shipping rules. Standard costs 40 plus 10 a kilogram; Express costs 100 plus 20 a kilogram. What does this program print?
class Standard: def cost(self, kg): return 40 + 10 * kg
class Express: def cost(self, kg): return 100 + 20 * kg
class Shipping: def __init__(self, rule): self.rule = rule
def set_rule(self, rule): self.rule = rule
def quote(self, kg): return f"{type(self.rule).__name__}: {self.rule.cost(kg)}"
s = Shipping(Standard())print(s.quote(3))s.set_rule(Express())print(s.quote(3))interface ShippingRule { int cost(int kg);}
class Standard implements ShippingRule { public int cost(int kg) { return 40 + 10 * kg; }}
class Express implements ShippingRule { public int cost(int kg) { return 100 + 20 * kg; }}
class Shipping { private ShippingRule rule;
Shipping(ShippingRule rule) { this.rule = rule; }
void setRule(ShippingRule rule) { this.rule = rule; }
String quote(int kg) { return rule.getClass().getSimpleName() + ": " + rule.cost(kg); }}
public class Main { public static void main(String[] args) { Shipping s = new Shipping(new Standard()); System.out.println(s.quote(3)); s.setRule(new Express()); System.out.println(s.quote(3)); }}Complete the body of watch, so that the parcel remembers the new watcher in its list. The program should print the three status lines and then 1.
class Phone: def update(self, code, status): print(f"phone: {code} is {status}")
class Dashboard: def __init__(self): self.seen = 0
def update(self, code, status): self.seen += 1 print(f"dashboard: {code} {status}")
class Parcel: def __init__(self, code): self.code = code self.watchers = []
def watch(self, w): ________ # ← remember this watcher
def stop_watching(self, w): self.watchers.remove(w)
def set_status(self, status): for w in self.watchers: w.update(self.code, status)
p = Parcel("P7")phone = Phone()board = Dashboard()p.watch(board)p.watch(phone)p.set_status("packed")p.stop_watching(board)p.set_status("delivered")print(board.seen)import java.util.ArrayList;import java.util.List;
interface Watcher { void update(String code, String status);}
class Phone implements Watcher { public void update(String code, String status) { System.out.println("phone: " + code + " is " + status); }}
class Dashboard implements Watcher { int seen = 0;
public void update(String code, String status) { seen += 1; System.out.println("dashboard: " + code + " " + status); }}
class Parcel { private final String code; private final List<Watcher> watchers = new ArrayList<>();
Parcel(String code) { this.code = code; }
void watch(Watcher w) { ________ // ← remember this watcher }
void stopWatching(Watcher w) { watchers.remove(w); }
void setStatus(String status) { for (Watcher w : watchers) { w.update(code, status); } }}
public class Main { public static void main(String[] args) { Parcel p = new Parcel("P7"); Phone phone = new Phone(); Dashboard board = new Dashboard(); p.watch(board); p.watch(phone); p.setStatus("packed"); p.stopWatching(board); p.setStatus("delivered"); System.out.println(board.seen); }}Now the whole parcel program, with watch filled in. What does it print?
class Phone: def update(self, code, status): print(f"phone: {code} is {status}")
class Dashboard: def __init__(self): self.seen = 0
def update(self, code, status): self.seen += 1 print(f"dashboard: {code} {status}")
class Parcel: def __init__(self, code): self.code = code self.watchers = []
def watch(self, w): self.watchers.append(w)
def stop_watching(self, w): self.watchers.remove(w)
def set_status(self, status): for w in self.watchers: w.update(self.code, status)
p = Parcel("P7")phone = Phone()board = Dashboard()p.watch(board)p.watch(phone)p.set_status("packed")p.stop_watching(board)p.set_status("delivered")print(board.seen)import java.util.ArrayList;import java.util.List;
interface Watcher { void update(String code, String status);}
class Phone implements Watcher { public void update(String code, String status) { System.out.println("phone: " + code + " is " + status); }}
class Dashboard implements Watcher { int seen = 0;
public void update(String code, String status) { seen += 1; System.out.println("dashboard: " + code + " " + status); }}
class Parcel { private final String code; private final List<Watcher> watchers = new ArrayList<>();
Parcel(String code) { this.code = code; }
void watch(Watcher w) { watchers.add(w); }
void stopWatching(Watcher w) { watchers.remove(w); }
void setStatus(String status) { for (Watcher w : watchers) { w.update(code, status); } }}
public class Main { public static void main(String[] args) { Parcel p = new Parcel("P7"); Phone phone = new Phone(); Dashboard board = new Dashboard(); p.watch(board); p.watch(phone); p.setStatus("packed"); p.stopWatching(board); p.setStatus("delivered"); System.out.println(board.seen); }}In the whole shop, the Shipping object is made in exactly one line, with Standard, and the rule only changes when the buyer ticks an Express box. A teammate wants a ShippingRuleFactory. What is the better advice?
- Name the pattern a shipping problem needs, predict a strategy and an observer, complete the line that adds a watcher, and decide when a factory is not worth it.