使用mvc架构模式实现账户之间的转账(没有涉及事务处理)

 创建一个账户实体类:用于封装账户信息的。

        数据类型尽量不要使用基本数据类型,使用引用数据类型;

创建一个持久层:用于对数据进行处理

创建一个服务层:用户处理业务

创建一个视图层:用户页面的展示

创建一个控制层:用于调用,以及页面之间的流转

实现用户的登录、转账成功、转账失败、余额不足等页面

<%@page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>模拟转账</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/transfer" method="post">
        转出账户:<input type="text" name="fromcard" ><br>
        转入账户:<input type="text" name="tocard" ><br>
        转出金额:<input type="text" name="outmoney" ><br>
        <input type="submit" value="提交">
    </form>
</body>
</html>
<%@page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>转账失败</title>
</head>
<body>
    <h1>转账失败</h1>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>转账失败</title>
</head>
<body>
<h1>余额不足</h1>
</body>
</html>
<%@page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>成功页面</title>
</head>
<body>
    <h1>转账成功</h1>
</body>
</html>

编写持久层

该层主要用于对数据进行处理,在该业务中只有对数据的查询和更新功能

package com.zhoujun.c2002.Dao;

import com.zhoujun.c2002.bean.Account;
import com.zhoujun.c2002.utils.JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class AccountDao {

    /**
     *更新账户
     * @param account
     * @return count 执行次数,用于判断sql语句是否执行了
     */
    public int update(Account account){
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;

        try {
            conn = JDBC.getConnection();
            String sql = "update bank set cardId = ? ,money = ? where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,account.getCardId());
            ps.setDouble(2,account.getMoney());
            ps.setLong(3,account.getId());
            count = ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBC.close(conn,ps,null);
        }
        return count;
    }

    /**
     * 查询根据账号查询账户
     * @param cardId
     * @return
     */
    public Account selectbycardId(String cardId){
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Account account = null;
        try {
            conn = JDBC.getConnection();
            String sql = "select id,money from bank where cardId = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,cardId);
            rs = ps.executeQuery();
            if (rs.next()) {
                Long id = rs.getLong("id");
                Double money = rs.getDouble("money");

                account = new Account();
                account.setCardId(cardId);
                account.setId(id);
                account.setMoney(money);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBC.close(conn,ps,rs);
        }
        return  account;
    }
}

编写业务层

package com.zhoujun.c2002.service;

import com.zhoujun.c2002.Dao.AccountDao;
import com.zhoujun.c2002.Exception.AppException;
import com.zhoujun.c2002.Exception.MoneyNotEnoughException;
import com.zhoujun.c2002.bean.Account;
import com.zhoujun.c2002.utils.JDBC;

import java.sql.Connection;
import java.sql.SQLException;

public class TransferService {

    private  AccountDao accountDao = new AccountDao();

    public void transfer(String fromcardId,String tocardId,Double money) throws MoneyNotEnoughException, AppException {

        Account account = accountDao.selectbycardId(fromcardId);
        if (account.getMoney() < money){
            throw new MoneyNotEnoughException("对不起,余额不足");
        }
        Account account1 = accountDao.selectbycardId(tocardId);

        account.setMoney(account.getMoney() - money);
        account1.setMoney(account1.getMoney() + money);

        int count = accountDao.update(account);

        count += accountDao.update(account1);

        if (count != 2) {
            throw new AppException("账户转账异常!!!");
        }

    }
}

首先调用持久层对数据进行查询操作,调用实体类中的getMoney()方法与浏览器上用户输入的数据进行比较,如果结果是小于,则抛出一个异常。如果结果是大于,则进行数据的更新。在调用持久层对数据进行处理(更新,这里暂时没有使用事务)

编写控制层

package com.zhoujun.c2002.servlet;

import com.zhoujun.c2002.Exception.AppException;
import com.zhoujun.c2002.Exception.MoneyNotEnoughException;
import com.zhoujun.c2002.service.TransferService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/transfer")
public class TransferServlet extends HttpServlet {

    private TransferService transferService = new TransferService();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //收集前端发送的数据
        String fromcard = request.getParameter("fromcard");
        String tocard = request.getParameter("tocard");
        Double outmoney =  Double.parseDouble(request.getParameter("outmoney"));

        try {
            transferService.transfer(fromcard,tocard,outmoney);
            response.sendRedirect(request.getContextPath() + "/success.jsp");
        } catch (MoneyNotEnoughException e) {
            response.sendRedirect(request.getContextPath() + "/moneynotenough.jsp");
        } catch (Exception e){
            response.sendRedirect(request.getContextPath() + "/fail.jsp");
        }
    }
}

获取用户输入的值,调用服务层中的transfer()方法,将获取到的值传入,如果成功了就重定向到登录成功的页面,如果失败就重定向到失败页面

这是本人用于学习的记录,如果有缺陷希望各位大佬们能够指点,来自一个正在进步中的编程小白

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值