基于Javaweb实现ATM机系统开发实战(九)存款功能实现

先看前端界面确定后端需要处理的参数,把一些参数进行修改:

基于Javaweb实现ATM机系统开发实战(九)存款功能实现_第1张图片

 

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

    
    
    

    






    
    
    
    



    
    
 

存款


<%-- --%>
${balance }

${error3}































基于Javaweb实现ATM机系统开发实战(九)存款功能实现_第2张图片

 根据前端的地址创建servlet:

package com.atm.servlet;

import com.atm.pojo.User;
import com.atm.service.UserService;
import com.atm.service.impl.UserServiceImpl;

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

//用户存款
@WebServlet("/UserStore")
public class UserStoreServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    User user=(User) request.getSession().getAttribute("ua");
    int money=Integer.parseInt(request.getParameter("saveNum"));
    if(money ==0){
        request.getSession().setAttribute("error3","存款金额不能为0");
        response.sendRedirect("changepage/UserStore.jsp");
    } else if (money%100!=0) {
        request.getSession().setAttribute("error3","存款金额必须为100的倍数");
        response.sendRedirect("changepage/UserStore.jsp");
    } else if (money>10000) {
        request.getSession().setAttribute("error3","存款金额不能大于10000");
        response.sendRedirect("changepage/UserStore.jsp");
    }else {
        try{
            userService.storeMoney(money,user.getIcno());
            response.sendRedirect("/UserSerch");
        }catch (Exception e){
            request.getSession().setAttribute("error3","存款失败");
            response.sendRedirect("changepage/UserStore.jsp");
            e.printStackTrace();
        }

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

}

接口实现:


    //用户存款
    public void storeMoney(int money,String icno)throws Exception;

实现类实现:


    //用户存款
    public void storeMoney(int money,String icno)throws Exception{
         userDao.storeMoney(money,icno);
    }

数据层:

    //用户存款
    public void storeMoney(int money,String icno) throws Exception{
    Connection connection=DBUtils.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("update user set balance=balance+? where icno=?");
        preparedStatement.setInt(1,money);
        preparedStatement.setString(2,icno);
        preparedStatement.executeUpdate();
        DBUtils.release(connection,preparedStatement,null);

    }

重启系统,发现功能实现啦~

基于Javaweb实现ATM机系统开发实战(九)存款功能实现_第3张图片

 实现代码已上传~

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