Accessing methods of an object inside in ArrayList
I have created an arraylist in a class called bank below is a method i created in the bank class which is used to apply interest to all accounts in the array list by calling the method in the account class for every account in the arraylist but I am having trouble in getting the arraylist to apply the method to the accounts
Code java:
public void Interest()
{
// bank is the name of the arraylist and .addInterest() is a method in the account class
for (int i = 0; i< count; i++)
bank.get(i).addInterest();
}
I have not dealt with arraylists very much so I am not too familiar with them, i assume that my syntax is incorrect because when i try to compile that result says "can't find symbol variable addInterest(). I can post more of the bank class or account class if needed. I appreciate any help or suggestions.
Re: Accessing methods of an object inside in ArrayList
Quote:
I can post more of the bank class or account class if needed.
Yes this is needed. A 'cannot find symbol' means that the method/variable the error refers to does not exist. Make sure the method is defined in your class you are trying to call it on.
Re: Accessing methods of an object inside in ArrayList
here is the Account Class
Code java:
import java.text.NumberFormat;
public class Account
{
private final double RATE = 0.03; // interest rate of 3.0%
private long acctNumber;
private double balance;
private String name;
private String phoneNumber;
//-----------------------------------------------
//Creates a template account
//-----------------------------------------------
public Account ()
{
name = "";
acctNumber = 0000;
phoneNumber = "555-1111";
balance = 0.0;
}
//---------------------------------------------------------------------------------------------
// Creates account by defining the owner, account number, phone number, and starting balance
//---------------------------------------------------------------------------------------------
public Account (String owner, long account, String phone, double initial)
{
name = owner;
acctNumber = account;
phoneNumber = phone;
balance = initial;
}
//-----------------------------------------------------------------------
// Returns balance
//-----------------------------------------------------------------------
public double getBalance()
{
return balance;
}
//----------------------------------------------------------------------
// Returns account number
//----------------------------------------------------------------------
public long getAcctNumber()
{
return acctNumber;
}
//--------------------------------------------------
// deposits money into the account
//--------------------------------------------------
public double accDeposit (double amount)
{
balance = balance + amount;
return balance;
}
//-------------------------------------------------
//withdraws money from the account
//-------------------------------------------------
public double accWithdraw (double amount)
{
balance = balance - amount;
return balance;
}
//------------------------------------------------
//adds interest to the account
//------------------------------------------------
public double addInterest()
{
balance += (balance * RATE);
return balance;
}
//-------------------------------------------------
//Prints off the information for the account
//-------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return name + "\t" + acctNumber + "\t" + phoneNumber + "\t" + fmt.format(balance);
}
}
and here is the full bank class ( keep in mind I am still working on this file so there are a couple errors in it right now, but i am mainly focusing on getting the Interest() method working right now)
Code java:
import java.util.Scanner;
import java.util.ArrayList;
public class Bank
{
private ArrayList bank;
private int count;
//---------------------------------------------------------
//Constructs the Bank and fills it with template accounts
//---------------------------------------------------------
public Bank()
{
bank = new ArrayList();
}
//----------------------------------------------------------
//Creates a new Account
//----------------------------------------------------------
public void create (long accountNum, String owner, String phNumber, double initial)
{
bank.add (new Account(owner, accountNum, phNumber, initial));
count++;
}
//-------------------------------------------------------------------------
//Desposits money into the account that matches the given account number
//-------------------------------------------------------------------------
public void deposit(long acctNum)
{
Scanner scan = new Scanner (System.in);
System.out.println("How much would you like to deposit into account " +acctNum+ "?");
double depositAmount = scan.nextDouble();
findAcct(acctNum).accDeposit(depositAmount);
}
//-----------------------------------------------------------------------
//withdraws money from the account that matches the given account number
//-----------------------------------------------------------------------
public void withdraw(long acctNum)
{
Scanner scan = new Scanner (System.in);
System.out.println("How much would you like to withdraw from account " +acctNum+ "?");
double withdrawAmount = scan.nextDouble();
findAcct(acctNum).accWithdraw(withdrawAmount);
}
//----------------------------------------------
//Adds interest to all accounts
//----------------------------------------------
public void Interest()
{
for (int i=0; i<count; i++)
bank.get(i).addInterest();
}
//---------------------------------------------------
//Prints out all the accounts and their information
//---------------------------------------------------
public String toString()
{
String report = "";
for (int i = 0; i<bank.size(); i++)
report += bank.get(i) + "\n";
return report;
}
//----------------------------------------------------
//Finds the specified account and returns the account
//----------------------------------------------------
private Account findAcct(long acctNum)
{
Account test = new Account();
for (int i=0; i<count; i++)
{
if (acctNum == bank.get(i).getAcctNumber())
test = bank.get(i);
}
return test;
}
}
Re: Accessing methods of an object inside in ArrayList
The ArrayList in bank has not specified a class type to hold. It therefore holds Objects. The code trying to add interest is thus trying to do so on Object, which does not have that method defined. 2 solutions: use generics to define the type of class the List holds, or retrieve the object from the List and Cast it to the specific class, then call the method.