-4

単純な銀行関数を計算しようとしています.2つの引き出し値150と47.62を実行クラスから別のメソッドに渡しました.しかし、47.62が2回かかり、ここで間違った結果が実行クラスに与えられます.

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; 
  }
}

私の顧客クラス

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());
  }
}
4

3 に答える 3

1
public double getDeposite() {
    balance = balance + deposite;
    return deposite;
  }

このメソッドは間違っているように見えます。変数を返すたびに別の変数がインクリメントされます。これは、このメソッドを呼び出すほど、バランスが大きくなることを意味します。これは意図的なものですか?

また、さまざまな変数で何が起こっているかを正確に確認できるように、基本的な単体テストとデバッグを検討する必要があります。

于 2013-05-20T08:18:10.200 に答える