Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

ArrayList/Loops - Java Help

loop array

  • Please log in to reply
1 reply to this topic

#1 AllegraC

AllegraC

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 09 April 2008 - 02:26 PM

I need help with my codes. I'm creating a bank to store bank accounts. I've got this all up and running but the final requirements are to be able to search for an account using names then list all relevent account details using the accountDetails() method in BankAccount. And list all accounts found between two given account numbers.

I'm new to Java - I'm not particularly quick at it =S alot of the time I am doing things and not fully aware of what it actually does. Any help would be greatly appreciated it! (The last method in my bank class was my weak attempt at the list account details using by name) ...



BANK CLASS:
import java.util.ArrayList;

public class Bank
{
    ArrayList<BankAccount> accounts;
    private String bankName;
       
    public Bank(String enterBankName)
    {  
        bankName = enterBankName;
        accounts = new ArrayList<BankAccount>();
    }
    
    public String getBankName()
    {
        return bankName;
    }
        
      public void addAccount(BankAccount account) 
   { 
       accounts.add(account);
   } 
       
    public int numberOfAccounts()
    {
        return accounts.size();
    }
       
    public void removeAccount(BankAccount account) 
    { 
        accounts.remove(account); 
    } 
   
    public void listAccountDetails()
    {
        for(BankAccount account : accounts) {
            account.accountDetails();
        }
    }
        
    public void removeAccountByNumber(int accountNumber)
    {
       BankAccount removeAccount = null;
       for(BankAccount account : accounts) {
       if (account.getAccountNumber() == (accountNumber)) 
    {
        removeAccount = account;
    }
}
        accounts.remove(removeAccount); 
    }
    
[COLOR="Red"]    public void returnAccountsByName(String accountName)
    {
        BankAccount listAccountByName = null;
        for (BankAccount account : accounts) {
        if (account.getAccountName() == (accountName))
        {
            listAccountByName = account; 
        }
}
            accounts.get(accountDetails());
}
    [/COLOR]
    
}
BANK ACCOUNT CLASS:
import java.util.ArrayList;

public class BankAccount
{
    private int accountNumber;
    private String accountName;
    private String accountType;
    private int accountBalance;
    private int size;
    ArrayList<BankAccount> accounts;


    public BankAccount(int enterAccountNumber, String enterAccountName, String enterAccountType, int enterAccountBalance)
    {
     accountNumber = enterAccountNumber;
     accountName = enterAccountName;
     accountType = enterAccountType;
     accountBalance = enterAccountBalance;
    }

public int getAccountNumber()
{
    return accountNumber;
}

public String getAccountName()
{
    return accountName;
}

public String getAccountType()
{
    return accountType;
}

public int getAccountBalance()
{
    return accountBalance;
}


    public void accountDetails()
    {
        System.out.println("Account Number is " + accountNumber);
        System.out.println("Account Name is " + accountName);
    
        {if (accountType.equals("savings"))
            System.out.println("Account type is Savings");
        else System.out.println("Account type is Cheque");
        
        System.out.println("Current balance is $" + accountBalance);
}}

    
    public void deposit(int amount) 
    {if (amount > 0)
         accountBalance = accountBalance + amount;
     else System.out.println("Illegal Transaction");
    }
    

    public void withdraw(int amount)
    {if (accountBalance > amount)
        accountBalance = accountBalance - amount;
    else System.out.println("Insufficient Funds");
    }

    
    public void changeAccountName (String newAccountName)
    {if (size > 5)
        newAccountName = accountName;
        int size = accountName.length();
        System.out.println("Name Must Have 5 Or More Characters");

       }
    }

Edited by v0id, 09 April 2008 - 08:51 PM.
Remember the code-tags!

  • 0

#2 keller

keller

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 09 April 2008 - 03:17 PM

I am not sure I understand what you are trying to do by looking at your code and reading your description. Could you try to elaborate a little bit more?
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download