Why won't my public void Constructor {} compile?

痞子三分冷 提交于 2019-12-17 06:18:09

问题


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

回答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.



来源:https://stackoverflow.com/questions/26694833/why-wont-my-public-void-constructor-compile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!