adding methods to Bank Class with ArrayList

adding the following methods: addAccount, deposit, withdraw, getBalanc

Page 1 of 1

3 Replies - 7811 Views - Last Post: 29 September 2009 - 05:35 PM Rate Topic: -----

#1 bobsaget  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 27-September 09

adding methods to Bank Class with ArrayList

Post icon  Posted 28 September 2009 - 08:15 PM

I am given a Bank account class that uses an ArrayList and i have to add the following methods:
public void addAccount(int accountNumber, double initialBalance)
public void deposit(int accountNumber, double amount)
public void withdraw(int accountNumber, double amount)
public double getBalance(int accountNumber)



I have to use the this Bank class in my solution
import java.util.ArrayList;

/**
   This bank contains a collection of bank accounts.
*/
public class Bank
{
   /**
	  Constructs a bank with no bank accounts.
   */
   public Bank()
   {
	  accounts = new ArrayList<BankAccount>();
   }
   
   /**
	  Adds an account to this bank.
	  @param a the account to add
   */
   public void addAccount(BankAccount a)
   {
	  accounts.add(a);
   }
   
   /**
	  Gets the sum of the balances of all accounts in this bank.
	  @return the total balance
   */
   public double getTotalBalance()
   {
	  double total = 0;
	  for (BankAccount a : accounts)
	  {
		 total = total + a.getBalance();
	  }
	  return total;
   }
   
   /**
	  Counts the number of bank accounts whose balance is at
	  least a given value.
	  @param atLeast the balance required to count an account
	  @return the number of accounts having at least the given balance
   */
   public int count(double atLeast)
   {
	  int matches = 0;
	  for (BankAccount a : accounts)
	  {
		 if (a.getBalance() >= atLeast) matches++; // found a match
	  }
	  return matches;
   }

   /**
	  Finds a bank account with a given number.
	  @param accountNumber the number to find
	  @return the account with the given number, or null if there
	  is no such account
   */
   public BankAccount find(int accountNumber)
   {
	  for (BankAccount a : accounts)
	  {
		 if (a.getAccountNumber() == accountNumber) // found a match
			return a;
	  }
	  return null; // no match in the entire array list
   }
   
   /**
	  Gets the bank account with the largest balance.
	  @return the account with the largest balance, or null if the
	  bank has no accounts
   */
   public BankAccount getMaximum()
   {
	  if (accounts.size() == 0) return null;
	  BankAccount largestYet = accounts.get(0);
	  
	  for (int i = 1; i < accounts.size(); i++)
	  {
		 BankAccount a = accounts.get(i);
		 if (a.getBalance() > largestYet.getBalance())
			largestYet = a;
	  }
	  return largestYet;
   }
   
   /**
	  Adds an account to the bank.
	  @param accountNumber the account number of this account
	  @param initialBalance the initial balance of this account
   */
   public void addAccount(int accountNumber, double initialBalance)
   {
	  . . .
   }
   
   /**
	  Deposits money into an account
	  @param accountNumber the account number
	  @param amount the amount to be deposited
   */
   public void deposit(int accountNumber, double amount)
   {
	  . . .
   }
   
   /**
	  Withdraws money from an account.
	  @param accountNumber the account number
	  @param amount the amount to be withrawn
   */
   public void withdraw(int accountNumber, double amount)
   {
	  . . .
   }
   
   /**
	  Gets an account balance
	  @param accountNumber the account number
	  @return the account balance
   */
   public double getBalance(int accountNumber)
   {
	  . . .
   }
   
   private ArrayList<BankAccount> accounts;
}



this is the tester program for the class


/**
   This program tests the Bank class.
*/
public class BankTester
{
   public static void main(String[] args)
   {
	  Bank bank = new Bank();

	  int dannysAccount = 0;
	  int sallysAccount = 1;
	  int harrysAccount = 2;
	  int jerrysAccount = 3;
	  
	  bank.addAccount(dannysAccount, 1000);
	  bank.addAccount(sallysAccount, 2000);
	  bank.addAccount(harrysAccount, 3000);
	  bank.addAccount(jerrysAccount, 10000);

	  bank.deposit(dannysAccount, 200);
	  bank.withdraw(sallysAccount, 500);
	  bank.deposit(harrysAccount, 1000);
	  bank.withdraw(jerrysAccount, 7000);

	  System.out.println(
		 "Danny's Account Balance: " + bank.getBalance(dannysAccount));
	  System.out.println("Expected: 1200");
	  System.out.println(
		 "Sally's Account Balance: " + bank.getBalance(sallysAccount));
	  System.out.println("Expected: 1500");
	  System.out.println(
		 "Harry's Account Balance: " + bank.getBalance(harrysAccount));
	  System.out.println("Expected: 4000");
	  System.out.println(
		 "Jerry's Account Balance: " + bank.getBalance(jerrysAccount));
	  System.out.println("Expected: 3000");
   }
}



I am confused on how to create the methods using an ArrayList, i am not too familiar with ArrayLists and just know the very very basics. Especially when typing in the class without even modifying it there are already errors because the ArrayList is creating an ArrayList of BankAccounts and "BankAccount cannot be resolved to a type"
I appreciate any help
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: adding methods to Bank Class with ArrayList

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: adding methods to Bank Class with ArrayList

Posted 28 September 2009 - 08:28 PM

You just posted your assigment
You posted your teacher's code but you haven't written any code yet

[rules][/rules]
Was This Post Helpful? 0
  • +
  • -

#3 bobsaget  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 27-September 09

Re: adding methods to Bank Class with ArrayList

Posted 29 September 2009 - 05:24 PM

I am aware of the policies but that is the thing, i don't even know where to get started because it is already giving me errors with the ArrayList before even starting so i am not sure how to work with that because i am not good with ArrayLists
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: adding methods to Bank Class with ArrayList

Posted 29 September 2009 - 05:35 PM

View Postbobsaget, on 29 Sep, 2009 - 04:24 PM, said:

I am aware of the policies but that is the thing, i don't even know where to get started because it is already giving me errors with the ArrayList before even starting so i am not sure how to work with that because i am not good with ArrayLists

so try to replace the

.....

written by your teacher and then we will try to help you

Here is an ArrayList tutorial

http://www.java2s.co...__ArrayList.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1