📄 performtransactionaction.txt
字号:
import javax.servlet.http.HttpSession;
import itso.bank.exception.*;
import itso.bank.facade.*;
import itso.bank.model.*;
import itso.bank.util.*;
import java.math.BigDecimal;
import java.util.*;
// helper method to test that account belongs to customer
private boolean testAccountOfCustomer(Account[] custAccts, Account acct) {
for (int i=0; i<custAccts.length; i++) {
if ( custAccts[i].getId().equals(acct.getId()) ) return true;
}
return false;
}
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward();
// return value
TransactionForm transactionForm = (TransactionForm) form;
// Check if the Cancel button was pressed
if (isCancelled(request)) {
return mapping.findForward("cancel");
}
// Get the accountNumber from the session
HttpSession session = request.getSession(false);
String customerNumber= (String) session.getAttribute("customerNumber");
String accountNumber = (String) session.getAttribute("accountNumber");
// Get the form bean data
String action = transactionForm.getAction().trim();
String destinationAccountNumber =
(String) transactionForm.getDestinationAccount().trim();
String amountString = transactionForm.getAmount().trim();
Customer customer = null;
Account account = null;
// Reset the TransactionForm Form Bean to default values by
// creating a new one. This one we can populate with current
// data and display in the JSP
transactionForm = new TransactionForm();
try {
// Get the banking fa鏰de and the Account to work with
Banking banking = new Banking();
customer = banking.getCustomer(customerNumber);
account = banking.getAccount(accountNumber);
Account[] customerAccounts = banking.getAccounts(customerNumber);
// Check the account
// Perform the requested transaction.
// If successful set the correct forward
if ( !testAccountOfCustomer(customerAccounts, account) )
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error.accountNotOfCustomer"));
else if ("ListTransactions".equals(action)) {
transactionForm.setTransactions( banking.getTransactions(accountNumber) );
forward = mapping.findForward("listTransactions");
} else if ("Deposit".equals(action)) {
BigDecimal amount = AmountConverter.fromString(amountString);
banking.deposit(accountNumber, amount);
forward = mapping.findForward("deposit");
} else if ("Withdraw".equals(action)) {
BigDecimal amount = AmountConverter.fromString(amountString);
banking.withdraw(accountNumber, amount);
forward = mapping.findForward("withdraw");
} else if ("Transfer".equals(action)) {
BigDecimal amount = AmountConverter.fromString(amountString);
if ( !testAccountOfCustomer(customerAccounts, banking.getAccount(destinationAccountNumber) ))
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("error.accountNotOfCustomer"));
else {
banking.transfer(accountNumber, destinationAccountNumber, amount);
forward = mapping.findForward("transfer");
}
}
// retrieve account again to get the latest balance
account = banking.getAccount(accountNumber);
} catch (BankException e) {
// If an error occurs during the transaction add the error describing
// the problem to the errors object. The exceptions return the key
// for the error in their getMessageKey() methods.
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError(e.getMessageKey()));
}
// If an error occured, save the errors back to the request and make
// sure we return to the input page so the user can correct the entry.
// Also set the values in the TransactionForm Form Bean so they are
// available to the user and easy to correct.
if (!errors.empty()) {
saveErrors(request, errors);
forward = mapping.getInputForward();
transactionForm.setAction(action);
transactionForm.setAmount(amountString);
transactionForm.setDestinationAccount(destinationAccountNumber);
}
// Update the transactionForm with current values after the transaction
if (account != null) {
transactionForm.setAccountID(account.getId());
transactionForm.setAccountBalance(account.getBalance() + "");
}
// Determine the scope where it should be stored
if ("request".equals(mapping.getScope()))
request.setAttribute("transactionForm", transactionForm);
else
session.setAttribute("transactionForm", transactionForm);
// Finish with
return (forward);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -