calling a method and altering a ArrayList

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1496 Views - Last Post: 02 November 2009 - 06:00 PM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

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

calling a method and altering a ArrayList

Posted 29 October 2009 - 06:16 PM

i am suppose to use the find(int number) method to search thro the ArrayList and deposit and amount of money into the found account. once i call the find(int number method), how would i change only one of the parameters of the Account object by changing balance without changing the others?

account
public class Account
{
//Declaring instance variables.
	private int accountNumber;
	private String ownersName;
	private double interestRate;
	private 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 void print()
	{
		System.out.println("Account Number: " + accountNumber);
		System.out.println("Owners Name: " + ownersName);
		System.out.println("Interest Rate: " + interestRate);
		System.out.println("Balance: " + balance);
	}
}



bank
//Importing the util library to use its functions (basically ArrayList)
import java.util.*;

public class Bank
{
//Declaring instance variables.
	private int nextAccount;
	private ArrayList<Account> accounts;
	
//Initializing the instance variables.
	public Bank()
	{
		nextAccount = 101;
		accounts = new ArrayList<Account>();
	}
	
//Adds a bank account to the bank with the desired input from the user of the owner's name
//and the initial balance.
	public void addAccount(String name, double balan)
	{		
		accounts.add(new Account(nextAccount, name, .035, balan));
		nextAccount++;
	}
	
//Checks to see if the user inputed account number is in the bank or not.
	public int find(int number)
	{
		int position = -1;
		for(int i = 0; i < accounts.size(); i++)
		{
			if(accounts.get(i).getAccountNumber() == number)
			{
				position = i + 1;
			}
			else
			{
				position = position;
			}
		}
		return position;
	}  
	
//Deposits money from the balance from the user inputed bank account.
	public void deposit(int number, double amt)
	{
		if(amt > 0)
		{
			//accounts.find(number)
		}



Is This A Good Question/Topic? 0
  • +

Replies To: calling a method and altering a ArrayList

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: calling a method and altering a ArrayList

Posted 29 October 2009 - 06:44 PM

void add(int accountNum, double amt) {
	// loop through all the Account in the ArrayList
	for(int i = 0; i < accounts.size(); i++) {
	   // get next Account stored in the arrayList
	   Account acc = accounts.get(i);
	   // if the accountNumber of this account match the one received as parameter
	   if(acc.getAccountNumber() == accountNum) {
		   // add the amount received as parameter
		   acc.deposit(amt);
		   // exit the loop
		   break;
	   }
}



Have fun
Was This Post Helpful? 0
  • +
  • -

#3 mattlyons  Icon User is offline

  • D.I.C Regular

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

Re: calling a method and altering a ArrayList

Posted 29 October 2009 - 06:47 PM

awesome. thx man
Was This Post Helpful? 0
  • +
  • -

#4 mattlyons  Icon User is offline

  • D.I.C Regular

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

Re: calling a method and altering a ArrayList

Posted 29 October 2009 - 07:42 PM

ok i used pbl's code for a find() method. now i have to call the find() method in a deposit and withdraw method. do i have the right idea here, just wrong syntax? i included in the code segment where and what the error is i am getting.

	public int find(int number) 
	{
		int position = -1;
		for(int i = 0; i < accounts.size(); i++) 
		{
			Account acc = accounts.get(i);
			if(acc.getAccountNumber() == number) 
			{
				position = i + 1;
			}
		}
		return position;
	}

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


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: calling a method and altering a ArrayList

Posted 30 October 2009 - 04:32 AM

View Postmattlyons, on 29 Oct, 2009 - 06:42 PM, said:

ok i used pbl's code for a find() method. now i have to call the find() method in a deposit and withdraw method. do i have the right idea here, just wrong syntax? i included in the code segment where and what the error is i am getting.

	public int find(int number) 
	{
		int position = -1;
		for(int i = 0; i < accounts.size(); i++) 
		{
			Account acc = accounts.get(i);
			if(acc.getAccountNumber() == number) 
			{
				position = i + 1;
			}
		}
		return position;
	}

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



3 things:

- first the "position" within the ArrayList list is i no need to init a position
- second no needs to continue the for loop if the account is found

	public int find(int number) 
	{
		for(int i = 0; i < accounts.size(); i++) 
		{
			Account acc = accounts.get(i);
			if(acc.getAccountNumber() == number) 
			{
				return i;
			}
		}
		return -1;
	}



- third instead of returning directly the account no need to fetch it again on the caller
this is what you where doing: Account acc = accounts.find(number);
	public Account find(int number) 
	{
		for(int i = 0; i < accounts.size(); i++) 
		{
			Account acc = accounts.get(i);
			if(acc.getAccountNumber() == number) 
			{
				return acc;
			}
		}
		return null;
	}



For the second eror don't see in your code where you create the Bank object accounts:

Bank accounts = new Bank();
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 a method and altering a ArrayList

Posted 30 October 2009 - 06:33 AM

-well the assignment of the find() method was to return the position of the account entered by the user so that is why i went that way.
-as for stopping the for loop when the account is found, i know i should have a break in there but he doesn't want us to use breaks AND he also tells us to never ever type the word return more than once in a method. haha he just wants it his way
-again on his specifics, i can't use your Account find method because he gives us the signature he wants us to use so that wouldn't work. my deposit and withdraw methods are suppose to call the find() method in them

you said use Bank accounts = new Bank() but accounts is already defined in Bank class as an ArrayList of accounts.

This post has been edited by mattlyons: 30 October 2009 - 06:42 AM

Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: calling a method and altering a ArrayList

Posted 30 October 2009 - 04:02 PM

View Postmattlyons, on 30 Oct, 2009 - 05:33 AM, said:

-well the assignment of the find() method was to return the position of the account entered by the user so that is why i went that way.


So that code of your is wrong

Account acc = accounts.find(number);
it should be
int index = accounts.find(number);

View Postmattlyons, on 30 Oct, 2009 - 05:33 AM, said:

-as for stopping the for loop when the account is found, i know i should have a break in there but he doesn't want us to use breaks AND he also tells us to never ever type the word return more than once in a method. haha he just wants it his way


There are pros and cons for that
I would respect your teacher decison.... but just your argue...
If you have 100,000 accounts and the account you are treating is the second account in the list you will do

		   Account acc = accounts.get(i);
			if(acc.getAccountNumber() == number) 
			{
				position = i + 1;
			}


99, 998 times for nothing.... is your teacher also selling hardware on the side ? :)

Imagine during the night where the interests are credited to CPU time you will lose !!!

at least make it that way

		int position = -1;
		for(int i = 0; i < accounts.size(); i++) 
		{
			if(position != -1) {	 // account already found
				 Account acc = accounts.get(i);
				 if(acc.getAccountNumber() == number) 
				 {
					 position = i + 1;
				  }
			 }
		}
		return position;
	}



View Postmattlyons, on 30 Oct, 2009 - 05:33 AM, said:

you said use Bank accounts = new Bank() but accounts is already defined in Bank class as an ArrayList of accounts.

Yes but your code does not show where the Bank class is instantiate
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 a method and altering a ArrayList

Posted 31 October 2009 - 06:26 PM

ok good call on the find() method. in the deposit() and withdraw() methods, it still says "cannot find symbol - method find(int)".

and back to you saying that i haven't made a Bank() yet, what do you mean? i am a little confused on exactly what that means. is this why i am getting the error above?

public void deposit(int number, double amt) 
{
	int index = accounts.find(number);  //error: "cannot find symbol - method find(int)"
	index.deposit(amt);
}


This post has been edited by mattlyons: 31 October 2009 - 06:27 PM

Was This Post Helpful? 0
  • +
  • -

#9 mattlyons  Icon User is offline

  • D.I.C Regular

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

Re: calling a method and altering a ArrayList

Posted 01 November 2009 - 09:33 AM

any help?
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: calling a method and altering a ArrayList

Posted 01 November 2009 - 01:14 PM

Repost your code including the class with the main() method
Was This Post Helpful? 0
  • +
  • -

#11 mattlyons  Icon User is offline

  • D.I.C Regular

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

Re: calling a method and altering a ArrayList

Posted 01 November 2009 - 05:40 PM

the deposit() and withdraw() methods MUST call the find() method. the error is in the bank class and itoccurs in the deposit() and withdraw() methods and i commented the error beside the lines. this program does not have a main()

bank
//Importing the util library to use its functions (basically ArrayList)
import java.util.*;

public class Bank
{
//Declaring instance variables.
	private int nextAccount;
	private ArrayList<Account> accounts;
	
//Initializing the instance variables.
	public Bank()
	{
		nextAccount = 101;
		accounts = new ArrayList<Account>();
	}
	
//Adds a bank account to the bank with the desired input from the user of the owner's name
//and the initial balance.
	public void addAccount(String name, double balan)
	{		
		accounts.add(new Account(nextAccount, name, .035, 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 + 1;
				}
			}
		}
		return position;
	}

//Deposits money from the balance from the user inputed bank account.  
	public void deposit(int number, double amt) 
	{
		int index = accounts.find(number);  //error: "cannot find symbol - method find(int)"
		index.deposit(amt);
	}
	 
//Withdraws money from the balannce from the user inputed bank account.
	public void withdraw(int number, double amt)
	{
		int index = accounts.find(number);  //error: "cannot find symbol - method find(int)"
		index.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++)  
		{
			while(i < accounts.size())
			{
				double temp = 0;
				temp = (accounts.get(i).getBalance() * accounts.get(i).getInterestRate()) + accounts.get(i).getBalance();
				accounts.get(i).getBalance() = temp;
			}
		}
	}

//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())
			{
				System.out.println("Account Number: " + accounts.get(i).getAccountNumber());
				System.out.println("Owner's Name: " + accounts.get(i).getOwnersName());
				System.out.println("Interest Rate: " + accounts.get(i).getInterestRate());
				System.out.println("Balance: " + accounts.get(i).getBalance());
				System.out.println();
				i++;
			}
		}		 
	}
}



account
public class Account
{
//Declaring instance variables.
	private int accountNumber;
	private String ownersName;
	private double interestRate;
	private 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 void print()
	{
		System.out.println("Account Number: " + accountNumber);
		System.out.println("Owners Name: " + ownersName);
		System.out.println("Interest Rate: " + interestRate);
		System.out.println("Balance: " + balance);
	}
}


This post has been edited by mattlyons: 01 November 2009 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#12 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: calling a method and altering a ArrayList

Posted 01 November 2009 - 05:52 PM

hi
accounts is an ArrayList object.
you cannot call the find method from accounts.
you can call find without creating an instance.
find will give you an index, so use:
accounts.get(find(number));
as in code:
public void deposit(int number, double amt) 
	{
		int index = accounts.get(find(number));  
		//i dont understand what you try to do... so i didnt make a change.
	}



the same change innside withdraw method also.

calling index.deposit(int num) is also illegal.
deposit is a Bank instance method, while index is an int variable.
Was This Post Helpful? 0
  • +
  • -

#13 mattlyons  Icon User is offline

  • D.I.C Regular

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

Re: calling a method and altering a ArrayList

Posted 01 November 2009 - 06:06 PM

still coming up with an error in the same line of code.

public void deposit(int number, double amt) 
{
   int index = accounts.get(find(number));  //error: "incompatible types - found Account but expected int"
   index.deposit(amt);
}



is this even possible what i am trying to do? the instructions on the assignment says i have to call the find() method in deposit() which finds the account with the inputted account number and then takes the amount inputted and deposits it into the account found.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: calling a method and altering a ArrayList

Posted 01 November 2009 - 07:10 PM

You haven't post your main() method that should instansiate the Bank class
Was This Post Helpful? 0
  • +
  • -

#15 mattlyons  Icon User is offline

  • D.I.C Regular

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

Re: calling a method and altering a ArrayList

Posted 01 November 2009 - 07:18 PM

this project has no main(). how do i instantiate a Bank class? is it:

Bank bank = new Bank()

where would that go? i am a little confused on what you mean by instantiating a Bank class.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2