-3
public class Account {
public double balance ;
public double deposite;
public double withdraw;
public Account(double balance) {
    this.balance = balance;
   }
public double getBalance() {
    return balance;
}
public void setBalance(double balance) {
    this.balance = balance;
}
public double getDeposite() {
    balance = balance + deposite;
    return deposite;
}
public void setDeposite(double deposite) {
    this.deposite = deposite;

}
public double getWithdraw() {
 return withdraw;
}
public void setWithdraw(double withdraw) {
    this.withdraw = withdraw;
    if(withdraw <= balance){
   balance =  balance - withdraw;
    }
}
 public boolean withdraw(double wamt)
{
    boolean result = false;
    if(withdraw <= wamt)
    {
    balance= balance - withdraw;
    return true;
    }
    return result; 
    }

}

My customer class

public class Customer {
private String firstName;
private String lastName;
Account account;
public Customer(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
    //this.account = account;
}
public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public Account getAccount() {
    return account;
}
public void setAccount(Account account) {
    this.account = account; }   }


public class BankProjectDemo {
public static void main(String[] args) {
   double balance = 500;
   Customer cust = new Customer("asasd0","asdasda");
   Account accnt = new Account(balance);
   System.out.println("Creating customer:  " +cust.getFirstName());
   accnt.setWithdraw(150);
   accnt.setDeposite(22.50);
   System.out.println("Withdraw1  "+accnt.getWithdraw());
   System.out.println("Depsoite  "+accnt.getDeposite());
   Account accnt1 = new Account(balance);
   accnt1.setWithdraw(47.62);
   System.out.println("Withdraw2 "+accnt1.getWithdraw()+"  " + accnt1.withdraw(balance));
   System.out.println("Balance " + accnt.getBalance());}}

I'm trying to calculate a simple banking function.I passed two withdraw value 150 and 47.62 through one method from execution class to another.But it takes 47.62 twice and giving me wrong result here is the execution class.

Shejan
  • 21
  • 5
  • 5
    You should think about what you're really modelling. A bank account has a balance - but it doesn't have "a withdraw". Withdrawing money is an *action*, not a property of the account. (You might model it to have several retained transactions, but not a *single* one.) – Jon Skeet May 19 '13 at 11:33
  • 7
    IF we do not know what the other methods do such as withdraw and deposit, then it becomes quite hard to answer the question. – Graham Smith May 19 '13 at 11:33
  • Why you do `Account accnt2 = accnt; accnt2.setWithdraw(1000);` instead of `accnt.setWithdraw(1000);`? Read this: http://stackoverflow.com/questions/40480/is-java-pass-by-reference – johnchen902 May 19 '13 at 11:35
  • 2
    @user2349171 Please edit your post instead of adding code in the comments. – Simon Forsberg May 19 '13 at 11:36
  • @user2349171 edit your question with that code, do not post it here as it adds value to your question. – Graham Smith May 19 '13 at 11:36
  • What's the difference between Account.setWithdraw(x) and Account.withdraw(x) ? Since a withdraw is an action, is it not enough with Account.withdraw(x) ? – Simon Forsberg May 19 '13 at 11:38
  • Because nobody else has said it yet, I'll give the obligatory warning: **Do not** use floating point data types for actual money! Use `BigDecimal` instead (as per [this question](http://stackoverflow.com/questions/8148684/what-is-the-best-data-type-to-use-for-money-in-java-app)). – T045T May 19 '13 at 12:04
  • Account.withdraw() methods returns a boolean result .both methods are same but one is boolean type and other is void type.My question is the resulting balance should be 324.88 as total balance but it gives 277.26. It takes second withdraw amount twice but why?????????? – Shejan May 20 '13 at 08:04

1 Answers1

6

In your

public void setWithdraw(double withdraw) {
    this.withdraw = withdraw;
    if(withdraw <= balance){
   balance =  balance - withdraw;
    }
}

You reduced your balance once, and in your

public boolean withdraw(double wamt)
{
    boolean result = false;
    if(withdraw <= wamt)
    {
    balance= balance - withdraw;
    return true;
    }
    return result; 
}

You reduced your balance once again, thus the double withdraw when you call accnt1.setWithdraw(47.62); followed by accnt1.withdraw(balance); in

accnt1.setWithdraw(47.62);
System.out.println("Withdraw2 "+accnt1.getWithdraw()+"  " + accnt1.withdraw(balance));

Change your setWithdraw(double withdraw) to exclude the

if(withdraw <= balance){
   balance =  balance - withdraw;
}

i.e.

public void setWithdraw(double withdraw) {
    this.withdraw = withdraw;
}

And you don't need the parameter wamt in your withdraw() function.

i.e.

public boolean withdraw()
{
    boolean result = false;
    if(withdraw <= balance)
    {
    balance= balance - withdraw;
    return true;
    }
    return result; 
}
Benny Ng
  • 328
  • 2
  • 6
  • if I do it how can i count both ?If i assign into different object the total result gives different – Shejan May 19 '13 at 16:17