I'm writing an assignment for my introduction to programming class and we're supposed to implement the following interface:

public Interface Account{ 
//Calculates interest (1%) and adds to the account balance
public void interest(); 
//Calculates the balance after a deposit
public void payIn(double money); 
//Calculates the balance after a charge on the account
public boolean payOut(double money); 
//Prints the balance
public void print(); 
}

into a class called GiroAccount. We're also supposed to include a constructor that can set the initial account balance. We're supposed to write our own interface Interaction including methods that would need to happen if an online money transfer were going on between this account and another one. I wrote the following interface:

public Interface Interaction {


	//Calculates the account balance if "deposit" sent the money
	public void transferOut(double transfer);
	//Calculates the account balance if "deposit" received the money
	public void transferIn(double tranfser);
}

And here is the class that I have written:

import java.util.Scanner;


public class GiroAccount implements Account, Interaction {
	
	double deposit;
	
	public GiroAccount(double deposit) {
		this.deposit = deposit;
	}
	
	public void interest() {
		deposit = (deposit*.01) + deposit;
	}
	
	public void payIn(double money) {
		deposit = deposit + money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
	}
	
	public boolean payOut(double money) {
		if (money < -3000.00){
		throw new RuntimeException("Your account is more than 3000 Euros overdrawn!");
		}
		else {
		deposit = deposit - money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
		}
		return true;
	}
	
	public void transferOut(double transfer) {
		deposit = deposit - transfer;
		System.out.println("You have transfered " + transfer + " Euros and your new account balance is " + deposit + " Euros.");
	}
	
	public void transferIn(double transfer) {
		deposit = deposit + transfer;
		System.out.println("You have received "+transfer+" Euros from the transfer and your new balance is "+deposit+" Euros.");
	}
	
	public void print() {
		System.out.println("Your balance is " + deposit + " Euros.");
	}
	
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);		
		
				
		System.out.println("Please enter the account number of the person to whom you wish to transfer money.");
		String accountNumber = input.nextLine();
		System.out.println("Thank you. Now please input the amount (in the form: ##.##) you wish to transfer.");
		double transfer = input.nextDouble();
		System.out.println("Thank you once more. Now can you please enter the reason for this transfer?");
		String reason = input.nextLine();
		System.out.println("Very well. Your transaction has been completed.");
		
		//transfer.transferIn();
	} 
}

In the third-to-last-line you can see my poor attempt to try and get the transferIn method to be used after the transaction of the transfer with the user. I'm just not quite sure how to get that to work.

Recommended Answers

All 21 Replies

the class double doesn't have a method transerIn
you'll need an instance of the class you're working in, and
use a method of this:

// create an instance
instance.transferIn(transfer);

I'm writing an assignment for my introduction to programming class and we're supposed to implement the following interface:

public Interface Account{ 
//Calculates interest (1%) and adds to the account balance
public void interest(); 
//Calculates the balance after a deposit
public void payIn(double money); 
//Calculates the balance after a charge on the account
public boolean payOut(double money); 
//Prints the balance
public void print(); 
}

into a class called GiroAccount. We're also supposed to include a constructor that can set the initial account balance. We're supposed to write our own interface Interaction including methods that would need to happen if an online money transfer were going on between this account and another one. I wrote the following interface:

public Interface Interaction {


	//Calculates the account balance if "deposit" sent the money
	public void transferOut(double transfer);
	//Calculates the account balance if "deposit" received the money
	public void transferIn(double tranfser);
}

And here is the class that I have written:

import java.util.Scanner;


public class GiroAccount implements Account, Interaction {
	
	double deposit;
	
	public GiroAccount(double deposit) {
		this.deposit = deposit;
	}
	
	public void interest() {
		deposit = (deposit*.01) + deposit;
	}
	
	public void payIn(double money) {
		deposit = deposit + money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
	}
	
	public boolean payOut(double money) {
		if (money < -3000.00){
		throw new RuntimeException("Your account is more than 3000 Euros overdrawn!");
		}
		else {
		deposit = deposit - money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
		}
		return true;
	}
	
	public void transferOut(double transfer) {
		deposit = deposit - transfer;
		System.out.println("You have transfered " + transfer + " Euros and your new account balance is " + deposit + " Euros.");
	}
	
	public void transferIn(double transfer) {
		deposit = deposit + transfer;
		System.out.println("You have received "+transfer+" Euros from the transfer and your new balance is "+deposit+" Euros.");
	}
	
	public void print() {
		System.out.println("Your balance is " + deposit + " Euros.");
	}
	
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);		
		
				
		System.out.println("Please enter the account number of the person to whom you wish to transfer money.");
		String accountNumber = input.nextLine();
		System.out.println("Thank you. Now please input the amount (in the form: ##.##) you wish to transfer.");
		double transfer = input.nextDouble();
		System.out.println("Thank you once more. Now can you please enter the reason for this transfer?");
		String reason = input.nextLine();
		System.out.println("Very well. Your transaction has been completed.");
		
		//transfer.transferIn();
	} 
}

In the third-to-last-line you can see my poor attempt to try and get the transferIn method to be used after the transaction of the transfer with the user. I'm just not quite sure how to get that to work.

try:

new GiroAccount().transferIn(transfer);

try:

new transferIn(transfer);

new transferIn(..)?

it's not a constructor :)

new transferIn(..)?

it's not a constructor :)

yup i changed already,see my editedt post:P. lol. but fast eyes stultuske

Oh! Let me try it out...

Hm. I get the error "cannot find symbol" with the arrow pointing to the word 'new'.

yup i changed already,see my editedt post:P. lol. but fast eyes stultuske

ever heard of Lucky Luke, the (comic) cowboy supposedly shoots faster than his shadow?
I'm from the same country dude ;) :D

commented: damnit +6

Hm. I get the error "cannot find symbol" with the arrow pointing to the word 'new'.

well i guess it would depend where you are calling the method from. try what stultuske said create a new instance of the class then access the method from there.
[EDIT]You win lmao :)

Haha. :D (sorry - just had to laugh at that - I read Lucky Luke as a kid!)

But it should still work if I do that within the main method? (If my questions are silly, I apologize - I've only been working with java for about two months!)

yup, no problem there

Like this:

GiroAccount myAccount = new GiroAccount();
		myAccount.transferIn(transfer);

?

Like this:

GiroAccount myAccount = new GiroAccount();
		myAccount.transferIn(transfer);

?

thats it :)

That makes sense to me - but I still get the error with 'new'. :/ Why might that be?

can you post your entire code?

Sure:

import java.util.Scanner;


public class GiroAccount implements Account, Interaction {
	
	double deposit;
	
	public GiroAccount(double deposit) {
		this.deposit = deposit;
	}
	
	public void interest() {
		deposit = (deposit*.01) + deposit;
	}
	
	public void payIn(double money) {
		deposit = deposit + money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
	}
	
	public boolean payOut(double money) {
		if (money < -3000.00){
		throw new RuntimeException("Your account is more than 3000 Euros overdrawn!");
		}
		else {
		deposit = deposit - money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
		}
		return true;
	}
	
	public void transferOut(double transfer) {
		deposit = deposit - transfer;
		System.out.println("You have transfered " + transfer + " Euros and your new account balance is " + deposit + " Euros.");
	}
	
	public void transferIn(double transfer) {
		deposit = deposit + transfer;
		System.out.println("You have received "+transfer+" Euros from the transfer and your new balance is "+deposit+" Euros.");
	}
	
	public void print() {
		System.out.println("Your balance is " + deposit + " Euros.");
	}
	
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);		
		
				
		System.out.println("Please enter the account number of the person to whom you wish to transfer money.");
		String accountNumber = input.nextLine();
		System.out.println("Thank you. Now please input the amount (in the form: ##.##) you wish to transfer.");
		double transfer = input.nextDouble();
		System.out.println("Thank you once more. Now can you please enter the reason for this transfer?");
		String reason = input.nextLine();
		System.out.println("Very well. Your transaction has been completed.");
		GiroAccount myAccount = new GiroAccount();
		myAccount.transferIn(transfer);
		
	} 
}

you don't have a default constructor there, only one that takes a double as parameter.
so, try

double initialAmount = // set your initial Amount;
GiroAccount myAccount = new GiroAccount(initialAmount);

or add a default constructor (one that doesn't take a parameter) to your class

Oh! Of course. :P Thank you!
If I can bug you with one last question: if I were to extend a class from this one, would that new class automatically use Account and Interaction, or do I need to write 'implements' again?

(It works now! :) )

no, they would implement those as well, you just don't have to redefine those methods, since you inherit them from this class. doesn't mean you're not allowed to, of course :)

Perfect! Thank you so much! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.