基于Javaweb实现ATM机系统开发实战(十二)用户转账功能实现

还是老规矩,先看前端传来怎样的参数:

<%@ 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}
































创建servlet接收和处理请求,这里转账其实跟存取款要做的操作都一样,所以我们就不单独创建一个接口来实现转账的方法,而是直接调用存取款来完成相关操作:

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("/UserSerch");

        } 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);
    }


}

重启系统,然后测试一下,发现转账成功啦~

用户的余额也都发生了变化:

 转账记录也记录了下来:

基于Javaweb实现ATM机系统开发实战(十二)用户转账功能实现_第1张图片

 

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