473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

ArrayList issue

3
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.  
Nov 4 '09 #1
5 2991
Frinavale
9,735 Expert Mod 8TB
Could you please explain what "problems" you are having storing and outputting the transactions?
Nov 4 '09 #2
blt51
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
Nov 4 '09 #3
mrjohn
32
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?
Nov 5 '09 #4
blt51
3
Yes actually it is. I meant to post that I had figured this out.

Thanks for the help.

Stupid mistake!
Nov 5 '09 #5
pjerald
77
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.
Nov 5 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
10
by: Eric | last post by:
I'm looking at this page in the MSDN right here: ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemcollectionsarraylist classsynchronizedtopic2.htm (or online here:...
7
by: Dave | last post by:
Hi all, I have... using System.Collections; namespace MyApp.Templates { public class MyPage : System.Web.UI.Page { ArrayList MyArray = new ArrayList();
1
by: sd | last post by:
QUESTION: How can my ASP page - which uses language="VBScript" - pass a "System.Collections.ArrayList" object - as a parameter - to a C# method in the middle-tier ??? ----- Is it possible at...
4
by: Peter | last post by:
I run into this situation all the time and I'm wondering what is the most efficient way to handle this issue: I'll be pulling data out of a data source and want to load the data into an array so...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
6
by: Vinit | last post by:
Hi I am passing an arraylist to a c#/.net webmethod from a perl client using soap:lite. The trace shows the elements in the xml request. The arraylist input in the webmethod however does not...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
1
tifoso
by: tifoso | last post by:
I searched here and only found somebody with a round-robin arraylist issue here but what they suggest I tried already and it doesn't work for me I hope somebody can shed some light Scenario: I'm...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.