import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
// This an online banking program.  It has two modes:  The user authentification
// mode and the transaction processing mode.  Once the user is authorized she
// can make repeated withdrawls and deposits.  When done she hits cancel bringing
// up the registration screen so other users may log in.


public class Bank07 extends HttpServlet {
    
   public void doGet (HttpServletRequest request,
                       HttpServletResponse response) 
                           throws ServletException, IOException{
      
      double balance;
      double amount;
      PrintWriter out;
         
      response.setContentType("text/html");

      out = response.getWriter();
      Bankjdbc03 bank=new Bankjdbc03();

// now we get parameters passed via forms.  Not of all these will be non-null

      String username = request.getParameter("username");
      String password = request.getParameter("password");
      String check = request.getParameter("check");
      String amountstring = request.getParameter("amount");
      String deposit = request.getParameter("deposit");
      String withdraw = request.getParameter("withdraw");
      String cancel = request.getParameter("cancel");
            
      //  out.println("<p> username=" + username);
      //  out.println("<p> password=" + password);
      //  out.println("<p> check=" + check);
      //  out.println("<p> amountstring=" + amountstring);
      //  out.println("<p> deposit=" + deposit);
      //  out.println("<p> withdraw=" + withdraw);
      //  out.println("<p> cancel=" + cancel);

              
      out.println("<HTML><HEAD><TITLE>Hometown Bank</TITLE> " +
      "</HEAD><BODY><H1 ALIGN=center>Hometown Bank</H1>" );

// if we don't have a valid account then print the Check Balance form

      if (username==null || password==null || cancel!=null){
          out.println("<FORM ACTION=Bank07  METHOD=POST  " + 
          "<P><P>Enter userID: " +
          "<INPUT TYPE=text NAME=username > " +
          "Enter password: <INPUT TYPE=password NAME=password >" +
          "<P></BODY></HTML> ");
          out.println("<P> " +
          "<INPUT TYPE=submit NAME='check' VALUE='Check Balance'> " +
          "<INPUT TYPE=submit NAME=cancel VALUE=Cancel></FORM> ");
          out.println("</BODY></HTML> ");
      }

// if we do have username and passward given then process transaction and
// print Deposit/Withdraw form.

      else {
         balance=bank.getBalance(username,password);
         if (balance < 0) {
            out.println("<p><b> Sorry, you entered an invalid username or password</b>");
            out.println("<form method=post action=Bank07>" +
            "<input type=submit value='try again!'> "+ 
            "</form></body></html>");
            out.close();
            return;
         }

// extra care is needed to convert amout from string to double

         if (amountstring !=null){
            if (amountstring.trim().length()==0){
               amount=0;
            }
            else {
               amount = Double.valueOf(amountstring.trim()).doubleValue();
            }
         }
         else {
            amount=0;
         }


         if (deposit!=null) {
            balance=balance + amount;
            bank.setBalance(username,password,balance);
            out.println("<p><b> $" + amountstring + " deposited</b> ");

         }
         if (withdraw!=null) {
            balance=balance-amount;
            bank.setBalance(username,password,balance);
            out.println("<p><b> $" + amountstring + " withdrawn</b> ");
         }
         

         out.println("<p><b> Hi " + username + "</b>");
         out.println("<p><b>Balance: $" + balance + "</b> "); 
         out.println("<FORM ACTION=Bank07 METHOD=POST ><P> Enter Amount:" + 
         "<INPUT TYPE=text NAME=amount ><P> " +
         "<INPUT TYPE=submit NAME=deposit VALUE=Deposit> " + 
         "<INPUT TYPE=submit  NAME=withdraw VALUE=Withdraw> " +
         "<INPUT TYPE=submit NAME=cancel VALUE=Cancel> "); 
   
         out.println("<INPUT TYPE=hidden NAME=username VALUE="+username+ " >");
         out.println("<INPUT TYPE=hidden NAME=password VALUE="+password+ " >");

         out.println("</FORM></BODY></HTML> ");

     }
   
     out.close();
   }// end doGet
   public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
                       throws ServletException, IOException {
      doGet(request, response);
   }// end doPost

}// end class