Search code examples
javaarraysjoptionpane

Array is not displaying (Java)


I have a problem with one method for my assignment. This is the requirement for this method:

"This method uses a loop to list all accounts contained in the Array, adding each account details to a String, before outputting to screen in the format specified in the screenshot below. Ensure that there are no out of bounds exceptions by checking if each array slot has an account object before adding its details to the output String. (arrayname[index] != null)"

This is my code for this method:

public void listAllAccounts()
{
    String allAccountsString = "List of all accounts: \n";

    for(int i = 0; i < ACCOUNT_SPACES; i++)
    {
        //allAccountsString += accountArray[numAccounts];
        if (accountArray[i] !=null)
        {
             allAccountsString += accountArray[i].toString() + "\n\n" ;
        }
    }
    JOptionPane.showMessageDialog(null, allAccountsString);

The problem is that the Message Dialog does not display the accounts I have already created. It just displays "List of all accounts: \n";

Any ideas?

This is the code for the whole class:

public class MyBankController
{
    /**
     * Variables that will be used by this class
     */

    private BankAccount newAccount;
    private BankAccount accountArray[];
    int numAccounts = 0;
    int ACCOUNT_SPACES = 2;
    private boolean accountStatus = false;

    /**
     * Constructor for objects of class MyBankController - to be left empty by requirements.
     */
    public MyBankController()
    {
        //
    }

    /**
     * A method to create a new account, accepting user input and allocating memory space. 
     */
    public void createAccount(String customerName, int accountNumber)
    {
        newAccount = new BankAccount(customerName, accountNumber);
        accountArray = new BankAccount [2];
        if(numAccounts +1 <= ACCOUNT_SPACES)
        {
            numAccounts++;
            printAccountDetails();
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Sorry, a maximum limit of accounts allowed has been reached." + "\n" + "Limit: " + numAccounts + "/10", "Warning!", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    /**
     * Method to print the account details - by calling an object from the BankAccount class.
     */

    private void printAccountDetails()
    {
        JOptionPane.showMessageDialog(null, newAccount.toString(), "Account Details", JOptionPane.INFORMATION_MESSAGE);
    }

    public void listAllAccounts()
    {
        String allAccountsString = "List of all accounts: \n";

        for(int i = 0; i < ACCOUNT_SPACES; i++)
        {
            if (accountArray[i] !=null)
            {
                allAccountsString += accountArray[i].toString() + "\n\n" ;

            }
            JOptionPane.showMessageDialog(null, allAccountsString);
        }
    }

    public void listAllOpenAccounts()
    {

        String allAccountsString = "List of all accounts: \n";

        for(int i = 0; i < ACCOUNT_SPACES; i++)
        {
            //allAccountsString += accountArray[numAccounts];
            if (accountArray[i] !=null && accountStatus == true)
            {
                allAccountsString += accountArray[i].toString() + "\n\n" ;``
            }
        }
        JOptionPane.showMessageDialog(null, allAccountsString);
    }
}

Solution

  • Thanks for your help. I used a variable called newAccount rather than the array itself.

    The solution is:

    /**
     * Importing JOptionPane for user GUI.
     */
    import javax.swing.JOptionPane;
    import javax.swing.*;
    /**
     * The MyBankController class will control the creation of accounts utilizing the BankAccount class you
    created in Part A of the assignment
     * 
     * @author Katarzyna Korzeniec 
     * @version 03/02/2015
     */
    public class MyBankController
    {
        /**
         * Variables that will be used by this class
         */
    
        //private BankAccount newAccount;
        private BankAccount accountArray[] = new BankAccount[2];
        int numAccounts = 0;
        int ACCOUNT_SPACES = 2;
         //boolean accountStatus = false;
    
        /**
         * Constructor for objects of class MyBankController - to be left empty by requirements.
         */
        public MyBankController()
        {
            //
        }
    
        /**
         * A method to create a new account, accepting user input and allocating memory space. 
         */
        public void createAccount(String customerName, int accountNumber)
        {
    
    
            if(numAccounts +1 <= ACCOUNT_SPACES)
            {
                accountArray[numAccounts] = new BankAccount(customerName, accountNumber);
    
                printAccountDetails(numAccounts);
                numAccounts++;
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Sorry, a maximum limit of accounts allowed has been reached." + "\n" + "Limit: " + numAccounts + "/10", "Warning!", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    
        /**
         * Method to print the account details - by calling an object from the BankAccount class.
         */
    
        private void printAccountDetails(int value)
        {
            JOptionPane.showMessageDialog(null, accountArray[value].toString(), "Account Details", JOptionPane.INFORMATION_MESSAGE);
        }
    
        public void listAllAccounts()
        {
            String allAccountsString = "List of all accounts: \n";
    
            for(int i = 0; i < ACCOUNT_SPACES; i++)
            {
                if (accountArray[i] !=null)
                {
                    allAccountsString += accountArray[i].toString() + "\n\n" ;
    
                }
    
            }
            JOptionPane.showMessageDialog(null, allAccountsString);
        }
    
        public void listAllOpenAccounts()
        {
    
            String allAccountsString = "List of all accounts: \n";
    
            for(int i = 0; i < ACCOUNT_SPACES; i++)
            {
                //allAccountsString += accountArray[numAccounts];
                if (accountArray[i] !=null && (accountArray[i].getAccountStatus() !=false))
                {
                    allAccountsString += accountArray[i].toString() + "\n\n" ;
                }
            }
            JOptionPane.showMessageDialog(null, allAccountsString);
        }
    }