Skip to content

Instantly share code, notes, and snippets.

@tbbooher
Last active November 5, 2023 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbbooher/6f4aedfd7f1c62e14edcffdadae4901a to your computer and use it in GitHub Desktop.
Save tbbooher/6f4aedfd7f1c62e14edcffdadae4901a to your computer and use it in GitHub Desktop.
class ATM:
def __init__(self):
self._total_twenties = 0
self._total_fives = 0
def deposit_twenty(self, amount):
if amount > 0:
self._total_twenties += amount
return f"Deposited {amount * 20} dollars successfully."
else:
return "Invalid amount. Please deposit a positive number of twenties."
def deposit_five(self, amount):
if amount > 0:
self._total_fives += amount
return f"Deposited {amount * 5} dollars successfully."
else:
return "Invalid amount. Please deposit a positive number of fives."
def can_withdraw(self, amount):
if amount <= 0 or amount % 5 != 0:
return False, "Invalid amount. Amount must be a positive multiple of 5."
# Calculate if the withdrawal can be made with available notes
total_amount = self._total_twenties * 20 + self._total_fives * 5
if amount > total_amount:
return False, "Insufficient funds."
twenties_needed = min(amount // 20, self._total_twenties)
amount -= twenties_needed * 20
fives_needed = amount // 5
if fives_needed > self._total_fives:
return False, "Cannot dispense the exact amount with available notes."
return True, "Withdrawal is possible."
def withdraw(self, amount):
can_withdraw, message = self.can_withdraw(amount)
if not can_withdraw:
return message
twenties_to_withdraw = min(amount // 20, self._total_twenties)
self._total_twenties -= twenties_to_withdraw
amount -= twenties_to_withdraw * 20
fives_to_withdraw = amount // 5
self._total_fives -= fives_to_withdraw
return f"Dispensed {twenties_to_withdraw * 20 + fives_to_withdraw * 5} dollars."
def __str__(self):
total_cash = self._total_twenties * 20 + self._total_fives * 5
return f"ATM has {self._total_twenties} twenty dollar bills and {self._total_fives} five dollar bills: ${total_cash}"
# Example usage:
atm = ATM()
print(atm) # Initially empty
print(atm.deposit_twenty(10)) # Deposit twenties
print(atm.deposit_five(0)) # Attempt to deposit an invalid amount of fives
print(atm.withdraw(40)) # Withdraw an amount
print(atm.withdraw(15)) # Attempt to withdraw an invalid amount
print(atm) # Check ATM status after transactions
class ATMdriver:
def __init__(self, atm_machine):
self.atm_machine = atm_machine
def display_menu(self):
print("Please select an option:")
print("W: withdraw money from the ATM")
print("T: add twenties to the ATM")
print("F: add fives to the ATM")
print("Q: quit")
def execute_option(self, option):
option = option.upper()
if option == 'W':
self.process_withdrawal()
elif option == 'T':
self.process_deposit_twenty()
elif option == 'F':
self.process_deposit_five()
elif option == 'Q':
self.quit()
else:
print("Invalid option selected.")
def validate_amount(self, amount):
return amount > 0 and amount % 5 == 0
def process_withdrawal(self, amount):
if self.validate_amount(amount):
print(self.atm_machine.withdraw(amount))
else:
print("Invalid amount. Amount must be a positive multiple of 5.")
def process_deposit_twenty(self, amount):
if amount > 0:
print(self.atm_machine.deposit_twenty(amount))
else:
print("Invalid amount. Please enter a positive number.")
def process_deposit_five(self, amount):
amount = int(input("How many fives do you want to add?\n"))
if amount > 0:
print(self.atm_machine.deposit_five(amount))
else:
print("Invalid amount. Please enter a positive number.")
def quit(self):
print("Quitting program. Final ATM status:")
print(self.atm_machine)
exit(0)
# Main interaction loop for the ATMdriver, assuming an ATM instance exists as atm_machine
def main():
atm_machine = ATM()
driver = ATMdriver(atm_machine)
while True:
driver.display_menu()
choice = input("Your choice: ")
driver.execute_option(choice)
print("\n")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment