-3

I'm making a a program for class. So far i have 1-3 done but i cant figure how to implement 4 and 5. I have been stuck on this for awhile. There are two classes that must be used.

  1. how many bank accounts are in the bank

2) Make a new array to hold the specified number of BankAccounts

3) In a loop, ask the user for the bank account number and balance, construct a BankAccount object with the account number and balance, and put the new BankAccount object in the array

4) After the user has entered all the bank accounts, use a for loop (or a for each loop) to compute the total balance of the accounts in the array

5) Print the computed total balance of the accounts and the average balance.

package Hw2;
import java.util.Scanner;
import java.util.ArrayList;

public class BankArrayTester {
public static void main(String[] args){
      Scanner in = new Scanner(System.in);
      System.out.println("Please enter the number bank accounts:");
      int accounts= in.nextInt();
      BankAccount [] accountinfo = new BankAccount [accounts];
      int c=0;
      while(c<accounts){
          c++;
         System.out.println("Enter account number for account "+c);
        int number=in.nextInt();
        System.out.println("Enter balance for account "+c);
        double balance=in.nextDouble();
        int a=0;
        BankAccount numberbalance = new BankAccount(number,balance);
        accountinfo [a]=numberbalance;
        double test1;
        for (int i = 0; i < accountinfo.length; i++) {
            test1 = accountinfo[a].getBalance(); 
            System.out.println(test1);
        }
      }
    }
}

other class

    package Hw2;
     /**
     A bank account has a balance that can be changed by 
   deposits and withdrawals.
  */
  public class BankAccount
  {
    private double balance;
    private int accountNumber;

/**
   Constructs a bank account with a zero balance.
*/
public BankAccount(int _accountNumber)
{   
   balance = 0;
}

/**
   Constructs a bank account with a given balance.
   @param initialBalance the initial balance
*/
public BankAccount(int _accountNumber, double initialBalance)
{   
   accountNumber = _accountNumber;
   balance = initialBalance;
}

/**
   Deposits money into the bank account.
   @param amount the amount to deposit
*/
public void deposit(double amount)
{  
   double newBalance = balance + amount;
   balance = newBalance;
}

/**
   Withdraws money from the bank account.
   @param amount the amount to withdraw
*/
public void withdraw(double amount)
{   
   double newBalance = balance - amount;
   balance = newBalance;
}

/**
   Gets the current balance of the bank account.
   @return the current balance
*/
public double getBalance()
{   
   return balance;
}
}

/**
   Gets the account number of the bank account.
   @return the account number
*/
Ryan H
  • 55
  • 7
  • 1
    So . . . what have you tried that hasn't worked? – TangledUpInBlue Feb 05 '16 at 16:50
  • 1
    And in what way did it not work? – Patricia Shanahan Feb 05 '16 at 16:50
  • Im trying to get it to output the total and average of the balances entered. I am not sure how to go about doing this. I have tried making a new array and putting the balances in their but I could never get it working. – Ryan H Feb 05 '16 at 16:54
  • As a general tip, `double` is a terrible data-type to use for amounts of money. When you add and subtract `double` values, you'll get rounding errors - such is the nature of floating point arithmetic. If you're storing bank balances, a good data-type to use would be the `BigDecimal` class. – Dawood ibn Kareem Feb 05 '16 at 16:54

2 Answers2

1

Use the following for finding the total sum of balances

double sum=0;
for (int i = 0; i < accountinfo.length; i++) {
        test1 = accountinfo[i].getBalance(); 
        sum+=test1;
        System.out.println(test1);
    }

To print the sum and average

System.out.println("Total ::"+sum);
System.out.println("Average ::"+sum/accounts);
Pooja Arora
  • 574
  • 7
  • 19
  • The output i'm getting from the sum is just the last account balance entered. Its not adding the balances entered from previous inputs. – Ryan H Feb 05 '16 at 17:08
0

You need to know how is average calculated (at least). If you divide sum of all elements to the count of the elements you will get the average. My tip is to use one for loop and it will do the trick :)

FixXxeR
  • 169
  • 2
  • 2
  • 11