ATM Algorithm

debit-cardsAn ATM, a.k.a. Cash Withdrawal Machine, uses a computer program to interact with the customer and count the number of banknotes to dispense based on the customer’s requested amount.

In the UK, ATM’s tend to only stock £20 banknotes and £10 banknotes and apply the following rules:

  • The minimal amount that can be withdrawn is £10,
  • The maximal amount that can be withdrawn is £200,
  • The amount to be withdrawn must be a multiple of 10 e.g. £10, £20, £30, £40, … £180, £190, £200.

Quotient (DIV) and Remainder (MOD)


To calculate how many £20 banknotes and how many £10 banknotes correspond to the requested amount, the ATM program uses two arithmetic operators called DIV and MOD.

The quotient (DIV) returns the result of the whole division. For instance:

70 DIV 20 = 3

The remainder (MOD) returns the remainder of the whole division. For instance:

70 MOD 20 = 10

By using these operators we can deduct that £70 pounds will result in:

  • 70 DIV 20 = 3 banknotes of £20
  • 70 MOD 20 = 10 (= 1 banknote of £10)

Note that in Python the DIV operator is // whereas the MOD operator is %.
For instance:

  • quotient = 70 // 20
  • remainder = 70 % 20

Also to find out if a number is a multiple of 10, we can check if number MOD 10 == 0. Effectively, if the remainder of dividing this number by 10 is null, we can conclude that this number is a multiple of 10.

ATM Algorithm: Pseudocode


The suggested pseudocode for our ATM is as follows:

WHILE TRUE
   DISPLAY "Welcome to Python Bank ATM - Cash Withdrawal"
   amount = INPUT "How much would you like to withdraw today?"
   IF (amount MOD 10) != 0 THEN
      DISPLAY "You can only withdraw a multiple of ten!"
   ELSE
      IF amount<10 OR amount>200 THEN
         DISPLAY "You can only withdraw between £10 and £200"
      ELSE
         notes20 = amount DIV 20
         notes10 = (amount MOD 20) / 10
         DISPLAY "Collect your money: "
         DISPLAY "    >> £20 Banknotes: " + notes20
         DISPLAY "    >> £10 Banknotes: " + notes10
         DISPLAY "Thank you for using this Python Bank ATM."
         DISPLAY "Good Bye."
      END IF
   END IF
END WHILE

Your task is to use this pseudocode to implement this algorithm using Python.

Extension Task 1


The bank reloads the ATM to its full capacity every morning. The ATM has a full capacity of £1,000.
Adapt this code so that:

  1. When you run the code, the ATM starts with a full capacity of £1,000.
  2. After each withdrawal, take away the amount withdrawn from the ATM total capacity.
  3. If the ATM is empty a message is displayed to inform that the ATM is not operational.
  4. If the ATM is not empty, change your code to check that it contains enough cash left to meet the amount requested by a customer before dispensing the banknotes.
  5. If the customer requested amount cannot be met, inform the customer of the maximum amount that is still available to withdraw.

Extension Task 2


When the bank reloads the ATM, it ensures that the ATM contains exactly:

  • 30 banknotes of £20.
  • 40 banknotes of £10.

Adapt your code to ensure that the ATM checks that there are enough banknotes of £20 or £10 before dispensing the banknotes. Note that if the ATM runs out of £20 banknotes, it may still be able to dispense the requested amount using £10 banknotes instead.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.1 / 5. Vote count: 22

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: