Final challenge: a new payment
After this: Add a GiftCard, predict what the checkout prints, and complete the class line.
The shop now sells gift cards. A GiftCard is a new kind of payment: it has a balance of its own, like a wallet, but no fee. If the amount is more than the balance, it refuses; otherwise it takes the amount off and says what is left.
This challenge has five steps, and each step is scored on its own. You will predict what the checkout prints with a gift card in the loop, complete the class line that makes GiftCard keep the PaymentMethod promise, count what had to change, follow one card through two orders, and say what each language does when a method is forgotten. Every step shows the code in Python and in Java; 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 GiftCard with 1000 on it joins the loop, between a card and a wallet with 500. Each places an order of 800. What does this print?
from abc import ABC, abstractmethod
class PaymentMethod(ABC): @abstractmethod def fee(self, amount): ...
@abstractmethod def pay(self, amount): ...
class Card(PaymentMethod): def fee(self, amount): return amount * 2 // 100
def pay(self, amount): return f"card charged {amount + self.fee(amount)}"
class Upi(PaymentMethod): def fee(self, amount): return 0
def pay(self, amount): return f"UPI paid {amount}"
class Wallet(PaymentMethod): def __init__(self, balance): self.balance = balance
def fee(self, amount): return amount * 1 // 100
def pay(self, amount): total = amount + self.fee(amount) if total > self.balance: return f"wallet refused: needs {total}, has {self.balance}" self.balance -= total return f"wallet paid {total}, left {self.balance}"
class GiftCard(PaymentMethod): def __init__(self, balance): self.balance = balance
def fee(self, amount): return 0
def pay(self, amount): if amount > self.balance: return f"gift card refused: needs {amount}, has {self.balance}" self.balance -= amount return f"gift card paid {amount}, left {self.balance}"
class Checkout: def __init__(self, method): self.method = method
def place_order(self, amount): return self.method.pay(amount)
for m in [Card(), GiftCard(1000), Wallet(500)]: print(Checkout(m).place_order(800))import java.util.List;
interface PaymentMethod { int fee(int amount); String pay(int amount);}
class Card implements PaymentMethod { public int fee(int amount) { return amount * 2 / 100; }
public String pay(int amount) { return "card charged " + (amount + fee(amount)); }}
class Upi implements PaymentMethod { public int fee(int amount) { return 0; }
public String pay(int amount) { return "UPI paid " + amount; }}
class Wallet implements PaymentMethod { private int balance;
Wallet(int balance) { this.balance = balance; }
public int fee(int amount) { return amount * 1 / 100; }
public String pay(int amount) { int total = amount + fee(amount); if (total > balance) { return "wallet refused: needs " + total + ", has " + balance; } balance -= total; return "wallet paid " + total + ", left " + balance; }}
class GiftCard implements PaymentMethod { private int balance;
GiftCard(int balance) { this.balance = balance; }
public int fee(int amount) { return 0; }
public String pay(int amount) { if (amount > balance) { return "gift card refused: needs " + amount + ", has " + balance; } balance -= amount; return "gift card paid " + amount + ", left " + balance; }}
class Checkout { private PaymentMethod method;
Checkout(PaymentMethod method) { this.method = method; }
String placeOrder(int amount) { return method.pay(amount); }}
public class Main { public static void main(String[] args) { for (PaymentMethod m : List.of(new Card(), new GiftCard(1000), new Wallet(500))) { System.out.println(new Checkout(m).placeOrder(800)); } }}Complete the first line of GiftCard, so that it keeps the PaymentMethod promise and the checkout accepts it. The program should print gift card paid 800, left 200.
from abc import ABC, abstractmethod
class PaymentMethod(ABC): @abstractmethod def fee(self, amount): ...
@abstractmethod def pay(self, amount): ...
class Card(PaymentMethod): def fee(self, amount): return amount * 2 // 100
def pay(self, amount): return f"card charged {amount + self.fee(amount)}"
class Upi(PaymentMethod): def fee(self, amount): return 0
def pay(self, amount): return f"UPI paid {amount}"
class Wallet(PaymentMethod): def __init__(self, balance): self.balance = balance
def fee(self, amount): return amount * 1 // 100
def pay(self, amount): total = amount + self.fee(amount) if total > self.balance: return f"wallet refused: needs {total}, has {self.balance}" self.balance -= total return f"wallet paid {total}, left {self.balance}"
____________________ # ← GiftCard keeps the PaymentMethod promise def __init__(self, balance): self.balance = balance
def fee(self, amount): return 0
def pay(self, amount): if amount > self.balance: return f"gift card refused: needs {amount}, has {self.balance}" self.balance -= amount return f"gift card paid {amount}, left {self.balance}"
class Checkout: def __init__(self, method): self.method = method
def place_order(self, amount): return self.method.pay(amount)
print(Checkout(GiftCard(1000)).place_order(800))interface PaymentMethod { int fee(int amount); String pay(int amount);}
class Card implements PaymentMethod { public int fee(int amount) { return amount * 2 / 100; }
public String pay(int amount) { return "card charged " + (amount + fee(amount)); }}
class Upi implements PaymentMethod { public int fee(int amount) { return 0; }
public String pay(int amount) { return "UPI paid " + amount; }}
class Wallet implements PaymentMethod { private int balance;
Wallet(int balance) { this.balance = balance; }
public int fee(int amount) { return amount * 1 / 100; }
public String pay(int amount) { int total = amount + fee(amount); if (total > balance) { return "wallet refused: needs " + total + ", has " + balance; } balance -= total; return "wallet paid " + total + ", left " + balance; }}
____________________ // ← GiftCard keeps the PaymentMethod promise private int balance;
GiftCard(int balance) { this.balance = balance; }
public int fee(int amount) { return 0; }
public String pay(int amount) { if (amount > balance) { return "gift card refused: needs " + amount + ", has " + balance; } balance -= amount; return "gift card paid " + amount + ", left " + balance; }}
class Checkout { private PaymentMethod method;
Checkout(PaymentMethod method) { this.method = method; }
String placeOrder(int amount) { return method.pay(amount); }}
public class Main { public static void main(String[] args) { System.out.println(new Checkout(new GiftCard(1000)).placeOrder(800)); }}To add GiftCard to the shop, which code was written or changed?
One checkout holds one gift card with 2000 on it, and places two orders of 1250. What does this print?
from abc import ABC, abstractmethod
class PaymentMethod(ABC): @abstractmethod def fee(self, amount): ...
@abstractmethod def pay(self, amount): ...
class Card(PaymentMethod): def fee(self, amount): return amount * 2 // 100
def pay(self, amount): return f"card charged {amount + self.fee(amount)}"
class Upi(PaymentMethod): def fee(self, amount): return 0
def pay(self, amount): return f"UPI paid {amount}"
class Wallet(PaymentMethod): def __init__(self, balance): self.balance = balance
def fee(self, amount): return amount * 1 // 100
def pay(self, amount): total = amount + self.fee(amount) if total > self.balance: return f"wallet refused: needs {total}, has {self.balance}" self.balance -= total return f"wallet paid {total}, left {self.balance}"
class GiftCard(PaymentMethod): def __init__(self, balance): self.balance = balance
def fee(self, amount): return 0
def pay(self, amount): if amount > self.balance: return f"gift card refused: needs {amount}, has {self.balance}" self.balance -= amount return f"gift card paid {amount}, left {self.balance}"
class Checkout: def __init__(self, method): self.method = method
def place_order(self, amount): return self.method.pay(amount)
checkout = Checkout(GiftCard(2000))print(checkout.place_order(1250))print(checkout.place_order(1250))interface PaymentMethod { int fee(int amount); String pay(int amount);}
class Card implements PaymentMethod { public int fee(int amount) { return amount * 2 / 100; }
public String pay(int amount) { return "card charged " + (amount + fee(amount)); }}
class Upi implements PaymentMethod { public int fee(int amount) { return 0; }
public String pay(int amount) { return "UPI paid " + amount; }}
class Wallet implements PaymentMethod { private int balance;
Wallet(int balance) { this.balance = balance; }
public int fee(int amount) { return amount * 1 / 100; }
public String pay(int amount) { int total = amount + fee(amount); if (total > balance) { return "wallet refused: needs " + total + ", has " + balance; } balance -= total; return "wallet paid " + total + ", left " + balance; }}
class GiftCard implements PaymentMethod { private int balance;
GiftCard(int balance) { this.balance = balance; }
public int fee(int amount) { return 0; }
public String pay(int amount) { if (amount > balance) { return "gift card refused: needs " + amount + ", has " + balance; } balance -= amount; return "gift card paid " + amount + ", left " + balance; }}
class Checkout { private PaymentMethod method;
Checkout(PaymentMethod method) { this.method = method; }
String placeOrder(int amount) { return method.pay(amount); }}
public class Main { public static void main(String[] args) { Checkout checkout = new Checkout(new GiftCard(2000)); System.out.println(checkout.placeOrder(1250)); System.out.println(checkout.placeOrder(1250)); }}Suppose GiftCard had been written without its fee method. What would happen in each language?
- Add a GiftCard that keeps the PaymentMethod promise, predict what the checkout prints, follow one card through two orders, and say when each language reports a forgotten method.