接口,子类,继承

时间:2014-02-17 03:25:42

标签: java

我一直致力于实现银行交易处理器的任务。我正在尝试在应用负面存款时打印错误消息。 代码的开头

import java.util.ArrayList ;

public class TransactionProcessorTest
{
    public static void main (String[] args)
    {
        // the account list
        ArrayList<BankAccount> bankAccounts = new ArrayList<>();

        bankAccounts.add(new CheckingAccount    ("chk001",  500.00)) ;
        bankAccounts.add(new SavingsAccount     ("sav001", 5000.00)) ;
        bankAccounts.add(new CheckingAccount    ("chk002",  299.00)) ;
        bankAccounts.add(new SavingsAccount     ("sav002", 9049.00)) ;

        // trhe transation list
        ArrayList<Transaction> transactionList = new ArrayList<>() ;

            // deposit into each type of account
        transactionList.add(new DepositTransaction   ("chk001",  500.00)) ;
        transactionList.add(new DepositTransaction   ("sav001", 5000.00)) ;
        transactionList.add(new DepositTransaction   ("loc001",  350.00)) ;


        // the transaction processor
//      TransactionProcessor transProcessor = new TransactionProcessor() ;

        // print out the bank accounts before applying the transactions
        System.out.println("Bank Account Balances Before Processing Transactions:") ;
        System.out.println("=====================================================") ;
        for (BankAccount acct : bankAccounts)
            System.out.println(acct) ;
        System.out.println("\n") ;

        // process the transactions
        System.out.println("Processing Transactions:") ;
        System.out.println("========================") ;

//      boolean clean = transProcessor.processTransactions(bankAccounts,transactionList) ;
// remove the following line if you uncomment the previous line
boolean clean = true ;

for(Transaction t : transactionList)
    System.out.println(t);

BankAccount test = bankAccounts.get(0)
           ,test1 = bankAccounts.get(1);
Transaction t0 = transactionList.get(0)
           ,t1 = transactionList.get(1)
           ,t3 = transactionList.get(3);

t0.applyTransaction(test);
t3.applyTransaction(test);
t1.applyTransaction(test1);



        System.out.printf("\nThere were %s encountered during transaction processing.%n"
                         ,(clean ? "no errors" : "ERRORS")
                         ) ;

        System.out.println("\n") ;

        // print out the bank accounts after applying the transactions
        System.out.println("Bank Account Balances After Processing Transactions:") ;
        System.out.println("====================================================") ;
        for (BankAccount acct : bankAccounts)
            System.out.println(acct) ;

        return ;
    }
}

//===================
// Bank Account Types
//===================

interface Checkable
{
    // tagging interface

}

interface Savable
{
    // tagging interface

}

interface Borrowable
{
    // tagging interface

}

abstract class BankAccount
{
    private String accountNumber  =   "" ;
    private double accountBalance = 0.00 ;

    public BankAccount (String pAccountNumber, double pInitialBalance)
    {
        accountNumber  = pAccountNumber  ;
        accountBalance = pInitialBalance ;

        return ;
    }

    // getters
    public String getAccountNumber  () { return accountNumber   ;}
    public double getAccountBalance () { return accountBalance  ;}

    // mutator
    boolean updateBalance (double amount)
    {
        boolean retCode = false ;

        accountBalance += amount;

        return retCode ;
    }

    // predicates
    public boolean acceptsChecks        () { return false ; }
    public boolean acceptsWires         () { return false ; }
    public boolean acceptsDeposit       () { return false ; }
    public boolean acceptsWithdrawal    () { return false ; }

    // string-izer
    @Override
    public String toString ()
    {


        return "account#: " + getAccountNumber() + "; balance: " + getAccountBalance() ;
    }
}

class CheckingAccount extends BankAccount implements Checkable
{
    public CheckingAccount (String pAccountNumber, double pInitialBalance)
    {
        super(pAccountNumber,pInitialBalance) ;

        return ;
    }

    // mutator

    boolean updateBalance (double amount)
    {
        boolean retCode = false ;

        super.updateBalance(amount);

        return retCode ;
    }



    // predicates
    @Override
    public boolean acceptsChecks        ()  { return true ; }
    @Override
    public boolean acceptsWires         ()  { return true ; }
    @Override
    public boolean acceptsDeposit       ()  { return true ; }
    @Override
    public boolean acceptsWithdrawal    ()  { return true ; }

    // string-izer
    @Override
    public String toString () { return "Checking " + super.toString() ; }
}

以下是Transactable接口。

interface Transactable
{
    boolean applyTransaction (BankAccount pAccount) ;
}

abstract class Transaction implements Transactable
{
    private String accountNumber =   "" ;
    private double amount        = 0.00 ;

    public Transaction (String pAccountNumber, double pAmount)
    {
        accountNumber = pAccountNumber ;
        amount        = pAmount        ;

        return ;
    }

    // getters
    public String getAccountNumber  () {    return accountNumber    ;}
    public double getAmount         () {    return amount           ;}

    // string-izer
    @Override
    public String toString ()
    {
        return "account#: " + getAccountNumber() + "; balance: " + getAmount() ;
    }

}

这是我遇到问题的部分。当我在检查余额是否小于0.00后尝试打印错误消息时,错误消息不会立即打印在负数下。

    class DepositTransaction extends Transaction
{
    public DepositTransaction (String pAccountNumber, double pAmount)
    {
        super(pAccountNumber,pAmount) ;

        return ;
    }

    @Override
    public boolean applyTransaction (BankAccount pAccount)
    {
        boolean     retCode = false ;

        if(super.getAmount() > 0.00) {  
            retCode = true;     
            pAccount.updateBalance(getAmount()); 
        }
        else {  
            retCode = false;    
            System.out.println("Account doesn't accept negative amount");
        }
        return retCode ;
    }



    // string-izer
    @Override
    public String toString () { return "Deposit    " + super.toString() ; }

}

这是我目前的截图

Checking account#: chk001; balance: 500.0
Saving account#: sav001; balance: 5000.0
Checking account#: chk002; balance: 299.0
Saving account#: sav002; balance: 9049.0


Processing Transactions:
========================
Deposit    account#: chk001; balance: 500.0
Deposit    account#: sav001; balance: 5000.0
Deposit    account#: loc001; balance: 350.0
Deposit    account#: chk001; balance: -10.0
Deposit    account#: CHK001; balance: 10.0
Account doesn't accept negative amount

There were no errors encountered during transaction processing.


Bank Account Balances After Processing Transactions:
====================================================
Checking account#: chk001; balance: 1000.0
Saving account#: sav001; balance: 10000.0
Checking account#: chk002; balance: 299.0
Saving account#: sav002; balance: 9049.0

我希望能够在负数下打印错误消息。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

main方法中查看此块。

for(Transaction t : transactionList)
    System.out.println(t);

BankAccount test = bankAccounts.get(0)
           ,test1 = bankAccounts.get(1);
Transaction t0 = transactionList.get(0)
           ,t1 = transactionList.get(1)
           ,t3 = transactionList.get(3);

t0.applyTransaction(test);
t3.applyTransaction(test);
t1.applyTransaction(test1);

前两行打印出交易。之后,您应用它们,这是产生错误消息的原因。因此,您可以打印出所有交易,然后输出错误。

在我看来,您想重新安排此部分,以便依次打印出每个交易然后应用。