i need help on :
 we sum the balances of all of the Accounts ??
 print the total sum.
     print the owners of all the Accounts that have balances < 100 ?
Danny is generous – and wants to deposit 72 into all accounts with balances < 100
How many accounts have balances < 200 now ?
○ First print the account information out ..
○ How count how many
frank won Super Power Ball
replace her account with her new name  Madame Mona, with the balance of 4377
harry is leaving town – delete his account

public class Account {

private int balance;        //current balance 
private String owner;       // Account owner


public Account(String owner, int balance) {
    this.owner = owner;
    this.balance = balance;

}

public void deposit(int amt) {
    balance += amt;
}

public void withdraw( int amt) {
    balance -= amt;
}


public int getBalance () {
    return balance;
}


public String getOwner () {
    return owner;
}


public String toString () {
    return "Account :: Owner: " + owner + " Balance: " + balance ;
}


import java.util.ArrayList;

 public class ArrayCC {


//have to have globasl scope for the gBank
//declaree the array
static ArrayList<Account> gBank;


public static void main(String[] args) {

   gBank = new ArrayList<Account>(); //accessible as a paraneter 

    gBank.add(new Account("liz", 344));   \

    Account acct;   
    acct = new Account("tom", 76);   

    gBank.add(acct);      
    gBank.add(new Account("harry", 155));
    gBank.add(new Account("blake", 8));

    //how would you print all the objects? use advanced for loop
    for (Account i : gBank) {
        System.out.println(i);
    }



    gBank.add(2, new Account("Danny", 787));

        System.out.println("\nAt a club");
        for (Account i : gBank) {
        System.out.print(i);
    }

        gBank.set(2, new Account("Danny", 787));

        for (Account i : gBank) {
        System.out.println(i);
    }      


        gBank.get(getIndex("danny")).deposit(45);
        gBank.get(2).deposit(45);

Hint:

for (Account acc : gBank) {...

this loops through all the accounts, setting acc to refer to each account in turn
so in the loop you can use acc with all the public methods of the Account class, eg

for (Account acc : gBank) {
   if(acc.getOwner().equals(me)) acc.deposit(lotsOfMoney);
}
import java.util.ArrayList;

/* i need help on :
we sum the balances of all of the Accounts ??
print the total sum.
    print the owners of all the Accounts that have balances < 100 ?
Danny is generous – and wants to deposit 72 into all accounts with balances < 100
How many accounts have balances < 200 now ?
○ First print the account information out ..
○ How count how many
frank won Super Power Ball
replace her account with her new name  Madame Mona, with the balance of 4377
harry is leaving town – delete his account
 */
public class ArrayCC {

  // have to have globasl scope for the gBank
  // declaree the array
  static ArrayList<Account> gBank;

  private static int getIndex(String owner) {
    for (int i = 0; i < gBank.size(); i++) {
      if (gBank.get(i).getOwner() == owner)
        return i;
    }
    return -1;
  }

  public static void main(String[] args) {

    gBank = new ArrayList<Account>(); // accessible as a paraneter

    gBank.add(new Account("liz", 344));

    Account acct;
    acct = new Account("tom", 76);

    gBank.add(acct);
    gBank.add(new Account("harry", 155));
    gBank.add(new Account("blake", 8));

    // how would you print all the objects? use advanced for loop
    for (Account i : gBank) {
      System.out.println(i);
    }

    gBank.add(2, new Account("Danny", 787));

    System.out.println("\nAt a club");
    for (Account i : gBank) {
      System.out.print(i);
    }
    System.out.println("\n");

    gBank.set(2, new Account("Danny", 787));

    for (Account i : gBank) {
      System.out.println(i);
    }
    System.out.println("\n");

    gBank.get(getIndex("Danny")).deposit(45);
    gBank.get(2).deposit(45);

    System.out.println("Accounts before homework: ");
    for (Account i : gBank) {
      System.out.println(i);
    }

    // homework-1 we sum the balances of all of the Accounts, print the total sum.
    int sum = 0;
    for (Account a : gBank)
      sum += a.getBalance();

    System.out.println("Total Balances sum: " + sum);

    // homework-2 print the owners of all the Accounts that have balances < 100
    String owners = "";
    for (Account a : gBank)
      if (a.getBalance() < 100)
        owners += a.getOwner() + ", ";
    if (owners != "")
      owners = owners.substring(0, owners.length() - 2);

    System.out.println("Owners with Balance<100: " + owners);

    // homework-3 Danny is generous – and wants to deposit 72 into all accounts with
    // balances < 100
    int i = getIndex("Danny");
    for (Account a : gBank)
      if (a.getBalance() < 100) {
        gBank.get(i).withdraw(72);
        a.deposit(72);
      }

    System.out.println("Accounts after Danny gave 72 to all with Balance<100:");
    for (Account a : gBank) {
      System.out.println(a);
    }

    // homework-4 How many accounts have balances < 200 now
    int c = 0;
    System.out.println("Accounts with Balance<200: ");
    for (Account a : gBank)
      if (a.getBalance() < 200) {
        System.out.println(a);
        c++;
      }

    System.out.println("Count of Accounts with Balance<200: " + c);

    // homework-5 frank won Super Power Ball, replace her account with her new name Madame Mona, with the balance of 4377
    // but "frank" doesn't exist in given code, so I'll use "liz"
    i = getIndex("liz"); // i = getIndex("frank");
    gBank.set(i, new Account("Madame Mona", 4377));

    System.out.println("Accounts after account of liz renamed with given balance:");
    for (Account a : gBank) {
      System.out.println(a);
    }

    // homework-6 harry is leaving town – delete his account
    i = getIndex("harry");
    gBank.remove(i);

    System.out.println("Accounts after deleting harry's account:");
    for (Account a : gBank) {
      System.out.println(a);
    }

  }
}

... and what is your question?

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.