首先创建一个业务接口:
package com.atm.service;
import com.atm.pojo.RunMessage;
//交易记录的业务接口
public interface RunMessageService{
//添加交易记录
public void addRunMessage(RunMessage runMessage) throws Exception ;
}
再完成业务接口的实现类:
package com.atm.service.impl;
import com.atm.dao.RunMessageDao;
import com.atm.pojo.RunMessage;
import com.atm.service.RunMessageService;
//交易记录业务接口实现类
public class RunMessageServiceImpl implements RunMessageService {
private RunMessageDao runMessageDao=new RunMessageDao();
@Override
public void addRunMessage(RunMessage runMessage) throws Exception {
runMessageDao.insert(runMessage);
}
}
在创建数据层的持久化类:
package com.atm.dao;
import com.atm.pojo.RunMessage;
import com.atm.utill.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
//交易记录持久化类
public class RunMessageDao {
//保存交易记录
public void insert(RunMessage runMessage) throws Exception {
Connection conn= DBUtils.getConnection();
PreparedStatement preparedStatement = conn.prepareStatement("insert into runmessage values (null,?,?,?,?,?) ");
preparedStatement.setString(1,runMessage.getIcno());
preparedStatement.setString(2,runMessage.getRm_date());
preparedStatement.setString(3,runMessage.getRm_currency());
preparedStatement.setDouble(4,runMessage.getRm_balance());
preparedStatement.setDouble(5,runMessage.getRm_rest());
preparedStatement.executeUpdate();
DBUtils.release(conn,preparedStatement,null);
}
}
在存款中调用接口来保存交易记录:
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("/UserStore")
public class UserStoreServlet extends HttpServlet {
private UserService userService=new UserServiceImpl();
private RunMessageService runMessageService=new RunMessageServiceImpl();
@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());
//保存交易记录
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);
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);
}
}
取款中也是一样的:
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("/UserFetch")
public class UserFetchServlet extends HttpServlet {
private RunMessageService runMessageService=new RunMessageServiceImpl();
private UserService userService=new UserServiceImpl();
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//拿到当前用户信息
User user = (User) request.getSession().getAttribute("ua");
//查询余额:
double balance = 0;
try {
balance = userService.getBalance(user.getIcno());
} catch (Exception e) {
e.printStackTrace();
}
//获得当前存款参数
int money = Integer.parseInt(request.getParameter("fetchNum"));
if (money == 0) {
request.getSession().setAttribute("error2", "取款金额不能为0");
response.sendRedirect("changepage/UserFetch.jsp");
} else if (money % 100 != 0) {
request.getSession().setAttribute("error2", "取款金额必须为100的倍数");
response.sendRedirect("changepage/UserFetch.jsp");
} else if (money > 10000) {
request.getSession().setAttribute("error2", "单次取款金额不能大于10000");
response.sendRedirect("changepage/UserFetch.jsp");
} else if (money > balance) {
request.getSession().setAttribute("error2", "余额不足");
response.sendRedirect("changepage/UserFetch.jsp");
} else {
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);
response.sendRedirect("/UserSerch");
} catch (Exception e) {
request.getSession().setAttribute("error2", "取款失败");
response.sendRedirect("changepage/UserFetch.jsp");
e.printStackTrace();
}
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
然后我们重启一下系统,存个钱测试一下;
发现交易记录已经成功保存啦~
代码已上传~