我一直在做一個實現銀行交易處理器的任務。我正在嘗試在應用負數存款時打印錯誤消息。 開始的代碼接口,子類,繼承
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
我希望能夠打印權下的負數金額的錯誤消息。
任何幫助表示讚賞。
不要開始將所有塊代碼放在一行中,這是非常難以閱讀。 –
請不要使用'double'數據類型來存儲金額。如果你這樣做,壞事等着你。 –
double由教練給出,我們不能改變它.. –