8 Replies - 762 Views - Last Post: 06 November 2009 - 01:39 PM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

calling methods from classes confusion

Posted 06 November 2009 - 01:00 PM

why can't i call the find() method from the Bank class from the SavingsAccount class?
the error is in the SavingsAccount class and i have commented in the error on the correct line.

SavingsAccount
public class SavingsAccount extends Account
{
//Initiazling variables.
	private int countWithdrawals;

	public SavingsAccount(int aNum, String oName, double iRate, double bal)
	{
//Declaring variables and calling superclass constructor.
		super(aNum, oName, iRate, bal);
		countWithdrawals = 0;
	}		
	
	public void withdraw(int number, double amt)
	{
		SavingsAccount sa = accounts.get(find(number));  //error: "cannot find symbol - method find(int)"
		sa.withdraw(amt);
		countWithdrawals++;
		if(countWithdrawals > 3)
		{
			sa.withdraw(30);
		}
		if(sa.getBalance() < 0)
		{
			sa.withdraw(35);
		}
	}
}



Bank
//Importing libraries to use its functions.
import java.text.*;
import java.util.*;

public class Bank
{
//Declaring instance variables.
	private int nextAccount;
	private ArrayList<Account> accounts;
	private ArrayList<SavingsAccount> sAccount;
	private ArrayList<CheckingAccount> cAccount;
	
//Initializing the instance variables.
	public Bank()
	{
		nextAccount = 101;
		accounts = new ArrayList<Account>();
//		 sAccount = new ArrayList<SavingsAccount>();
//		 cAccount = new ArrayList<CheckingAccount>();
	}
	
//Adds a savings account to the bank with the desired input from the user of the owner's name
//and the initial balance.
	public void addSavingsAccount(String name, double balan)
	{		
		accounts.add(new SavingsAccount(nextAccount, name, 3.5, balan));
		nextAccount++;
	}
	
//Adds a checking account to the bank with the desired input from the user of the owner's name
//and the initial balance.
	public void addCheckingAccount(String name, double balan)
	{
		accounts.add(new CheckingAccount(nextAccount, name, 1.95, balan));
		nextAccount++;
	}
	
//Goes and finds at what position the user inputed account number is in the ArrayList
//else returns -1.
	public int find(int number) 
	{
		int position = -1;
		for(int i = 0; i < accounts.size(); i++) 
		{
			if(position == -1)
			{
				Account acc = accounts.get(i);
				if(acc.getAccountNumber() == number) 
				{
					position = i;
				}
			}
		}
		return position;
	}

//Deposits money from the balance from the user inputed bank account.  
	public void deposit(int number, double amt) 
	{
		Account acc = accounts.get(find(number));
		acc.deposit(amt);
	}
	 
//Withdraws money from the balannce from the user inputed bank account.
	public void withdraw(int number, double amt)
	{
		Account acc = accounts.get(find(number));
		acc.withdraw(amt);
	}

//Applies the standard 3.5% interest rate to all the accounts in the bank.
	public void applyInterest()
	{
		for(int i = 0; i < accounts.size(); i++)  
		{
			accounts.get(i).addInterest();
		}
	}

//Prints all the accounts in the bank in order with all the information associated with them.
	public void printAccounts()
	{
		for(int i = 0; i < accounts.size(); i++)
		{
			while(i < accounts.size())
			{
				accounts.get(i).print();
				System.out.println();
				i++;
			}
		}		 
	}
}



Account
//Importing library to use its functions.
import java.text.*;

public class Account
{
//Declaring instance variables.
	private int accountNumber;
	private String ownersName;
	private double interestRate;
	protected double balance;
	
	public Account(int aNum, String oName, double iRate, double bal)
	{
//User initializing the instance variables.
		accountNumber = aNum;
		ownersName = oName;
		interestRate = iRate;
		if(bal < 0.0)
		{
			balance = 0.0;
		}
		else
		{
			balance = bal;
		}
	}

//Gets the account number.
	public int getAccountNumber()
	{
		return accountNumber;
	}

//Gets the owner's name.
	public String getOwnersName()
	{
		return ownersName;
	}
	
//Gets the interest rate.
	public double getInterestRate()
	{
		return interestRate;
	}
	
//Gets the balance.
	public double getBalance()
	{
		return balance;
	}
	
//User sets the owner's name.
	public void setOwnersName(String newOwnersName)
	{
		ownersName = newOwnersName;
	}
	
//User sets the interest rate.
	public void setInterestRate(double newInterestRate)
	{
		interestRate = newInterestRate;
	}
	
//User deposits an amount to the balance that the user pleases.
	public void deposit(double amt)
	{
		if(amt > 0.0)
		{
			balance = balance + amt;
		}
		else
		{
			balance = balance;
		}
	}
	
//User withdraws an amount from the balance that the user pleases.
	public void withdraw(double amt)
	{
		if(amt > 0.0)
		{
			balance = balance - amt;
		}
		else
		{
			balance = balance;
		}
	}

//Adds the interest to the balance in the account.
	public void addInterest()
	{
		balance = balance + (balance * (interestRate / 100));
	}
	
//Prints all the information associated with the account.
	public String toString()
	{
		NumberFormat fmt = NumberFormat.getCurrencyInstance();
		String s = "Account Number: " + accountNumber +"\nOwners Name: " + ownersName + "\nInterest Rate: " + interestRate + "%\n" + "Balance: " + fmt.format(balance);
//		 System.out.println("Account Number: " + accountNumber);
//		 System.out.println("Owners Name: " + ownersName);
//		 System.out.println("Interest Rate: " + interestRate + "%");
//		 System.out.println("Balance: " + fmt.format(balance));
		return s;
	}
}



one more thing: the toString() method that is the last line in the class right above, can i write that better? is there a way to break it up so it isn't so long? i had to change that method from a print() to a toString() so i am just learning this toString() method.

Is This A Good Question/Topic? 0
  • +

Replies To: calling methods from classes confusion

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: calling methods from classes confusion

Posted 06 November 2009 - 01:09 PM

find() is a method of the Bank class, and you need an object of that class to call it.
Was This Post Helpful? 1
  • +
  • -

#3 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: calling methods from classes confusion

Posted 06 November 2009 - 01:11 PM

Where is the accounts variable? You are using it as the calling object but I do not see that anywhere in the class.

In regards to your toString() method, it looks fantastic, tons better then all those println's :)
Was This Post Helpful? 1
  • +
  • -

#4 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: calling methods from classes confusion

Posted 06 November 2009 - 01:17 PM

so if i understood right, i need to make an object of Bank in my SavingsAccount class so i can call the method find()? if so, would that just be this?
Bank bank = new Bank();


Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: calling methods from classes confusion

Posted 06 November 2009 - 01:24 PM

That doesn't make any sense though...because a Bank should be the enclosing class for all of the accounts.

You'll have to use a different way than using find().

You can just traverse the ArrayList and try to find the corresponding account number, instead of just using the find() method.

Using the find() method doesn't make any sense because a SavingsAccount wouldn't need an entire bank, now would it? ;)

This post has been edited by Locke: 06 November 2009 - 01:28 PM

Was This Post Helpful? 0
  • +
  • -

#6 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: calling methods from classes confusion

Posted 06 November 2009 - 01:31 PM

i guess it wouldn't make sense. teacher has thrown a lot of new material at us lately and i don't have a full grasp on it yet :P

and can you explain what traverse an ArrayList means?

This post has been edited by mattlyons: 06 November 2009 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: calling methods from classes confusion

Posted 06 November 2009 - 01:32 PM

Inheritance 101
			   Bank // Has Account objects and Savings
Account			 Savings Account

You cannot use a method that is higher up in the heirarchy.
Bank can call methods from Account and SavingsAccount.
Account can call only its methods.
Savings Account Can call only its methods and all methods from Account
since it is extending it as a parent class.

Savings Account Cannot call Methods from Bank account for it is
lower in the heirarchy then Bank Class is.


Hope this clarifies Things :)
(Correct that if it is wrong Locke, been awhile since had to even think about Inheritance lol)

This post has been edited by Fuzzyness: 06 November 2009 - 01:32 PM

Was This Post Helpful? 0
  • +
  • -

#8 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: calling methods from classes confusion

Posted 06 November 2009 - 01:35 PM

ok that makes a lot of sense. thanks for that
Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: calling methods from classes confusion

Posted 06 November 2009 - 01:39 PM

Alright...first off. Bank is not inheriting anything. It is neither a parent nor a child. It is simply a wrapping class for holding the accounts (parent/children) information.

NONE of the account classes can call anything in the Bank class. They are of no relation. You can use the Bank's Account objects to call their corresponding methods, but not the other way around.

The Bank class is simply a container for all of the accounts.

This post has been edited by Locke: 06 November 2009 - 01:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1