基于Javaweb实现ATM机系统开发实战(十五)退卡和转账跳转实现

首先创建一个servlet接受和处理请求:

package com.atm.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;


//用户退出
@WebServlet("/logout")
public class ExitServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//销毁session对象,或销毁session中保存的用户数据

//        request.getSession().removeAttribute("ua");
        request.getSession().invalidate();
        response.sendRedirect("/changepage/login.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

对前端页面进行修改:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%> 




    
    IMUT_BANK ATM WelCome!!
    
    
    
    
    

    
    
    

    
    
    
    
    




    
    
 

IMUT BANK借记卡明细对账单

<% SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 request.setAttribute("date_time", df.format(new Date())); %>

打印日期:${date_time }



       卡      号               日      期               货      币               交  易  金  额               账  户  余  额       
${user.icno } ${user.rm_date } ${user.rm_currency } ${user.rm_balance } ${user.rm_rest }
<%-- ${requestScope.pageBean.pageNum == 1} --%> <%-- ${pageContext.request.contextPath}/serchDetaile?pageNum=1 --%>



<%-- 构建分页导航 --%>

共有${pageBean.totalRecord}条数据,共${pageBean.totalPage}页,当前为${pageBean.pageNum}页


首页 <%--如果当前页为第一页时,就没有上一页这个超链接显示 --%> ${i} ${i} 下一页 <%--如果当前页不是第一页也不是最后一页,则有上一页和下一页这个超链接显示 --%> 上一页 ${i} ${i} 下一页 <%-- 如果当前页是最后一页,则只有上一页这个超链接显示,下一页没有 --%> 上一页 ${i} ${i} <%--尾页 --%> 尾页
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>



    
    IMUT_BANK ATM WelCome!!
    
    
    
    
    

    
    
    

    






    
    
    
    



    
    
 

转账

当前账户余额


${error4}
































测试一下效果,发现功能实现啦~

基于Javaweb实现ATM机系统开发实战(十五)退卡和转账跳转实现_第1张图片

 接下来我们实现转账跳转的操作:

package com.atm.servlet;

import com.atm.pojo.RunMessage;
import com.atm.pojo.User;
import com.atm.service.RunMessageService;
import com.atm.service.UserService;
import com.atm.service.impl.RunMessageServiceImpl;
import com.atm.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

//用户转账
@WebServlet("/UserTransfer")
public class UserTransferServlet extends HttpServlet {

    private UserService userService=new UserServiceImpl();
    private RunMessageService runMessageService=new RunMessageServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //获取相关参数
        String icno =request.getParameter("transferIn");
        int money=Integer.parseInt(request.getParameter("transferNum"));
        //做出相应的业务数据判断
        //先判断账户是否存在
        User user=null;
        try {
             user = userService.getUserByIcno(icno);
            if(user==null){
                request.getSession().setAttribute("error4","转账的账户不存在");
                response.sendRedirect("/changepage/UserTransfer.jsp");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        //判断转账的金额

        //查询当前用户的余额
        //拿到当前用户信息
        user = (User) request.getSession().getAttribute("ua");
        double balance = 0;
        try {
            balance = userService.getBalance(user.getIcno());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (money == 0) {
            request.getSession().setAttribute("error4", "转账金额不能为0");
            response.sendRedirect("changepage/UserTransfer.jsp");
        } else if (money % 100 != 0) {
            request.getSession().setAttribute("error4", "转账金额必须为100的倍数");
            response.sendRedirect("changepage/UserTransfer.jsp");
        } else if (money > 10000) {
            request.getSession().setAttribute("error4", "单次转账金额不能大于10000");
            response.sendRedirect("changepage/UserTransfer.jsp");
        } else if (money > balance) {
            request.getSession().setAttribute("error4", "余额不足");
            response.sendRedirect("changepage/UserTransfer.jsp");
    }
    //执行转账操作
        try {
            //转出:取款
            userService.fetchMoney(money,user.getIcno());
            //保存交易记录
            SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-dd hh:mm:ss");
            //查询当前最新的账户余额
            double rest= userService.getBalance(user.getIcno());
            RunMessage runMessage =new RunMessage();
            runMessage.setIcno(user.getIcno());
            runMessage.setRm_currency("人民币");
            runMessage.setRm_date(sdf.format(new Date()));
            runMessage.setRm_balance(-Double.valueOf(money));
            runMessage.setRm_rest(rest);
            //保存交易记录
            runMessageService.addRunMessage(runMessage);


            //转入:存款
            userService.storeMoney(money,icno);
            //保存交易记录

            //查询当前最新的账户余额
            double restIn= userService.getBalance(icno);
            runMessage.setIcno(icno);
            runMessage.setRm_currency("人民币");
            runMessage.setRm_date(sdf.format(new Date()));
            runMessage.setRm_balance(Double.valueOf(money));
            runMessage.setRm_rest(restIn);
            //保存交易记录
            runMessageService.addRunMessage(runMessage);
            response.sendRedirect("/changepage/TransferSucc.jsp");

        } catch (Exception e) {

            request.getSession().setAttribute("error4", "转账失败");
            response.sendRedirect("changepage/UserTransfer.jsp");
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


}

也是同样的,对前端页面进行修改:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




    
    Apricot 1.3
    
    
    

    
    
    

    
    
    

    






    
    
    
    



    
    
 

转账成功

转账成功 3s后自动跳转回主页... ...







如果没有跳转,请点击 跳转

效果测试:

基于Javaweb实现ATM机系统开发实战(十五)退卡和转账跳转实现_第2张图片

实现代码已上传~

你可能感兴趣的:(JAVA学习笔记,java)