H. Я получаю сообщение об ошибке в этом фрагменте кода и не знаю, что делать. Может ли кто-нибудь сказать мне, что может быть проблемой и как ее решить.Ошибка 'Constructor not found ....'
Ошибка: Нет подходящий конструктор найдено для транзакции (междунар, String, двойной, двойной (
код не указывается:
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;
}
}
Это не конструктор, который принимает четыре аргумента. Единственный конструктор, который вы дали, содержит три аргумента. – Jared