Chapter 10 Activity Instructions

Activity: ATM

 

Automatic Teller MachineIn this chapter, you learned how to write your own Python classes (objects). Those classes can be split into separate source files for better re-usability and clarity. Let's put those skills to use on a banking program that simulates an Automatic TellerMachine (ATM)!

 

 

Program Overview

This program will use three separate source files:

  • main.py - You will create the main program code inside this file
  • Bank.py - You will create the ATM class inside this file
  • Input.py - We provide a complete Validator class to help get user input

Your ATM class will be responsible for keeping a running balance of the user's bank account. The account will start with $20.00 and go up or down as the user makes deposits and withdrawals.

Your main code will import the Bank and Input source files in order to use the ATM and Validator classes. The program will first get the starting balance and print it to the screen. Next, you will prompt the user for a verified amount to deposit, call the deposit() function on the ATM, and print out the resulting balance. Finally, you will prompt the user for a verified amount to withdraw, call the withdrawal() function on the ATM and print out the final balance.

Here is a sample program run, first depositing $15.50 and then withdrawing $25.00.

Starting Balance: $20.00
Please enter deposit amount ($0.00 - $1000.00): 15.50
Depositing $15.50
New Balance: $35.50
Please enter withdrawal amount ($0.00 - $1000.00): 25.00
Withdrawing $25.00
New Balance: $10.50

Your ATM must also protect against attempts to withdraw too much money. The withdrawal() function will print an "Insufficient funds" error message if the amount is greater than the available balance.

Starting Balance: $20.00
Please enter deposit amount ($0.00 - $1000.00): 15.50
Depositing $15.50
New Balance: $35.50
Please enter withdrawal amount ($0.00 - $1000.00): 150.00
Insufficient funds
New Balance: $35.50

Of course, the Validator object should be used to get a verified floating point amount within the allowed range, 0.00 to 1000.00. The user will be prompted to retry until valid amounts are entered.

Starting Balance: $20.00
Please enter deposit amount ($0.00 - $1000.00): zip
Please enter deposit amount ($0.00 - $1000.00): 10
Depositing $10.00
New Balance: $30.00
Please enter withdrawal amount ($0.00 - $1000.00): all
Please enter withdrawal amount ($0.00 - $1000.00): -1
Please enter withdrawal amount ($0.00 - $1000.00): 1500
Please enter withdrawal amount ($0.00 - $1000.00): 10
Withdrawing $10.00
New Balance: $20.00

 

Starting Code

Your "main.py" and "Bank.py" source files are initially empty. It's your job to write the code in these files based on the detailed steps below. The "Input.py" source file is provided for you, and you should not have to make any changes to the Validator class.

If you are working offline, you can copy and paste this code into an "Input.py" source file that you create. But if you are working with CompuScholar's auto-graded system, you will find this code waiting for you when you click on the activity link in the course interface.

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

Notice that Validator has a get_float() function that works similar to the input validation logic you have used recently. Provide a message prompt and a minimum and maximum value, and the class will ensure the user enters a valid floating point number in the allowed range.

Detailed Program Requirements

Start by working on your ATM class in the "Bank.py" source file.

  1. Name your class "ATM"
  2. Add a class variable called balance and initialize it to 20.00.
  3. Define a method called deposit() that takes an input parameter named amount. Inside deposit():
    1. Print "Depositing $XX.XX", where XX.XX is the amount to two decimal places. Remember, you can use str.format() with the "{:.2f}" placeholder in the format string to display a floating point value to two decimal places.
    2. Set the balance equal to the balance plus the amount. Remember, to access a class variable, always use the class name on front (e.g. "ATM.balance").
    3. Return the new balance from the function.
  4. Define a method called withdraw() that takes an input parameter named amount. Inside withdraw():
    1. Verify that the current balance is greater than or equal to the amount. If there is enough money:
      1. Print "Withdrawing $XX.XX", where XX.XX is the amount to two decimal places.
      2. Set the balance equal to the balance minus the amount.
    2. Else print "Insufficient Funds"
    3. Return the new balance from the method.
  5. Define a method called get_balance() that simply returns the current balance.

Once your ATM class is complete, switch over to the "main.py" source file to complete your main program. The main program should complete these steps:

  1. Import the "Bank.py" and "Input.py" source files. Remember to omit the ".py" file extension.
  2. Declare a variable called balance and set it equal to the results of calling the ATM.get_balance() function. Remember to include the filename when calling a class in another file (e.g. "Bank.ATM.get_balance()").
  3. Print "Starting balance: $XX.XX", where XX.XX is the balance to two decimal places.
  4. Use the Input.Validator class to get a floating point number between 0.00 and 1000.00. The prompt should read "Please enter deposit amount ($0.00 - $1000.00): ". Store the resulting input in a variable called amount.
  5. Call the ATM's deposit() function and pass in the amount. Save the returned value in a variable called balance.
  6. Print "New balance: $XX.XX", where XX.XX is the balance to two decimal places.
  7. Repeat steps 9, 10 and 11 for a withdrawal. Change the input prompt to "Please enter withdrawal amount...", call the ATM's withdrawal() function, and print the new balance to the screen.

 

Activity Results

When finished, your program should successfully match the sample program runs shown above. Try testing it using a variety of deposit and withdrawal amounts and check that the correct balance is printed each time. Verify that the input verification feature works for both the deposit and withdrawal amount. Also confirm that an attempt to withdraw more money than is available produces the "Insufficient funds" error message.

When your program is complete and tested, submit it for grading.

Activity Rubric

If your project is being automatically graded by our system, your grade will be calculated from 0 to 100 as follows:

PointsDescription
5Successful import of Bank.py
5Successful import of Input.py
5get_balance() called on ATM class
5Validator get_float() used for deposit amount
5Validator get_float() used for withdrawal amount
5ATM class defined in Bank.py
5ATM balance class variable initialized to 20
5ATM deposit method defined
5ATM withdraw method defined
5ATM get_balance method defined
10Test starting balance display
10Test deposit results
10Test withdrawal results
10Test insufficient funds
10Test input validation

Last modified: Sunday, 18 August 2019, 10:12 PM