'构造函数未找到....'错误

时间:2014-12-17 07:27:37

标签: java

小时。我在这段代码上收到错误,并且不知道该怎么做。任何人都可以告诉我问题是什么以及如何解决它。

错误: 没有为Transaction找到合适的构造函数(int,String,double,double(

代码加下划线:

Transaction transaction = new Transaction(bankAccount.getTransactions().size(), "Deposit", depositAmount, currentBalance);
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mainsample;
/**
 *
 * @author Khalid
 */
public class TransactionProcess {

    public void deposit(BankAccount bankAccount, double depositAmount) {
    //Get the CurrentBalance
    double currentBalance = bankAccount.getCurrentBalance();

    //First Argument : set the Id of transaction
    //Second Argument : set the Type of Transaction
    //Third Argument : set the TransactionAmount 
    //Fourth Argument : set the Balance Before the transaction (for record purposes)
    Transaction transaction = new Transaction(bankAccount.getTransactions().size(), "Deposit", depositAmount, currentBalance);

    if (depositAmount <= 0) {
      System.out.println("Amount to be deposited should be positive");
    } else {
      //Set the updated or transacted balance of bankAccount.
      bankAccount.setCurrentBalance(currentBalance + depositAmount);
      //then set the MoneyAfterTransaction

      bankAccount.addTransaction(transaction);    // adds a transaction to the bank account
      System.out.println(depositAmount + " has been deposited.");
    }

  }

  // Explanation same as above
  public void withdraw(BankAccount bankAccount, double withdrawAmount) {
    double currentBalance = bankAccount.getCurrentBalance();
    Transaction transaction = new Transaction(bankAccount.getTransactions().size(), "Withdraw", withdrawAmount, currentBalance);

    if (withdrawAmount <= 0) {
      System.out.println("Amount to be withdrawn should be positive");
    } else {
      if (currentBalance < withdrawAmount) {
        System.out.println("Insufficient balance");
      } else {
        bankAccount.setCurrentBalance(currentBalance - withdrawAmount);        
        bankAccount.addTransaction(transaction);    // adds a transaction to the bank account
        System.out.println(withdrawAmount + " has been withdrawed,");
      }
    }
  }
}
package mainsample;

/**
 *
 * @author Khalid
 */
public class Transaction {


  private String transactionType;
  private double transactionAmount;
  private int transactionDate;


  public Transaction() {}

  public Transaction( String transactionType, int transactionDate, double transactionAmount) {

    this.transactionType = transactionType;
    this.transactionAmount = transactionAmount;
    this.transactionDate = transactionDate;

  }

  public int getTransactionDate() {
      return transactionDate;
  }

  public void setTransactionDate (int transactionDate) {
      this.transactionDate = transactionDate;
  }


  public String getTransactionType() {
    return transactionType;
  }

  public void setTransactionType(String transactionType) {
    this.transactionType = transactionType;
  }

  public double getTransactionAmount() {
    return transactionAmount;
  }

  public void setTransactionAmount(double transactionAmount) {
    this.transactionAmount = transactionAmount;
  }


  //Override the toString() method of String ? 
  public String toString() {
    return " Transaction Amount : "+ this.transactionAmount +
           " Transaction Type : " + this.transactionType +
           " Transaction Date:  " + this.transactionDate;

  }

}
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mainsample;
import java.util.*;
/**
 *
 * @author Khalid
 */
public class BankAccount {
    private int accountId;
    private String holderName;
    private String holderAddress;
    private String openDate;
    private double currentBalance;

    private List<Transaction> transactions = new ArrayList<Transaction>();

    //Provide Blank Constructor
    public BankAccount(){}

    //Constructor with an arguments.
    public BankAccount(int accountNum, String holderNam,double currentBalance, String holderAdd,String openDate) {
            this.accountId = accountNum;
            this.holderName = holderNam;
            this.holderAddress = holderAdd;
            this.openDate = openDate;
            this.currentBalance = currentBalance;
    }

    // Always Provide Setter and Getters
    public int getAccountId() {
        return accountId;
    }
    public void setAccountId(int accountId) {

         this.accountId = accountId;                  
    }
    public String getHolderName() {
        return holderName;
    }
    public void setHolderName(String holderName) {
        this.holderName = holderName;
    }
    public String getHolderAddress() {
        return holderAddress;
    }
    public void setHolderAddress(String holderAddress) {
        this.holderAddress = holderAddress;
    }
    public String getOpenDate() {
        return openDate;
    }
    public void setOpenDate(String openDate) {
        this.openDate = openDate;
    }

    public double getCurrentBalance() {
        return currentBalance;
    }
    public void setCurrentBalance(double currentBalance) {
        this.currentBalance = currentBalance;
    }

    public List<Transaction> getTransactions() {
        return transactions;
    }

    public void setTransactions(List<Transaction> transactions) {
        this.transactions = transactions;
    }

    public void addTransaction(Transaction transaction){
      if(transactions.size() >= 6){  // test if the list has 6 or more transactions saved 
      transactions.remove(0);     // if so, then remove the first (it's the oldest)
     }
     transactions.add(transaction); // the new transaction is always added, no matter how many other transactions there are already in the list
     }

    public String toString(){
        return "\nAccount number: " + accountId + 
                "\nHolder's name: " + holderName + 
                "\nHolder's address: " + holderAddress + 
                "\nOpen Date: " + openDate + 
                "\nCurrent balance: " + currentBalance;
    }


}

3 个答案:

答案 0 :(得分:6)

您正在呼叫Transaction(bankAccount.getTransactions().size(), "Deposit", depositAmount, currentBalance);这意味着您正在通过four arguments

但您创建的Transaction( String transactionType, int transactionDate, double transactionAmount)仅接受three arguments

根据您的需要,我猜您可能需要在构造函数定义中添加一个参数来存储值Deposit,或者在调用构造函数时将其删除(如果您不需要它)。

如果你想要三个参数:

更改程序中的以下代码行:

Transaction transaction = new Transaction(bankAccount.getTransactions().size(), depositAmount, currentBalance);

根据我的评论如下:

  

您需要验证要传递的所有参数的数据类型。它肯定意味着需要String并且你传递一个int。我怀疑它是否与 bankAccount.getTransactions()。size()参数有关,因为 size()应该为您提供 int 值但是在构造函数中,您需要 String transactionType 字符串值。您必须根据需要更改类型。 并提示将int值转换为String,您可以使用String.valueOf();

答案 1 :(得分:3)

你有的构造函数

Transaction(String, int, double);

但你正试图给予

 Transaction(int, String, int, double);

修改以下内容,

Transaction transaction = new Transaction("Deposit", depositAmount, currentBalance);

而不是

Transaction transaction = new Transaction(bankAccount.getTransactions().size(), "Deposit", depositAmount, currentBalance);

使用以下内容替换您的Transaction类。

 public class Transaction {


      private String transactionType;
      private double transactionAmount;
      private double balanceAmount;
      private Date transactionDate;


      public Transaction() {}

      public Transaction( String transactionType, Date transactionDate, double transactionAmount, double balanceAmount) {

        this.transactionType = transactionType;
        this.transactionAmount = transactionAmount;
        this.transactionDate = transactionDate;
        this.balanceAmount = balanceAmount;
     }

       public Transaction( String transactionType, double transactionAmount, double balanceAmount) {

    this.transactionType = transactionType;
    this.transactionAmount = transactionAmount;
    this.balanceAmount = balanceAmount;

  }

      public String getTransactionType() {
        return transactionType;
      }

      public void setTransactionType(String transactionType) {
        this.transactionType = transactionType;
      }

      public double getTransactionAmount() {
        return transactionAmount;
      }

      public void setTransactionAmount(double transactionAmount) {
        this.transactionAmount = transactionAmount;
      }


      //Override the toString() method of String ? 
      public String toString() {
        return " Transaction Amount : "+ this.transactionAmount +
               " Transaction Type : " + this.transactionType +
               " Balance Amount : " + this.balanceAmount +
               " Transaction Date:  " + this.transactionDate;

      }

        /**
         * @return the balanceAmount
         */
        public double getBalanceAmount()
        {
            return balanceAmount;
        }

        /**
         * @param balanceAmount the balanceAmount to set
         */
        public void setBalanceAmount(double balanceAmount)
        {
            this.balanceAmount = balanceAmount;
        }

    }

打印交易方法

public void printTransaction(int accountNumberId)
  {
      List<Transaction> transactionList = getTransactions(accountNumberId); // this getTransactions() you have to create and fetch all the transaction listed by the holderName

      for(Transaction transaction : transactionList)
      {
          System.out.println("Transaction Amount : "+transaction.getTransactionAmount());
          System.out.println("Transaction Date : "+transaction.getTransactionDate());
          System.out.println("Transaction Type : "+transaction.getTransactionType());
      }
  }

答案 2 :(得分:0)

错误原因是您有以下构造函数用于事务类:

public Transaction() {}
public Transaction( String transactionType, int transactionDate, double transactionAmount)

你传递的4个参数不符合其中任何一个。而是尝试:

 Transaction transaction = new Transaction("Deposit", 0, depositAmount);

由于bankAccount具有currentBalance详细信息,因此您无需传递这些信息。此外,没有必要在您的情况下传递事务的大小,因为我在您的代码中没有看到任何目的。