Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Accessing methods of an object inside in ArrayList

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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

     
    	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.
    Last edited by jmack; January 23rd, 2011 at 10:11 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Accessing methods of an object inside in ArrayList

    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.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Accessing methods of an object inside in ArrayList

    here is the Account Class

     
    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)

     
    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;
     
    	}
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  5. The Following User Says Thank You to copeg For This Useful Post:

    jmack (January 23rd, 2011)

Similar Threads

  1. SAX Parser will not add object to arrayList
    By michaelz in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 1st, 2010, 02:59 PM
  2. How do i access methods from an arraylist?
    By kiph in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 7th, 2010, 07:52 AM
  3. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 12:45 PM
  4. accessing elements in a 2d ArrayList
    By Flamespewer in forum Collections and Generics
    Replies: 2
    Last Post: March 8th, 2010, 05:11 PM
  5. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 02:08 AM