## Activity Solution: ATM

In this activity, students will complete a banking simulation using three separate source files:

• main.py - written by the student, contains the main program code
• Bank.py - written by the student, contains the ATM class
• Input.py - provided for the student, contains the Validator object

The ATM class keeps a running balance as internal data, and provides simple deposit(), withdraw(), and get_balance() functions. The main code will get user input using the Validator object and pass those amounts into ATM functions, printing the resulting balance to the screen. In addition to new class-related skills, this activity re-enforces existing concepts around user input validation and numeric conversions.

### Activity Solution Code

The following solution code demonstrates a complete program with all three files.

# Student Name

# import source files

import Bank

import Input

# use the Bank.ATM to get current balance

balance = Bank.ATM.get_balance()

print(str.format("Starting Balance: ${:.2f}",balance)) # make a deposit and withdrawal based on user input amount = Input.Validator.get_float("Please enter deposit amount ($0.00 - $1000.00): ",0.00,1000.00) balance = Bank.ATM.deposit(amount) print(str.format("New Balance:${:.2f}",balance))

amount = Input.Validator.get_float("Please enter withdrawal amount ($0.00 -$1000.00): ",0.00,1000.00)

balance = Bank.ATM.withdraw(amount)

print(str.format("New Balance: ${:.2f}",balance)) # Student Name class ATM: balance = 20.00 # initialize starting balance # define the deposit() function def deposit(amount): print(str.format("Depositing${:.2f}",amount))

ATM.balance = ATM.balance + amount

return ATM.balance

# define the withdraw() function

def withdraw(amount):

# verify sufficient funds before withdrawal

if (ATM.balance >= amount):

print(str.format("Withdrawing \${:.2f}",amount))

ATM.balance = ATM.balance - amount

else:

print("Insufficient funds")

return ATM.balance

# define the get_balance() function

def get_balance():

return ATM.balance

class Validator:

def get_integer(prompt, min, max):

# loop until correct input received

while True:

try:

inputString = input(prompt)  # get user input

inputInt = int(inputString)  # try to convert to int

if (inputInt >= min) and (inputInt <= max):  # verify range

return inputInt            # success!

except:

continue                    # try again

def get_float(prompt, min, max):

# loop until correct input received

while True:

try:

inputString = input(prompt)      # get user input

inputFloat = float(inputString)  # try to convert to float

if (inputFloat >= min) and (inputFloat <= max):  # verify range

return inputFloat          # success!

except:

continue                    # try again