ArrayList issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blt51
    New Member
    • Nov 2009
    • 3

    ArrayList issue

    I need to write a program that handles a bank account and does a number of transactions using an arraylist. However, I'm having trouble getting the arraylist to store all the transactions and then output them. There are 2 classes, Bank and BankAccount. I also have a tester. Any help would be appreciated.

    Bank
    Code:
    package bank;
    
    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 sum of the balances
       */
       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 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 a Bank Account with an initial balance
        */
       public void addAccount(int accountNumber, double initialBalance){
    	   accounts.add(new BankAccount(accountNumber, initialBalance));
       }
       /**
        * deposit method handles transactions that deposit money into bank account
        */
       public void deposit(int accountNumber, double amount){
    	   BankAccount anAccount = this.find(accountNumber);
    	   anAccount.deposit(amount);
       }
       /**
        * withdraw method handles transactions that withdraw money from the bank account
        */
       public void withdraw(int accountNumber, double amount){
    	   BankAccount anAccount=this.find(accountNumber);
    	   anAccount.withdraw(amount);
       }
       /**
        * @return returns bank account's balance
        */
       public double getBalance(int accountNumber){
    	   BankAccount anAccount = this.find(accountNumber);
    	   return anAccount.getBalance(); 		  
       }
       /**
        * method which suspends an account
        */
       public void suspendAccount(int accountNumber){
    	   BankAccount temp = this.find(accountNumber);
    	   temp.suspend();
       }
       /**
        * method that reopens an account
        */
       public void reOpenAccount(int accountNumber){
    	   BankAccount temp = this.find(accountNumber);
    	   temp.reOpen();
       }
       /**
        * method that closes an account
        */
       public void closeAccount(int accountNumber){
    	   BankAccount anAccount = this.find(accountNumber);
    	   anAccount.close();
       }
       /**
        * @return returns the current status of an account
        */
       public String getAccountStatus(int accountNumber){
    	   BankAccount anAccount = this.find(accountNumber);
    	   return anAccount.getStatus();
       }
       /**
        * @return returns the transactions from an account
        */
       public String retrieveAccountTransactions(int accountNumber){
    	   return find(accountNumber).retrieveTransactions();
       }
       /**
        * Printout string of account summary
        */
       public String summarizeAllAccounts(){
    	   String string1 = "Bank Account Summary\n\nAccount \tBalance \t#Transactions \t\t Status\n";
    	   String string2 = new String();
    	   String string3 = new String();
    	   for(BankAccount a : accounts)
    	   {
    		   string2 += a.getAccountNumber() + "\t\t " +  a.getBalance() + "\t\t   " + a.retrieveNumberOfTransactions() + "\t\t\t" + a.getStatus() + "\n";
    	   }
    	   string3 += "End of Account Summary";   
    	   return string1 + string2 + string3;
       }
    
       private ArrayList<BankAccount> accounts;
    }

    BankAccount

    Code:
    package bank;
    
    import java.util.ArrayList;
    
    /**
       A bank account has a balance that can be changed by 
       deposits and withdrawals.
    */
    public class BankAccount
    {
    
    	public static final String OPEN = "open";
    	public static final String SUSPENDED = "suspended";
    	public static final String CLOSED = "closed";
    	
    	private String status;
    	private int accountNumber;
    	private double balance;
    	private ArrayList<Double> transactions;
    	
       /**
          Constructs a bank account with a zero balance
          @param anAccountNumber the account number for this account
       */
       public BankAccount(int anAccountNumber)
       {   
    	  status = new String(); 
    	  transactions = new ArrayList<Double>();
    	  setStatus(OPEN);
          accountNumber = anAccountNumber;
          balance = 0;
          
       }
       
       /**
          Constructs a bank account with a given balance
          @param anAccountNumber the account number for this account
          @param initialBalance the initial balance
       */
       public BankAccount(int anAccountNumber, double initialBalance)
       {   
    	  status = new String(); 
    	  setStatus(OPEN); 
    	  transactions = new ArrayList<Double>();
          accountNumber = anAccountNumber; 
          deposit(initialBalance);
       }
    
       /**
          Gets the account number of this bank account.
          @return the account number
       */
       public int getAccountNumber()
       {   
          return accountNumber;
       }
    
       /**
          Deposits money into the bank account.
          @param amount the amount to deposit
       */
       public void deposit(double amount){    
    	   if (status.equals(OPEN) && amount > 0){ 
    			 double newBalance = balance + amount;
    			 balance = newBalance;
    			 addTransaction(amount);
    	   }
       }
    
       /**
          Withdraws money from the bank account.
          @param amount the amount to withdraw
       */
       public void withdraw(double amount)
       {   
    	   if (status.equals(OPEN) && amount > 0 && amount <= balance){ 
    			double newBalance = balance - amount;
    			balance = newBalance;
    			addTransaction(amount);
    	   }
       }
      /**
       * Suspends an account
       */
       public void suspend(){
    	   {
    		   if (!isSuspended() && !isClosed())
    		   	{
    			   setStatus(SUSPENDED);
    		   	}
    	   	}
       }
    /**
     * Closes an ccount
     */
       public void close(){
    	   if (isSuspended())
    	   {
    		   reOpen();
    		   withdraw(getBalance());
    		   setStatus(CLOSED);
    	   }
          else
          {
        	  withdraw(getBalance());
        	  setStatus(CLOSED);
          }
       }
       /**
        * reopens an account
        */
       public void reOpen(){
    	   if (isSuspended())
    	   	{
    		   setStatus(OPEN);
    	   	}
       }
       /**
        * @return returns whether or not the status is open
        */
       public boolean isOpen(){
    	   if (status.equals(OPEN)){
    		   return true;
    	   }
    	   else
    		   return false;
       }
       /**
        * @return returns whether or not the status is suspended
        */
       public boolean isSuspended(){
    	   if (status.equals(SUSPENDED)){
    		   return true;
    	   }
    	   else
    		   return false;
       }
       /**
        * @return returns whether or not the status is closed
        */
       public boolean isClosed(){
    	   if (status.equals(CLOSED)){
    		   return true;
    	   }
    	   else
    		   return false;
       }
      /**
       * adds a transaction
       */
       private void addTransaction(double amount){	   
    	   transactions.add(amount);	   
       }
       /**
        * @return returns the output message
        */
       public String retrieveTransactions(){
    	   String output = "Account #" + accountNumber + " transactions\n\n";
    	   int count = 1;
    	   
    	   for (double i : transactions){
    		   output += (count + ": " + i +"\n");
    		   count++;
    	   }
    	   output += "balance = "+ balance + "\nEnd of transactions\n";
    	   
    	   return output;
    
       }
       /**
        * @return returns the number of transactions
        */
       public int retrieveNumberOfTransactions(){
    	   return transactions.size();
       }
       /**
        * @return returns the status of account
        */
       private String setStatus(String newStatus){
    	   if (status.equals(OPEN))
    	   	{
    		   this.status = OPEN;
    	   	}
    	   if (status.equals(SUSPENDED))
    	   	{
    		   this.status = SUSPENDED;
    	   	}
    	   if (status.equals(CLOSED))
    	   	{
    		   this.status = CLOSED;
    	   	}
    	   return status;
       }
       /**
        * @return returns the status of account
        */
       public String getStatus(){
    	   return status;
       }
       
       /**
          Gets the current balance of the bank account.
          @return the current balance
       */
       public double getBalance()
       {   
          return balance;
       }
       
    }
    Tester

    Code:
    package bank;
    public class BankTester
    {
    	
    	public static void main(String[] args)
        {
    	  Bank firstBankOfJava = new Bank();
    	  firstBankOfJava.addAccount(new BankAccount(1001, 500.00));
    	  firstBankOfJava.deposit(1001, 300.00);
    	  firstBankOfJava.deposit(1001, 50.00);
    	  firstBankOfJava.withdraw(1001, 500.00);
    	  firstBankOfJava.closeAccount(1001);
    	  firstBankOfJava.addAccount(new BankAccount(1002, 2500.00));
    	  firstBankOfJava.deposit(1002, 1500.00);
    	  firstBankOfJava.withdraw(1002, 2000.00);
    	  firstBankOfJava.withdraw(1002, 500.00);
    	  firstBankOfJava.deposit(1002, 4000.00);
    	  firstBankOfJava.suspendAccount(1002);
    	  firstBankOfJava.addAccount(new BankAccount(1003,1000));
    	  firstBankOfJava.deposit(1003, 100);
    	  firstBankOfJava.withdraw(1003,250);
    	  firstBankOfJava.deposit(1003,750);
    	  //MasterTester will close account before this is reached
    	  //firstBankOfJava.withdraw(1003,1600);
    	  firstBankOfJava.closeAccount(1003);
    	  String transactionReport1001 = firstBankOfJava.retrieveAccountTransactions(1001);
    	  System.out.println(transactionReport1001);
    	  String transactionReport1002 = firstBankOfJava.retrieveAccountTransactions(1002);
    	  System.out.println(transactionReport1002);
    	  String transactionReport1003 = firstBankOfJava.retrieveAccountTransactions(1003);
    	  System.out.println(transactionReport1003);
    	  String summaryReport = firstBankOfJava.summarizeAllAccounts();
    	  System.out.println(summaryReport);
       }
    }
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Could you please explain what "problems" you are having storing and outputting the transactions?

    Comment

    • blt51
      New Member
      • Nov 2009
      • 3

      #3
      It's supposed to print a summary of transactions for each account but it doesn't. I think the status is a problem but I can't follow why it won't store and print the transactions

      Comment

      • mrjohn
        New Member
        • May 2009
        • 31

        #4
        I took a quick look at it with the debugger, and it looks as though you don't ever assign a value to status in BankAccount.jav a. I think this may be the problem.
        [code="java] private String setStatus(Strin g newStatus){
        if (status.equals( OPEN))
        {
        this.status = OPEN;
        }
        if (status.equals( SUSPENDED))
        {
        this.status = SUSPENDED;
        }
        if (status.equals( CLOSED))
        {
        this.status = CLOSED;
        }
        return status;
        }[/code]
        I think you meant to check newStatus in those if-statements, rather than status itself. Is this the problem?

        Comment

        • blt51
          New Member
          • Nov 2009
          • 3

          #5
          Yes actually it is. I meant to post that I had figured this out.

          Thanks for the help.

          Stupid mistake!

          Comment

          • pjerald
            New Member
            • Oct 2007
            • 77

            #6
            Code:
            private void setStatus(String newStatus){
                   this.status = newStatus;
                   /*
                   if (status.equals(OPEN))
                       {
                       this.status = OPEN;
                       }
                   if (status.equals(SUSPENDED))
                       {
                       this.status = SUSPENDED;
                       }
                   if (status.equals(CLOSED))
                       {
                       this.status = CLOSED;
                       }
                    */
               }
            Change this and it should work. you are not setting the status of the BankAccount. Also i change the method to void as no need to return the status.

            Comment

            Working...