Java constructor not compiling properly

2019-01-08 02:29发布

问题:

I'm a newbie to Java and just started classes recently so still getting the hang of how everything works in it so please bear with me while I try to comprehend all this new material.

I have an assignment that requires a bank account to be able to transfer funds from a checking and savings account. The transactions are stored in arraylist and setup for the user to specify when to transfer the funds. The bank account class for checking and savings works ok but the transferservice class I created isn't compiling properly in my ide netbeans I'm using.

My instructor reworked some of my code to help but it still won't compile and the hints don't seem to be fixing the errors. I get Transaction is abstract and cannot be instantiated. Not quite sure what I can do to fix that error so any help would be greatly appreciated.

import java.util.ArrayList;
import java.util.Date;
import javax.transaction.Transaction;

public class TransferService {
    private Date currentDate;
    private ArrayList<Transaction> completedTransactions;
    private ArrayList<Transaction> pendingTransactions;

    public void TransferService(){
        this.currentDate = new Date();
        this.completedTransactions = new ArrayList<Transaction>();
        this.pendingTransactions = new ArrayList<Transaction>();
    }   

    public TransferService(BankAccount to, BankAccount from, double amount, Date when) throws InsufficientFundsException(){
        if (currentDate.after(when)){
            try(
            from.withdrawal(amount);
            to.deposit(amount);
            completedTransactions.add(new Transaction(to, from, this.currentDate, Transaction.TransactionStatus.COMPLETE));
            } catch (InsufficientFundsException ex){
                throw ex;
            }
        } else {
            pendingTransactions.add(new Transaction(to, from, null, Transaction.TransactionStatus.PENDING));
        }
    }

    private static class InsufficientFundsException extends Exception {

        public InsufficientFundsException() {
            System.out.println("Insufficient funds for transaction");
        }
    }

回答1:

Constructors have no return type. So not

// this is a "pseudo"-constructor
public void TransferService(){

but rather

// this is the real deal
public TransferService(){

Regarding,

Transaction is abstract and cannot be instantiated

Well, is it? Is the Transaction class an abstract class or an interface? Only you who has the code knows the answer to this. If this is true, then you'll need to use a concrete implementation of Transaction in your code.