Home

 

Implementing deposit and withdraw

We want to be able to deposit and withdraw funds into and from an account. We implement these actions in the accountDetails.jsp:

Click in the account table and select Table Æ Add Column to Right. Add three columns.

Select the third cell and set the width to 30 pixels in the Properties view.

Drag an Output component into cell four, with text Amount (- for withdraw):.

Drag an Input component into cell four/row two. Set the id to amountstring.

Drag a Button - Command component into cell five of row two. Set the id to process, and the label to Deposit/Withdraw.

In the Page Data view, create a request scope variable with the name amount (String). Then drag the amount variable onto the input field to create a binding of #{requestScope.amount}.

Select the Deposit/Withdraw button and go to the Quick Edit view to generate an action method named doProcessAction.

Complete the doProcessAction method in the page code (refer to 7672code\jsf\doProcessAction.txt, Example | 6-5).

Example 16-5 Deposit and withdraw action logic

public String doProcessAction() {
	String amountstring = (String)getRequestScope().get("amount");
	System.out.println("deposit/withdraw amount: " + amountstring);
	try {
		BigDecimal amount = new BigDecimal(amountstring);
		if (amount.scale() > 2) throw 
					new Exception("Only 2 digits allowed for cents");
		Account account = getAccount();
		BigDecimal balance = account.getBalance();
		if (amount.doubleValue() == 0) {
			throw new Exception("Amount is zero");
		} else if (amount.doubleValue() > 0) {
			balance = balance.add(amount);
		} else {
			if (balance.compareTo(amount.abs()) < 0) 
							throw new Exception("Withdraw amount too big");
			balance = balance.add(amount);
		}
		account.setBalance(balance);
		AccountManager accountManager = (AccountManager)
											getManagedBean("accountManager");
		accountManager.updateAccount(account);
		System.out.println("deposit/withdraw balance: " + balance);
		// create transaction
		TransactionManager transactionManager = (TransactionManager)
											getManagedBean("transactionManager");
		Transaction t = new Transaction();
		t.setId( (new com.ibm.ejs.util.Uuid()).toString() );
		t.setAmount(amount.abs());
		t.setTransTime( new Timestamp(System.currentTimeMillis()) );
		if (amount.doubleValue() > 0) t.setTransType("Credit");
		else t.setTransType("Debit");
		t.setAccount(account);
		transactionManager.createTransaction(t);
		transactionList = null;
		getTransactionList();
		getRequestScope().put("amount","");
	} catch (NumberFormatException e) {
		getFacesContext().addMessage("amount", 
			new FacesMessage("Bad amount"));
	} catch (Exception e) {
		getFacesContext().addMessage("amount", 
			new FacesMessage("Deposit/withdraw failed: " + e.getMessage()));
	}
	return "";
}

Make sure that you import java.math.BigDecimal and java.sql.Timestamp.

This action logic performs the following steps:

Retrieve the input amount.
Verify that a withdraw amount does not exceed the balance.
Change the balance and call the AccountManager to update the database.
Build a transaction record and call the TransactionManager to insert the record into the database.
Retrieve the transaction records again to display the new record in the list.
Issue JSF error messages for bad data.
ibm.com/redbooks