2014-11-02 108 views
1

我是Java的新手,最近刚刚开始上课,所以仍然掌握了一切如何工作的原因,所以请在我尝试理解所有这些新材料的时候忍受。Java构造函数没有正确编译

我有一项任务,需要一个银行账户能够从一个检查和储蓄账户转移资金。交易存储在数组列表中,并设置为用户指定何时转移资金。用于检查和储蓄的银行帐户类可以正常工作,但我创建的transferservice类不能在我使用的ide netbeans中正确编译。

我的导师重写了一些我的代码来帮助,但它仍然不会编译,提示似乎没有修复错误。我得到的交易是抽象的,不能实例化。不太确定我能做些什么来解决这个错误,所以任何帮助将不胜感激。

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

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

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

而是

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

关于,

交易是抽象的,不能被实例化

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

+0

好吧我想我明白你的意思了。我认为这不是一个抽象类,因为我没有一个设置。我的猜测是,我需要为事务设置一个单独的类,然后才能在TransferService方法和arraylist中实例化或声明使用它? – MrLevel81 2014-11-03 00:34:15