hi I need a java program that will use parallel array to hold an account number, account holders name, withraw, deposit, intial balance, balance and overdraft fee. It must also print out the account number, account holders name and balance.


thanks

Dani AI

Generated

Common causes for the wrong ending balance in this thread are all present in the code that was posted: the check amount is being added before the overdraft fee is applied (which makes the fee look like it's “multiplying” the transaction), braces are missing so deposit code runs when it shouldn’t, and the transaction counters/indexes are never advanced so entries overwrite each other. already spotted the initialization and brace problems; ’s suggestion to split logic into methods will make these fixes much easier to manage.

A small, safe pattern for processing transactions (keeps the check out of the ledger when it overdrafts and records the fee as its own transaction):

// simple sketch (different names than the original to avoid reuse)
char[] types = new char[100];
double[] amounts = new double[100];
double[] balances = new double[100];
int idx = 0;

if (type == 'c') {
    if (amount > balance) {
        double fee = (amount > 250.0) ? amount * 0.10 : 25.0;
        balance -= fee;                      // do not add the original check amount
        types[idx] = 'F'; amounts[idx] = fee; balances[idx] = balance;
        idx++;
    } else {
        balance -= amount;
        types[idx] = 'C'; amounts[idx] = amount; balances[idx] = balance;
        idx++;
    }
} else if (type == 'd') {
    balance += amount;
    types[idx] = 'D'; amounts[idx] = amount; balances[idx] = balance;
    idx++;
}

Practical improvements: use BigDecimal (or store cents in a long) for money to avoid rounding; prefer an ArrayList<Transaction> with a small Transaction class (type, amount, balanceAfter) instead of parallel arrays; always increment the index immediately after storing; check array bounds; accept both cases with Character.toLowerCase(...); and add unit tests for edge cases.

Quick test cases to verify behavior: starting 500.00, check 600.00 -> if rejecting the check apply fee only: 500.00 - 60.00 = 440.00; if allowing overdraft subtract both: 500.00 - 600.00 - 60.00 = -160.00. Decide which policy is required and implement the logic consistently.

Recommended Answers

All 7 Replies

hmm, is it the start of the new school year where you are?
Get a good book, there's at least one I know of that develops just that as one of its core example applications, good luck in finding it (will give you at least some work to do before you turn in the code verbatim as your own) :)

Hi everyone,

I remember doing some simple banking programs thread some where else and here is the link

I hope this has helped you

Thank You

Yours Sincerely

Richard West

Here's the program that I wrote, but I'm having problems with my ending balance and overdraft fee. The overdraft fee is multiplied by whatever amount I enter: can some help; here's the program:

import java.io.*;
import java.lang.*;


public class BankAccount5
{
    public static void main(String[]args)throws IOException
    {
        char [] newType = new char [20];
        double [] newTransaction = new double [20];
        double [] newBalance = new double [20];
        int o = 0;
        int s = 0;
        int t = 0;

        BufferedReader console = new BufferedReader (
            new InputStreamReader(System.in));

        System.out.println(" Enter Your Name");
        String name = console.readLine();

        System.out.println("Hello " + name);
        System.out.println(name +" " + "enter your account number");
        String account = console.readLine();

        System.out.println(" enter your original balance");
        String Bal = console.readLine();
        double initialBalance = Double.parseDouble(Bal);
        double balance = initialBalance;


        System.out.println("Enter c for Check; d for Deposit; or p to Print Summary");
        char continueAnswer = console.readLine().charAt(0);
        boolean answer = true;      
        newType [o] = continueAnswer;

        if (continueAnswer == 'p')answer = false;

        double Fee;
        double OverdraftTotal = 0;
        double checks = 0;
        double deposit  = 0;
        double TotalBalance = 0;

        while (answer == true)
        {
            System.out.println("Enter amount");
            String input = console.readLine();
            double transaction = Double.parseDouble(input);
            newTransaction [o] = transaction;


            if (continueAnswer == 'c')
            checks = checks + transaction;

            {
                checks = checks + 0;
                if (transaction > balance)
                {
                    if (transaction > 250)
                        Fee = transaction * .10;
                    else
                        Fee = 25.00;
                balance = (balance + transaction) - Fee;
                newBalance [t] =  TotalBalance;
                OverdraftTotal = OverdraftTotal + Fee;
                o = o + 0;
                t = t + 0;
                newType [o] = 'f';
                newTransaction [o] = Fee;


                }
                else 
                    balance = balance - transaction;
                    newBalance [t] = balance;

            }           
            if (continueAnswer == 'd')
                deposit = deposit + transaction;
                balance =  deposit + transaction;
                newBalance [t] = balance;
                o = o + 0;
                t = t + 1;

            System.out.println("Enter c for Check; d for Deposit; or p to Print Summary");
            continueAnswer = console.readLine().charAt(0);
            newType [o] = continueAnswer;
            if (continueAnswer == 'p')answer = false;

        }
        if (balance <= 0)
            System.out.println("Your account insufficient, You should enter $" + balance + " to bring account current");


        System.out.println("Name " + name + ", Account Number " + account); 
        System.out.println(" Account Statement");
        System.out.println("Your  balance  " + balance);    
        System.out.println("Amount of checks: " + checks);
        System.out.println("Overdraft total fees: $" + OverdraftTotal);
        System.out.println("Total Checks: $" + checks);
        System.out.println("Total Deposits: $" + deposit);
        while (s < o)
        {
            System.out.println(newType[s] + " " + newTransaction [s] + " " + newBalance[s]);
            s = s + 1;
            System.out.println("");
        }
}
}

1. Shouldn't TotalBalance start off with the initial balance?

double TotalBalance = initialBalance
//not: double TotalBalance = 0

2. When there are insufficient funds for a check, shouldn't the original transaction be cancelled and only the overdraft charge applied?

balance = (balance) - Fee;
//not: balance = (balance + transaction) - Fee;

3. Near line 78, it appears that you are missing the { } for the if.
The third line below is always executing even during withdrawals.

if (continueAnswer == 'd')
		deposit = deposit + transaction;
	balance = deposit + transaction;

4. Near line 82, Shouldn't o also be incremented as well? It is the transaction counter right?

o = o + 0;
t = t + 1;

For more help,

mmm..tough problem

You have written all the code in single program.
Why not break the logic for withraw, deposit, inquire balance, etc. operations into seperate methods.

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

Where's the snychronization? Your balance can become negative if you don't sync it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.