Friday, April 17, 2009

Write a program to display the withdrawal status to the user

Write a program to display the withdrawal status to the user. If the account number is valid, display the withdrawal screen to the user. The withdrawal screen should display the balance entered by the user on the withdrawal status screen and ask the user to enter the amount to be withdrawn. Display the balance to the user after withdrawing the amount.

Solution:
//withdraw.jsp
<html>
<head>
<title>MARKO</title>
</head>
<body>
<H1 align="center">MARKO Amount Withdrawal Screen</H1>
<%
String amt = request.getParameter("amt") ;
String balance = request.getParameter("bal");
if (amt == null)
{
%>
<form method="post">
<h3>
Your balance is <input type='text' readonly name='bal'
value='<%=balance%>'><br>
Please enter the amount to be withdrawn <input
type="text"
name="amt">
<input type="submit" value="Withdraw">
</form>
<%
}
else
{
%>
<jsp:useBean id="objW" class="MARKO.AccountBean"
scope="page" />
<jsp:setProperty name="objW" property="balance"
value="<%=balance%>"/>
<jsp:setProperty name="objW" property="amount"
value="<%=amt%>"/>
<H3 align="center">Rs. <%=amt%> withdrawn from your
account
the new balance is <jsp:getProperty name="objW"
property="balance"/>
</h3>
<%
}
%>
</body>
</html>
1. Save the file as withdraw.jsp in basic directory.

2. Open Notepad and enter the following code.
//AccountBean.java
package MARKO;
public class AccountBean implements java.io.Serializable
{
private int amount = 0;
private int balance = 0 ;
public String getAmount()// throws Exception
{
return String.valueOf(amount);
}
public void setAmount(String strAmt)// throws Exception
{
this.amount=Integer.parseInt(strAmt);
}
public String getBalance()
{
if (balance < amount)
{
return "Balance is not sufficient to withdraw money.";
}
else
{
balance = balance - amount;
}
return ("Rs."+String.valueOf(amount)+" withdrawn from your
account. The new balance is Rs"+String.valueOf(balance));
}
public void setBalance(String strBal)
{
balance = Integer.parseInt(strBal);
}
}
3. Save the file as AccountBean.java in Session8 directory.

4. Stop and restart Tomcat server.

5. Open Internet Explorer and enter http://localhost:8080/Session8/withdraw.jsp in the browser.

The output appears as shown in Figure 8.6.

Figure 8.6: Withdraw Amount Screen

Enter an amount greater than the balance amount. A message will be displayed to the user informing that balance amount is insufficient.

Figure 8.7: Output with invalid amount

Enter the amount to be withdrawn and click the Withdraw button. The output appears as shown in Figure 8.7 when you click on Withdraw button.

Figure 8.8: Output with valid amount

0 nhận xét: