PHP ATM withdrawal problem using if … else

Tram Ho

The problem of withdrawing money from the ATM

  • The amount of 500k bill is in ATM
  • The 200k bill is in the ATM
  • The amount of 100k bill is in ATM
  • The amount of 50k bills is in the ATM

Enter the amount you want to withdraw

=> Print out the number of bills with different denominations

For example:

  • Amount of 500k bills in ATM: 2
  • Amount of 200k bill in ATM: 2
  • Amount of 100k banknotes in ATM: 2
  • The amount of 50k bills in ATM: 2
  • Enter the amount you want to withdraw: 2000k => ATM does not have enough money
  • Enter the amount you want to withdraw: 1500k => 500K – 2 sheets, 200k – 2 sheets, 100k – 1 sheet
  • Enter the amount you want to withdraw: 1555k => The amount you want to withdraw must be a multiple of 50

Solution to the problem: First, wish we will create a form with 5 lines of text

  • Text line enter the number of 500k sheets that atm has
  • the text enter the 200k sheet number that atm has
  • the text enter the 100k sheet number that atm has
  • Text line enter the 50k sheet number that atm has
  • text enter the amount you want to withdraw

=> we will create 2 files, 1 file is index.php and 1 file is welcome.php, on the index.php file we create a form, you welcome us to handle the problem,

  • Step 1: – get the value from the form
  • Step 2: -check if the amount you want to withdraw is greater than the amount of the atm tree, we will report the error of the atm tree is not enough money
  • Step 3: -check if the amount is not a multiple of 50000 then the error is because the smallest face value to be withdrawn is 50000
  • Step 4:
    • TH1_check that if the amount to be withdrawn is divisible by 500000, the largest price will notice the withdrawal (amount to be withdrawn / 500000) a 500k note
    • TH2_ case the division is not fully divided by 500k, we round down the above division to print the number of 500k sheets, then take the remainder of the division before dividing by 200k again into 2 cases
      • TH1_Check that the remainder is divisible by 200k if not, then print the number of 200k sheets by dividing the balance by 200k
      • TH2_Not divisible by all, we take the result of the division to round down to print the number of 200k sheets and then take the remainder after dividing by 200k divided by 100k, again in 2 cases
        • TH1_check that the remainder if 100k is divisible by 100k = the remainder of the 200k division divided by 100k
        • TH2_Not divisible, we take the result of the division to round down to print the number of 100k sheets, then take the remainder after dividing by 100k divided by 50k, then print the number of 50k sheets = the balance divided by 100k divided by 50k

Below is the code and the illustration

code index.php (code form)

*** code welcome.php

Share the news now

Source : Viblo