Ok,
I trying to do a simulated ATM machine as a final year project and i got some of its functions done and the others not.
i am trying to tranfer money between currentAccount and savingsAccount using a method called Transfer but its not working can you help me please to sort that out.
here is the code for the BankAcount that includes the method
import java.util.*;
import java.text.*;
import java.io.*;
public class BankAccount{
BankAccount currentAccount;
BankAccount savingsAccount;
Customer cus;
public int accountNo;
public double balance;
public BankAccount(){
}
public BankAccount(int accountNo, double balance){
this.accountNo = accountNo;
this.balance= balance;
}
public int getAccountNo(){
return accountNo;
}
public double getBalance(){
return balance;
}
public void deposit(double amount){
this.balance = balance;
balance = balance + amount;
}
public void withdraw(double amount){
this.balance = balance;
balance = balance - amount ;
}
public double transfer(BankAccount currentAccount, BankAccount savingsAccount,double amount) {
//fetch the currentAccount
balance = currentAccount.getBalance();
if(balance == 0){
System.out.print("Can not transfer money ");
}
balance = savingsAccount.getBalance();
if(balance == 0) {
System.out.print("Invalid amount");
}
if(currentAccount.getBalance() > amount){
balance = currentAccount.getBalance() - amount;
balance = savingsAccount.getBalance() + amount;
}
return balance;
}
public void PrintReciept(){
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String date = formatter.format(today);
String message = "Account No:" + getAccountNo()+
"\n Customer No:" + cus.getCustomerNumber()+
"\n\n" + date+
"\n\n Balance:" + getBalance()+
"\n\n Thank You\n"+
"-\n";
}
}
What are you trying to do in that method?
First you pass in two bank accounts - why would you call "accountA.transfer(accountB, accountC, amount)"? Where's the money flowing? You say you're trying to transfer from the current to the savings, ie accountB to accountC, so why call a method on accountA? If you want a method to transfer funds I would suggest,public void transferTo(BackAccount toAccount, double amount) throws InsufficientFundsException {...}
But as for the problem, you should declare a local variable for the return value since you're modifying the balance of accountA via the field - and accountA is the one which seemingly has no involvement in the transaction (which as I say is daft because you're calling the method on it).