JkSoftware

Day 16 - money_machine.py

Nov 25th, 2021
1,042
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class MoneyMachine:
  2.     CURRENCY = "$"
  3.  
  4.     COIN_VALUES = {
  5.         "quarters": 0.25,
  6.         "dimes": 0.10,
  7.         "nickles": 0.05,
  8.         "pennies": 0.01
  9.     }
  10.  
  11.     def __init__(self):
  12.         self.profit = 0
  13.         self.money_received = 0
  14.  
  15.     def report(self):
  16.         """Prints the current profit"""
  17.         print(f"Money: {self.CURRENCY}{self.profit}")
  18.  
  19.     def process_coins(self):
  20.         """Returns the total calculated from coins inserted."""
  21.         print("Please insert coins.")
  22.         for coin in self.COIN_VALUES:
  23.             self.money_received += int(input(f"How many {coin}?: ")) * self.COIN_VALUES[coin]
  24.         return self.money_received
  25.  
  26.     def make_payment(self, cost):
  27.         """Returns True when payment is accepted, or False if insufficient."""
  28.         self.process_coins()
  29.         if self.money_received >= cost:
  30.             change = round(self.money_received - cost, 2)
  31.             print(f"Here is {self.CURRENCY}{change} in change.")
  32.             self.profit += cost
  33.             self.money_received = 0
  34.             return True
  35.         else:
  36.             print("Sorry that's not enough money. Money refunded.")
  37.             self.money_received = 0
  38.             return False
  39.  
  40.  
RAW Paste Data