当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Giving wrong result

问题描述:

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.

网友答案:

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; 
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: