Skip to content

Instantly share code, notes, and snippets.

@berinhard
Created March 22, 2019 19:17
Show Gist options
  • Save berinhard/40c216aeaff583b48fa8d2ad3745c93a to your computer and use it in GitHub Desktop.
Save berinhard/40c216aeaff583b48fa8d2ad3745c93a to your computer and use it in GitHub Desktop.
Paperboy 003
class Wallet:
def __init__(self, money):
self.money = money
def subtract(self, amount):
self.money -= amount
def add(self, amount):
self.money += amount
def empty(self):
self.subtract(self.money)
class Customer:
def __init__(self, name, wallet, coins_in_pocket):
self.name = name
self.wallet = wallet
self.coins_in_pocket = coins_in_pocket
def pay_for_something(self, payment):
total_money = self.coins_in_pocket
if self.wallet:
total_money += self.wallet.money
if payment > total_money:
return 0
to_pay = payment
if to_pay > self.wallet.money:
to_pay -= self.wallet.money
self.wallet.empty()
else:
to_pay = 0
self.wallet.subtract(payment)
if to_pay > 0:
self.coins_in_pocket -= to_pay
return payment
class Paperboy:
def __init__(self, num_of_papers, newspaper_price):
self.num_of_papers = num_of_papers
self.newspaper_price = newspaper_price
def deliver_paper(self, customer):
self.num_of_papers -= 1
payment = customer.pay_for_something(self.newspaper_price)
if payment == self.newspaper_price:
print("Thanks!")
else:
print("Show me the money!!!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment