Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

make a new method 1

Status
Not open for further replies.

asm1

Technical User
Oct 9, 2003
77
GB
I am trying to add the values from the account method but I don’t know how to call it or how write the first line.
Code:
creditAccounts()
	{
		if theAccountNumber == 1001{
			theBalance += 1000;
		}
		if theAccountNumber == 1002{
			theBalance += 500;
		}
		return theBalance;
	}
I am trying to test the account number and if it is a certain one add a value to balance.

the rest of the code is same as previous post

account1.java
Code:
public class Account1
{
	private int theAccountNumber;
	private int theBalance;
	private String theHolder;
	
	public Account1(int accN, int initBal, String name)//constructor method
	{
		this.setAccountNumber(accN);
		this.setBalance(initBal);
		this.setHolder(name);
	}
	
	public void setAccountNumber(int aNumber)//modifier method
	{
		theAccountNumber=aNumber;
	}
	
	public void setBalance(int anAmount)
	{
		theBalance=anAmount;
	}
	
	public void setHolder(String aName)
	{
		theHolder=aName;
	}
	
	public int getAccountNumber()
	{
		return theAccountNumber;
	}
	
	public int getBalance()
	{
		creditAccounts()
		return theBalance;
	}
	
	public String getHolder()
	{
		return theHolder;
	}
	
	creditAccounts()
	{
		if theAccountNumber == 1001{
			theBalance += 1000;
		}
		if theAccountNumber == 1002{
			theBalance += 500;
		}
		return theBalance;
	}
	
}

acccount.java

Code:
public class Account
{
	public static void main(String[]args)
	{
		//cereate new account
		Account1 myAccount=new Account1(1000, 2000, "Tony");
		
		//list the excisting accounts
		int accountNumber=myAccount.getAccountNumber();
		int accountBalance=myAccount.getBalance();
		String accountHolder = myAccount.getHolder();
	
		System.out.println("Account Number " +accountNumber);
		System.out.println("Account Balance "+accountBalance);		
		System.out.println("Account Holder " +accountHolder);
		System.out.println();
		
		Account1 hisAccount=new Account1(1001, 2000, "Mark");		
		accountNumber=hisAccount.getAccountNumber();
		accountBalance=hisAccount.getBalance();
		accountHolder = hisAccount.getHolder();		
		System.out.println("Account Number1 " +accountNumber);
		System.out.println("Account Balance1 "+accountBalance);		
		System.out.println("Account Holder1 " +accountHolder);
		System.out.println();
		
		Account1 herAccount=new Account1(1002, 500, "Sarah");		
		accountNumber=herAccount.getAccountNumber();
		accountBalance=herAccount.getBalance();
		accountHolder = herAccount.getHolder();		
		System.out.println("Account Number1 " +accountNumber);
		System.out.println("Account Balance1 "+accountBalance);		
		System.out.println("Account Holder1 " +accountHolder);		
	
	}
}

thx for your help
 
i have made the assumption that you want to make the creditAccounts method return an interger value.

public class Account1{
...//rest of code
public int creditAccounts(){
if theAccountNumber == 1001{
theBalance += 1000;
}
if theAccountNumber == 1002{
theBalance += 500;
}
return theBalance;
}
...//reset of code
}

you can then call it like this..

int newAmaount = hisAccount.creditAccounts();
 
my apologies i made some typos, ignore the above post and do this instead...

public class Account1
{
private int theAccountNumber;
private int theBalance;
private String theHolder;

public Account1(int accN, int initBal, String name)//constructor method
{
this.setAccountNumber(accN);
this.setBalance(initBal);
this.setHolder(name);
}

public void setAccountNumber(int aNumber)//modifier method
{
theAccountNumber=aNumber;
}

public void setBalance(int anAmount)
{
theBalance=anAmount;
}

public void setHolder(String aName)
{
theHolder=aName;
}

public int getAccountNumber()
{
return theAccountNumber;
}

public int getBalance()
{
creditAccounts();
return theBalance;
}

public String getHolder()
{
return theHolder;
}

public void creditAccounts(){
if(theAccountNumber == 1001)
theBalance += 1000;
if(theAccountNumber == 1002)
theBalance += 500;
}
}

then if you wanted to credit the accounts you can call it by using..

myAccount.creditAccounts();



 
thanks for your good advice on this post and my last post it was much appreciated.

Although I have some programming knowledge I am very new to Java and finding the concepts different to the languages I have used.

i have given you a star For you quality of advice and patience.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top