1

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 an ArrayList and set up 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 NetBeans.

The hints don't seem to be fixing the errors. I get the error:

Transaction is abstract and cannot be instantiated.

How can I fix this problem?

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");
        }
    }
4

1 回答 1

4

构造函数没有返回类型。所以不是

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

反而

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

关于,

事务是抽象的,不能实例化

嗯,是吗?Transaction 类是抽象类还是接口?只有拥有代码的你才知道答案。如果这是真的,那么您需要在代码中使用 Transaction 的具体实现。

于 2014-11-02T00:42:01.997 回答