logo Practice-It logo

balanceCheckbook

Language/Type: Java Scanner
Author: Marty Stepp (on 2020/04/15)

Write a static method called balanceCheckbook that takes a console Scanner as a parameter and that prompts a user for information about transactions for a bank account, reporting the new balance after each transaction and the minimum balance at the end and returning whether or not the account was ever overdrawn (true if it was, false if it was not). The user is prompted for an initial balance, the number of transactions to process, and the individual transaction amounts. Deposits to the account are entered as positive numbers and checks and ATM withdrawals are entered as negative numbers. A new balance is reported after each transaction. For example, the method would be called as follows:

        Scanner console = new Scanner(System.in);
        balanceCheckbook(console);

Below are two sample logs of execution that might be produced:

        initial balance? 48.50                  initial balance? 39.75
        how many transactions? 4                how many transactions? 5
        1/4 amount? -20.00                      1/5 amount? -18.50
        new balance = $28.5                     new balance = $21.25
        2/4 amount? -5.75                       2/5 amount? -7.20
        new balance = $22.75                    new balance = $14.05
        3/4 amount? 138.20                      3/5 amount? -23.10
        new balance = $160.95                   new balance = $-9.05
        4/4 amount? -20.00                      4/5 amount? 50.00
        new balance = $140.95                   new balance = $40.95
        minimum balance = $22.75                5/5 amount? -8.45
                                                new balance = $32.5
                                                minimum balance = $-9.05
        

In the log to the left, the user enters 4 different transactions and the minimum balance is not negative, so the method would return false to indicate that the account was never overdrawn. In the log to the right, the user enters 5 transactions and the minimum balance is negative, so the method would return true to indicate that the account was overdrawn. You are to exactly reproduce the format of these logs. You may assume that the number of transactions entered by the user is at least 1.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.