i need a little help here, what i need to do is create a ticket purchasing page where once the user has made a selection he/she is prompted for amount .And to pay that amount buttons are used .every time a button is clicked the amount reduces. I also need to show a thank you message and if the amount reaches zero otherwise a message stating change is displayed.
i have worked out the following code but contains many errors.i have been using servlets and jsp which is a requirement
Can anyone help whats wrong with my code?i need someone to fix my code for me ,i have spent hours trying to figure out what can possibly be wrong with the code .
import java.io.*;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;
public class TicketPurchase2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException,NullPointerException
{
try {
HttpSession session = request.getSession(true);
response.setContentType("text/html;charset=UTF-8");
ServletOutputStream out = response.getOutputStream();
// creating DAO object
DAO dao = new DAO();
String title = "Transport for London - Ticket Purchase";
String h1 = "Ticket Purchase Status";
String docType = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN''http://www.w3.org/TR/html4/loose.dtd'>";
out.println(docType);
out.println("<html>");
out.println("<head><title>" + title + "</title></head>");
out.println("<body style='background-color: #696 ; text-align: center;'>");
out.println("<script language='javascript' type='text/javascript'>" +
"function changeAmt(amount){" +
"document.getElementById('pay').value = amount;"+
"document.forms['promptAmount'].submit();"+
"}"+
"</script>");
out.println("<h1 style='text-align:center'>" + h1 + "</h1>");
//extracts the zone and fare information
String zone = request.getParameter("zone");
String fareType = request.getParameter("fareType");
//display zone and fare information in table
out.println("<table align='center' border='2px' width='500px'>\n" +
"<tr>\n" +
" <th>Zone</th> <th>Fare Type</th>\n" +
" <th>Total Cost</th> <th>Update Ticket</th></tr>");
out.println("<tr>\n" +
" <td>" + zone + "</td>\n" +
" <td>" + fareType + "</td>\n" +
" <td>"+ "£ " + dao.getCash(zone,fareType) + "0" + "</td>\n" +
" <td> <a href='TicketPurchase1'>Update</a></td>\n");
out.println("</table><br/>");
Transaction transaction = (Transaction)request.getAttribute("T1");
//amount payment
out.println("<form name='promptAmount' method='get' action='Relay'>");
out.print("<input type='hidden' id='pay' name='pay' value='0'>");
out.println("<input type='hidden' name='command' value='TicketPurchase3'/>");
out.println("<h3> Fare Amount :</h3>");
out.println("<input type='text' name='CashAmount' id='CashAmount' readonly='readonly' size='15px' value='"+
dao.getCash(zone,fareType)+"'/><br/>");
out.println("<h3> Amount Paid:</h3>");
out.println("<input type='text' name='amountpaid' id='amountpaid' readonly='readonly' size='15px' value='"+
transaction.getAmountpaid()+"'/>");
out.println("<h3>Remaining Amount:</h3>");
out.println("<input type='text' name='remainingamount' id='remainingamount' readonly='readonly' value='"+
transaction.getRemainAmount() +"'/><br/><br/>");
out.println("<input type='submit' name='5p' id='5p' value='5p' onClick='changeAmt('5');'/>");
out.println("<input type='submit' name='10p' id='10p' value='10p' onClick='changeAmt('10');'/>");
out.println("<input type='submit' name='20p' id='20p' value='20p' onClick='changeAmt('20');'/>");
out.println("<input type='submit' name='50p' id='50p' value='50p' onClick='changeAmt('50');'/>");
out.println("<input type='submit' name='1pound' id='1pound' value='1 £' onClick='changeAmt('100');'/>");
out.println("<input type='submit' name='2pound' id='2pound' value='2 £' onClick='changeAmt('200');'/>");
out.println("</form><br/>");
out.println("Return to " + "<a href='Home.html'>Home Page</a>");
out.println("</body></html>");
out.close();
out.println("<input type='hidden' name='zone' value='"+ zone +"'/>");
out.println("<input type='hidden' name='fareType' value='"+ fareType +"'/>");
}
catch (SQLException ex)
{
Logger.getLogger(TicketPurchase2.class.getName()).log(Level.SEVERE, null, ex);
}
catch (ClassNotFoundException ex)
{
Logger.getLogger(TicketPurchase2.class.getName()).log(Level.SEVERE, null, ex);
}
catch(NullPointerException ex)
{
System.out.println(ex);
}
}
}
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TicketPurchase3 extends HttpServlet
{
// creating DAO object
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException,NullPointerException
{
PrintWriter out = response.getWriter();
String webpage= "Relay?command=TicketPurchase2&&zone="+
request.getParameter("zone") +"&fareType="+
request.getParameter("fareType")+"&Purchase=Purchase";
/* { I dont know what url to use to correctly dispatch the data } */
try
{
// if(){}
// get fare amount
float amount=0f;
float amtPaid=0f;
float remainingamount = 0f;
float payamt = 0f;
amount=Float.parseFloat(request.getParameter("CashAmount"));
if(request.getParameter("pay") != null && request.getParameter("pay").length() > 0)
{
payamt = Float.parseFloat(request.getParameter("pay"))/ 100;
}
if(request.getParameter("amountpaid") != null && request.getParameter("amountpaid").length() > 0){
amtPaid = Float.parseFloat(request.getParameter("amountpaid"));
}
if(request.getParameter("remainingamount") != null && request.getParameter("remainingamount").length() > 0){
remainingamount = Float.parseFloat(request.getParameter("remainingamount"));
}
if (amount - (amtPaid + payamt) > 0){
Transaction t1 = new Transaction((amtPaid + payamt), amount - (amtPaid + payamt));
request.setAttribute("T1", t1);
getServletConfig().getServletContext().getRequestDispatcher(webpage).forward(request, response);
}else{
remainingamount = amount - (amtPaid + payamt);
// dao.saveTransaction((amountpaid + payamt), fareamount, farename, remainingamount, fares.getZonename());
if(remainingamount == 0){
out.println("Thank You <br/><a href=\"./\"> Back to Main </a>");
}else if(remainingamount < 0){
out.println("The change and the individual coins "+ remainingamount +" <br/><a href=\"./\"> Back to Main </a>");
}
}
}
catch (ServletException ex)
{
System.out.println(ex);
}
catch(NullPointerException ex)
{
System.out.println(ex);
}catch(IOException ex)
{
System.out.println(ex);
}
public class Transaction {
private float AmountPaid;
private float RemainingAmount;
public Transaction()
{}
/**Manage transactions*/
public Transaction(float amountpaid, float remainingamount)
{
AmountPaid = amountpaid;
RemainingAmount = remainingamount;
}
public void setAmountpaid(float amountpaid)
{
AmountPaid = amountpaid;
}
public float getAmountpaid()
{
return AmountPaid;
}
public void setRemainAmount(float remainingamount)
{
RemainingAmount = remainingamount;
}
public float getRemainAmount()
{
return RemainingAmount;
}
}
finally
{
out.close();
}//end of finally block
}//end of doGet()
}//end of class FareDisplay