user4359073
user4359073

Reputation:

'Constructor not found....' error

H. I am getting an error on this piece of code and don't have a clue what to do. Can anyone tell me what the problem could be and how to resolve it.

Error: No suitable constructor found for Transaction (int, String, double, double(

Code underlined:

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


}

Upvotes: 1

Views: 1433

Answers (3)

gprathour
gprathour

Reputation: 15333

You are calling Transaction(bankAccount.getTransactions().size(), "Deposit", depositAmount, currentBalance); This means you are passing four arguments.

But you have created Transaction( String transactionType, int transactionDate, double transactionAmount) which accepts only three arguments.

According to your need I guess you may want to add one more parameter to your constructor definition to store the value Deposit or remove it while calling the constructor if you don't need it.

If you want three arguments:

Change the following line of code in your program:

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

As per my comment below:

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

Upvotes: 5

newuser
newuser

Reputation: 8466

Constructor you have

Transaction(String, int, double);

But you are trying to give

 Transaction(int, String, int, double);

Modify the following,

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

instead of

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

Replace your Transaction class by the following.

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

    }

Print Transaction method

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

Upvotes: 3

SMA
SMA

Reputation: 37023

Reason for the error is that you have following constructor for transaction class:

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

And you are passing 4 argument which doesnt match any of these. Instead try:

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

As bankAccount has currentBalance details, you dont need to pass those. Also there is no need to pass the size of transaction in your case as i dont see any purpose in your code.

Upvotes: 0

Related Questions