Connecting Tech Pros Worldwide Forums | Help | Site Map

ArrayList issue

Newbie
 
Join Date: Nov 2009
Posts: 3
#1: 3 Weeks Ago
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
Expand|Select|Wrap|Line Numbers
  1. package bank;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6.    This bank contains a collection of bank accounts.
  7. */
  8. public class Bank
  9. {   
  10.    /**
  11.       Constructs a bank with no bank accounts.
  12.    */
  13.    public Bank()
  14.    {
  15.       accounts = new ArrayList<BankAccount>();
  16.    }
  17.  
  18.    /**
  19.       Adds an account to this bank.
  20.       @param a the account to add
  21.    */
  22.    public void addAccount(BankAccount a)
  23.    {
  24.       accounts.add(a);
  25.    }
  26.  
  27.    /**
  28.       Gets the sum of the balances of all accounts in this bank.
  29.       @return the sum of the balances
  30.    */
  31.    public double getTotalBalance()
  32.    {
  33.       double total = 0;
  34.       for (BankAccount a : accounts)
  35.       {
  36.          total = total + a.getBalance();
  37.       }
  38.       return total;
  39.    }
  40.  
  41.    /**
  42.       Counts the number of bank accounts whose balance is at
  43.       least a given value.
  44.       @param atLeast the balance required to count an account
  45.       @return the number of accounts having least the given balance
  46.    */
  47.    public int count(double atLeast)
  48.    {
  49.       int matches = 0;
  50.       for (BankAccount a : accounts)
  51.       {
  52.          if (a.getBalance() >= atLeast) matches++; // Found a match
  53.       }
  54.       return matches;
  55.    }
  56.  
  57.    /**
  58.       Finds a bank account with a given number.
  59.       @param accountNumber the number to find
  60.       @return the account with the given number, or null if there
  61.       is no such account
  62.    */
  63.    public BankAccount find(int accountNumber)
  64.    {
  65.       for (BankAccount a : accounts)
  66.       {
  67.          if (a.getAccountNumber() == accountNumber) // Found a match
  68.             return a;
  69.       } 
  70.       return null; // No match in the entire array list
  71.    }
  72.  
  73.    /**
  74.       Gets the bank account with the largest balance.
  75.       @return the account with the largest balance, or null if the
  76.       bank has no accounts
  77.    */
  78.    public BankAccount getMaximum()
  79.    {
  80.       if (accounts.size() == 0) return null;
  81.       BankAccount largestYet = accounts.get(0);
  82.       for (int i = 1; i < accounts.size(); i++) 
  83.       {
  84.          BankAccount a = accounts.get(i);
  85.          if (a.getBalance() > largestYet.getBalance())
  86.             largestYet = a;
  87.       }
  88.       return largestYet;
  89.    }
  90.  
  91.    /**
  92.     * adds a Bank Account with an initial balance
  93.     */
  94.    public void addAccount(int accountNumber, double initialBalance){
  95.        accounts.add(new BankAccount(accountNumber, initialBalance));
  96.    }
  97.    /**
  98.     * deposit method handles transactions that deposit money into bank account
  99.     */
  100.    public void deposit(int accountNumber, double amount){
  101.        BankAccount anAccount = this.find(accountNumber);
  102.        anAccount.deposit(amount);
  103.    }
  104.    /**
  105.     * withdraw method handles transactions that withdraw money from the bank account
  106.     */
  107.    public void withdraw(int accountNumber, double amount){
  108.        BankAccount anAccount=this.find(accountNumber);
  109.        anAccount.withdraw(amount);
  110.    }
  111.    /**
  112.     * @return returns bank account's balance
  113.     */
  114.    public double getBalance(int accountNumber){
  115.        BankAccount anAccount = this.find(accountNumber);
  116.        return anAccount.getBalance();           
  117.    }
  118.    /**
  119.     * method which suspends an account
  120.     */
  121.    public void suspendAccount(int accountNumber){
  122.        BankAccount temp = this.find(accountNumber);
  123.        temp.suspend();
  124.    }
  125.    /**
  126.     * method that reopens an account
  127.     */
  128.    public void reOpenAccount(int accountNumber){
  129.        BankAccount temp = this.find(accountNumber);
  130.        temp.reOpen();
  131.    }
  132.    /**
  133.     * method that closes an account
  134.     */
  135.    public void closeAccount(int accountNumber){
  136.        BankAccount anAccount = this.find(accountNumber);
  137.        anAccount.close();
  138.    }
  139.    /**
  140.     * @return returns the current status of an account
  141.     */
  142.    public String getAccountStatus(int accountNumber){
  143.        BankAccount anAccount = this.find(accountNumber);
  144.        return anAccount.getStatus();
  145.    }
  146.    /**
  147.     * @return returns the transactions from an account
  148.     */
  149.    public String retrieveAccountTransactions(int accountNumber){
  150.        return find(accountNumber).retrieveTransactions();
  151.    }
  152.    /**
  153.     * Printout string of account summary
  154.     */
  155.    public String summarizeAllAccounts(){
  156.        String string1 = "Bank Account Summary\n\nAccount \tBalance \t#Transactions \t\t Status\n";
  157.        String string2 = new String();
  158.        String string3 = new String();
  159.        for(BankAccount a : accounts)
  160.        {
  161.            string2 += a.getAccountNumber() + "\t\t " +  a.getBalance() + "\t\t   " + a.retrieveNumberOfTransactions() + "\t\t\t" + a.getStatus() + "\n";
  162.        }
  163.        string3 += "End of Account Summary";   
  164.        return string1 + string2 + string3;
  165.    }
  166.  
  167.    private ArrayList<BankAccount> accounts;
  168. }
  169.  

BankAccount

Expand|Select|Wrap|Line Numbers
  1. package bank;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6.    A bank account has a balance that can be changed by 
  7.    deposits and withdrawals.
  8. */
  9. public class BankAccount
  10. {
  11.  
  12.     public static final String OPEN = "open";
  13.     public static final String SUSPENDED = "suspended";
  14.     public static final String CLOSED = "closed";
  15.  
  16.     private String status;
  17.     private int accountNumber;
  18.     private double balance;
  19.     private ArrayList<Double> transactions;
  20.  
  21.    /**
  22.       Constructs a bank account with a zero balance
  23.       @param anAccountNumber the account number for this account
  24.    */
  25.    public BankAccount(int anAccountNumber)
  26.    {   
  27.       status = new String(); 
  28.       transactions = new ArrayList<Double>();
  29.       setStatus(OPEN);
  30.       accountNumber = anAccountNumber;
  31.       balance = 0;
  32.  
  33.    }
  34.  
  35.    /**
  36.       Constructs a bank account with a given balance
  37.       @param anAccountNumber the account number for this account
  38.       @param initialBalance the initial balance
  39.    */
  40.    public BankAccount(int anAccountNumber, double initialBalance)
  41.    {   
  42.       status = new String(); 
  43.       setStatus(OPEN); 
  44.       transactions = new ArrayList<Double>();
  45.       accountNumber = anAccountNumber; 
  46.       deposit(initialBalance);
  47.    }
  48.  
  49.    /**
  50.       Gets the account number of this bank account.
  51.       @return the account number
  52.    */
  53.    public int getAccountNumber()
  54.    {   
  55.       return accountNumber;
  56.    }
  57.  
  58.    /**
  59.       Deposits money into the bank account.
  60.       @param amount the amount to deposit
  61.    */
  62.    public void deposit(double amount){    
  63.        if (status.equals(OPEN) && amount > 0){ 
  64.              double newBalance = balance + amount;
  65.              balance = newBalance;
  66.              addTransaction(amount);
  67.        }
  68.    }
  69.  
  70.    /**
  71.       Withdraws money from the bank account.
  72.       @param amount the amount to withdraw
  73.    */
  74.    public void withdraw(double amount)
  75.    {   
  76.        if (status.equals(OPEN) && amount > 0 && amount <= balance){ 
  77.             double newBalance = balance - amount;
  78.             balance = newBalance;
  79.             addTransaction(amount);
  80.        }
  81.    }
  82.   /**
  83.    * Suspends an account
  84.    */
  85.    public void suspend(){
  86.        {
  87.            if (!isSuspended() && !isClosed())
  88.                {
  89.                setStatus(SUSPENDED);
  90.                }
  91.            }
  92.    }
  93. /**
  94.  * Closes an ccount
  95.  */
  96.    public void close(){
  97.        if (isSuspended())
  98.        {
  99.            reOpen();
  100.            withdraw(getBalance());
  101.            setStatus(CLOSED);
  102.        }
  103.       else
  104.       {
  105.           withdraw(getBalance());
  106.           setStatus(CLOSED);
  107.       }
  108.    }
  109.    /**
  110.     * reopens an account
  111.     */
  112.    public void reOpen(){
  113.        if (isSuspended())
  114.            {
  115.            setStatus(OPEN);
  116.            }
  117.    }
  118.    /**
  119.     * @return returns whether or not the status is open
  120.     */
  121.    public boolean isOpen(){
  122.        if (status.equals(OPEN)){
  123.            return true;
  124.        }
  125.        else
  126.            return false;
  127.    }
  128.    /**
  129.     * @return returns whether or not the status is suspended
  130.     */
  131.    public boolean isSuspended(){
  132.        if (status.equals(SUSPENDED)){
  133.            return true;
  134.        }
  135.        else
  136.            return false;
  137.    }
  138.    /**
  139.     * @return returns whether or not the status is closed
  140.     */
  141.    public boolean isClosed(){
  142.        if (status.equals(CLOSED)){
  143.            return true;
  144.        }
  145.        else
  146.            return false;
  147.    }
  148.   /**
  149.    * adds a transaction
  150.    */
  151.    private void addTransaction(double amount){       
  152.        transactions.add(amount);       
  153.    }
  154.    /**
  155.     * @return returns the output message
  156.     */
  157.    public String retrieveTransactions(){
  158.        String output = "Account #" + accountNumber + " transactions\n\n";
  159.        int count = 1;
  160.  
  161.        for (double i : transactions){
  162.            output += (count + ": " + i +"\n");
  163.            count++;
  164.        }
  165.        output += "balance = "+ balance + "\nEnd of transactions\n";
  166.  
  167.        return output;
  168.  
  169.    }
  170.    /**
  171.     * @return returns the number of transactions
  172.     */
  173.    public int retrieveNumberOfTransactions(){
  174.        return transactions.size();
  175.    }
  176.    /**
  177.     * @return returns the status of account
  178.     */
  179.    private String setStatus(String newStatus){
  180.        if (status.equals(OPEN))
  181.            {
  182.            this.status = OPEN;
  183.            }
  184.        if (status.equals(SUSPENDED))
  185.            {
  186.            this.status = SUSPENDED;
  187.            }
  188.        if (status.equals(CLOSED))
  189.            {
  190.            this.status = CLOSED;
  191.            }
  192.        return status;
  193.    }
  194.    /**
  195.     * @return returns the status of account
  196.     */
  197.    public String getStatus(){
  198.        return status;
  199.    }
  200.  
  201.    /**
  202.       Gets the current balance of the bank account.
  203.       @return the current balance
  204.    */
  205.    public double getBalance()
  206.    {   
  207.       return balance;
  208.    }
  209.  
  210. }
  211.  
Tester

Expand|Select|Wrap|Line Numbers
  1. package bank;
  2. public class BankTester
  3. {
  4.  
  5.     public static void main(String[] args)
  6.     {
  7.       Bank firstBankOfJava = new Bank();
  8.       firstBankOfJava.addAccount(new BankAccount(1001, 500.00));
  9.       firstBankOfJava.deposit(1001, 300.00);
  10.       firstBankOfJava.deposit(1001, 50.00);
  11.       firstBankOfJava.withdraw(1001, 500.00);
  12.       firstBankOfJava.closeAccount(1001);
  13.       firstBankOfJava.addAccount(new BankAccount(1002, 2500.00));
  14.       firstBankOfJava.deposit(1002, 1500.00);
  15.       firstBankOfJava.withdraw(1002, 2000.00);
  16.       firstBankOfJava.withdraw(1002, 500.00);
  17.       firstBankOfJava.deposit(1002, 4000.00);
  18.       firstBankOfJava.suspendAccount(1002);
  19.       firstBankOfJava.addAccount(new BankAccount(1003,1000));
  20.       firstBankOfJava.deposit(1003, 100);
  21.       firstBankOfJava.withdraw(1003,250);
  22.       firstBankOfJava.deposit(1003,750);
  23.       //MasterTester will close account before this is reached
  24.       //firstBankOfJava.withdraw(1003,1600);
  25.       firstBankOfJava.closeAccount(1003);
  26.       String transactionReport1001 = firstBankOfJava.retrieveAccountTransactions(1001);
  27.       System.out.println(transactionReport1001);
  28.       String transactionReport1002 = firstBankOfJava.retrieveAccountTransactions(1002);
  29.       System.out.println(transactionReport1002);
  30.       String transactionReport1003 = firstBankOfJava.retrieveAccountTransactions(1003);
  31.       System.out.println(transactionReport1003);
  32.       String summaryReport = firstBankOfJava.summarizeAllAccounts();
  33.       System.out.println(summaryReport);
  34.    }
  35. }
  36.  

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#2: 3 Weeks Ago

re: ArrayList issue


Could you please explain what "problems" you are having storing and outputting the transactions?
Newbie
 
Join Date: Nov 2009
Posts: 3
#3: 3 Weeks Ago

re: ArrayList issue


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
Newbie
 
Join Date: May 2009
Posts: 26
#4: 3 Weeks Ago

re: ArrayList issue


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.java. I think this may be the problem.
Expand|Select|Wrap|Line Numbers
  1.    private String setStatus(String newStatus){
  2.        if (status.equals(OPEN))
  3.            {
  4.            this.status = OPEN;
  5.            }
  6.        if (status.equals(SUSPENDED))
  7.            {
  8.            this.status = SUSPENDED;
  9.            }
  10.        if (status.equals(CLOSED))
  11.            {
  12.            this.status = CLOSED;
  13.            }
  14.        return status;
  15.    }
I think you meant to check newStatus in those if-statements, rather than status itself. Is this the problem?
Newbie
 
Join Date: Nov 2009
Posts: 3
#5: 3 Weeks Ago

re: ArrayList issue


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

Thanks for the help.

Stupid mistake!
Member
 
Join Date: Oct 2007
Location: Velacheri in Chennai(INDIA)
Posts: 77
#6: 3 Weeks Ago

re: ArrayList issue


Expand|Select|Wrap|Line Numbers
  1. private void setStatus(String newStatus){
  2.        this.status = newStatus;
  3.        /*
  4.        if (status.equals(OPEN))
  5.            {
  6.            this.status = OPEN;
  7.            }
  8.        if (status.equals(SUSPENDED))
  9.            {
  10.            this.status = SUSPENDED;
  11.            }
  12.        if (status.equals(CLOSED))
  13.            {
  14.            this.status = CLOSED;
  15.            }
  16.         */
  17.    }
  18.  
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.
Reply


Similar Java bytes