java - “找不到构造函数......”错误

标签 java

H.我在这段代码上遇到错误,并且不知道该怎么做。谁能告诉我问题可能是什么以及如何解决。

错误: 没有找到适合 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;
    }


}

最佳答案

您正在调用 Transaction(bankAccount.getTransactions().size(), "Deposit", DepositAmount, currentBalance); 这意味着您正在传递四个参数

但是您已经创建了仅接受三个参数Transaction( String transactionType, int transactionDate, double transactionAmount)

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

如果你想要三个参数:

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

交易 transaction = new Transaction(bankAccount.getTransactions().size(), DepositAmount, currentBalance);

根据我的评论:

You need to verify the data types of all the parameters you are passing. It certainly means String is needed and you are passing an int. And I doubt if it is regarding the bankAccount.getTransactions().size() parameter as size() is supposed to give you int value but in your constructor you need String transactionType String value. You will have to change the types as per your need. And a hint to convert int value to String you can use String.valueOf();

关于java - “找不到构造函数......”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27520288/

相关文章:

java - 我的数组中出现 NullPointerException

java - 在 z3 中表示推理规则

java - 一个 Java 文件中的多个枚举类

java - 使用 Spring + 没有为参数指定值调用 Postgres 存储过程

java - 将整数元素添加到列表 JAVA

java - 不幸的是(android应用程序)已经停止了。空指针异常

java - 获取谷歌搜索结果是否存在的信息(JAVA)

java - 以 Integer[] 作为键搜索 Hashmap

java - Apache Commons Lang : Upgrade from 2. 3 到 3.3.1 - UnhandledException 怎么样

java - 在 Android SQLite 中将许多嵌套的 JSON 对象和数组存储为数据结构的好方法?