Friday
March 24, 2017

Post a New Question

Posted by on Thursday, April 19, 2012 at 8:22am.

Write a simple ATM program. Ask the user to enter their account number, and then print their beginning balance. (Just make one up). Then ask them if they want to make a deposit or a withdrawal. Depending on their selection, call a function to perform the action that they wish. Then print the new balance. They should be able to do as many transactions as they wish, so use iteration here.

  • programming - , Thursday, April 19, 2012 at 9:12am

    Would you like to share with us what you have done so far, and what problems you are encountering, if any?
    It is also not clear which language should be used to write this program.

  • programming - , Thursday, April 19, 2012 at 11:43am

    Below is a small perl program which does the job. You may not know perl, but it should be possible to figure out what it's doing.

    It does not validate input as a number, but it does illustrate some of the possible gotchas, and what can be done.

    ------------------------------------


    print "Enter Account Number: ";
    $acct = <STDIN>;

    $bal = 500 + rand(500); #random balance from 500 to 1000
    $bal = int($bal*100)*.01; #restrict to 2 decimal places
    printf "Current Balance: \$%4.2f\n",$bal;

    while (1) {
    print "Deposit(D) or Withdrawal(W) or Quit(Q) ? ";
    until (($dw = <STDIN>) =~ /^\s*[dw]\s*$/i) { # make sure onlt D or W is entered
    exit if $dw =~ /^q/i;
    chomp $dw;
    print "\"$dw\" is not a valid choice.\n";
    print "Deposit(D) or Withdrawal(W)? ";
    }
    $dw =~ s/\s+//g; # strip white space
    $dw = "\U$dw"; # convert to upper case

    if ($dw eq "D") {
    $bal += update("Deposit");
    } else {
    $bal -= update("Withdrawal",$bal);
    }

    printf "New balance: \$%4.2f\n",$bal;
    print "\nAnother transaction [Y]? ";
    $yn = <STDIN>;
    last unless $yn =~ /^y?\s*$/i; #exit unless Y or <cr> entered
    }

    # Get deposit/withdrawal amount.
    # This function does not validate input as number
    sub update {
    my ($dw,$cb) = @_; # get D/W and current balance
    print "Amount of $dw: \$";
    $amt = <STDIN>;
    return $amt if $dw eq "Deposit"; # allow any deposit
    while ($amt > $cb) {
    printf "You cannot withdraw \$%4.2f -- balance is only \$%4.2f
    Please enter an amount less than \$%4.2f: \$",$amt,$cb,$cb;
    $amt = <STDIN>;
    }
    return $amt;
    }

Answer This Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Post a New Question