家庭记账本的记账功能实现

Posted yeyueweiliang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了家庭记账本的记账功能实现相关的知识,希望对你有一定的参考价值。

 

 

 

 

此处只放相关部分,如有需要,请参考同名下本人的其他博客

 

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<style type="text/css">
        *{margin: 0;padding: 0}
        html,body{height: 100%}     /*这里很关键*/
 
        .outer-wrap{
            /*只有同时为html和body设置height: 100%时,这里的height才生效,
            并且随浏览器窗口变化始终保持和浏览器视窗等高*/
            height: 100%;    
            position: relative;
            background-image: url(‘images/01.jpg‘);
        }
        .index-panel{
            width: 400px;
            height: 300px;
            background-image: url(‘images/04.jpg‘);
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -150px;
            margin-left: -200px;
        }
    </style>
<body>
<%
         request.setCharacterEncoding("utf-8");
         String message=(String)request.getAttribute("message");
         if(message!=null){
         if(message.equals("error")){
             %>
             <script type="text/javascript">
              alert("用户名或密码错误");
         </script>
         <%
      }else if(message.equals("noerror")){
          %>
          <script type="text/javascript">
              alert("登录成功");
         </script>
         <%
      }else{
      }
      }
      %>
      <div class="outer-wrap">
      <div style="font-size:160px;text-align:center">家庭记账本</div>
      <div class="index-panel">
           <div  style="font-size:40px;text-align:center">欢迎您的使用</div>
           <div style="font-size:30px;text-align:center">  <a href="add.jsp">记账</a><br/> </div>
           <div style="font-size:30px;text-align:center"> <a href="show.jsp">查看账目</a> </div>
            
      </div>
      </div>
</body>
</html>

add.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>记账</title>
</head>
 <style type="text/css">
        *{margin: 0;padding: 0}
        html,body{height: 100%}     /*这里很关键*/
 
        .outer-wrap{
            /*只有同时为html和body设置height: 100%时,这里的height才生效,
            并且随浏览器窗口变化始终保持和浏览器视窗等高*/
            height: 100%;    
            position: relative;
            background-image: url(‘images/01.jpg‘);
        }
        .add-panel{
            width: 400px;
            height: 300px;
            background-image: url(‘images/06.jpg‘);
            position: absolute;
            top: 50%;
            left: 50%;
            margin-top: -150px;
            margin-left: -200px;
        }
    </style>
<body>
<%
         request.setCharacterEncoding("utf-8");
         String message=(String)request.getAttribute("message");
         if(message!=null){
         if(message.equals("error")){
             %>
             <script type="text/javascript">
              alert("添加失败");
         </script>
         <%
      }else if(message.equals("noerror")){
          %>
          <script type="text/javascript">
              alert("添加成功");
         </script>
         <%
      }else{
      }
      }
         
      %>
      
<div class="outer-wrap">
      <div style="font-size:160px;text-align:center">家庭记账本</div>
      <div class="add-panel">
      <div style="text-align:center;color:yellow">请输入要添加的账目信息:</div><br/>
      <form action="AddBillServlet" method="post">
      
    <div style="text-align:center;color:yellow">
             账目类型<select id="btype" name="btype">
            <option value="饮食">饮食</option>
            <option value="教育">教育</option>
            <option value="购物">购物</option>
            <option value="医疗">医疗</option>
            <option value="收入">收入</option>
            <option value="借贷">借贷</option>
            <option value="其它">其它</option>
        </select>
    </div>
    <div style="text-align:center;color:red">注:借贷和其它类型的账目不计入收支</div>
    <div style="text-align:center;color:yellow">
     金额<input type="text"  name="bmoney"><br/>
    </div>
    <div style="text-align:center;color:yellow">
     日期<input type="date"  name="bdate"><br/>
    </div>
    <div style="text-align:center;color:yellow">
     备注<input type="text"  name="bremark"><br/>
    </div>
    <div style="text-align:center;color:yellow">
     <input type="submit"  value="添加"><br/>
    </div>
    </form>
      </div>
      </div>
</body>
</html>

AddBillServlet.java

package com.zzw.servlet;

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

import com.zzw.entity.Bill;
import com.zzw.service.IUserService;
import com.zzw.service.Impl.UserServiceImpl;


public class AddBillServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        String type= request.getParameter("btype");
        int money=Integer.parseInt(request.getParameter("bmoney"));
        String date= request.getParameter("bdate");
        String remark= request.getParameter("bremark");
        Bill bill =new Bill(type,money,date,remark);
        //接口 x=new 实现类()
        IUserService userservice = new UserServiceImpl();
        boolean result=userservice.AddBill(bill);
    
        if(!result) {
            request.setAttribute("message","error");
        }else {
            request.setAttribute("message","noerror");
        }
        request.getRequestDispatcher("add.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

UserServiceImpl.java

package com.zzw.service.Impl;




import com.zzw.dao.IUserDao;
import com.zzw.dao.Impl.UserDaoImpl;
import com.zzw.entity.Bill;
import com.zzw.entity.User;
import com.zzw.service.IUserService;

public class UserServiceImpl implements IUserService{
    IUserDao userdao= new UserDaoImpl();
    //登录
     public boolean Login(User user) {
            boolean flag=false;
          if(userdao.Login(user.getUname(),user.getUpwd())) {
                flag=true;
          }
          return flag;
     }
    //注册
    public boolean Register(User user) {
        boolean flag=false;
        if(!userdao.isExist(user.getUname())) {
            userdao.Register(user);
            flag=true;
        }else {
            System.out.println("此人已存在");
        }
       return flag; 
    }
  //根据账号查询用户
    public User Query(String uname) {
        return userdao.Query(uname);
    }
    //记账
    public boolean  AddBill(Bill bill) {
        boolean flag=false;
        if(userdao.AddBill(bill)) {
           flag=true;
        }
        return flag;
    }
}

IUserService.java

package com.zzw.service;

import com.zzw.entity.Bill;
import com.zzw.entity.User;

public interface IUserService {
    //登录
         public boolean Login(User user);
    //注册
         public boolean Register(User user) ;
    //根据账号查询用户
         public User Query(String uname) ;
    //记账
         public boolean  AddBill(Bill bill) ;
}

Bill.java

package com.zzw.entity;

public class Bill {
      private int bid;
      private String btype;
      private int bmoney;
      private String bdate;
      private String bremark;
      
    public Bill() {
    }
    public Bill(String btype, int bmoney, String bdate, String bremark) {
        this.btype = btype;
        this.bmoney = bmoney;
        this.bdate = bdate;
        this.bremark = bremark;
    }
    public Bill(int bid, String btype, int bmoney, String bdate, String bremark) {
        this.bid = bid;
        this.btype = btype;
        this.bmoney = bmoney;
        this.bdate = bdate;
        this.bremark = bremark;
    }
    @Override
    public String toString() {
        return "Bill [bid=" + bid + ", btype=" + btype + ", bmoney=" + bmoney + ", bdate=" + bdate + ", bremark="
                + bremark + "]";
    }
    public int getBid() {
        return bid;
    }
    public void setBid(int bid) {
        this.bid = bid;
    }
    public String getBtype() {
        return btype;
    }
    public void setBtype(String btype) {
        this.btype = btype;
    }
    public int getBmoney() {
        return bmoney;
    }
    public void setBmoney(int bmoney) {
        this.bmoney = bmoney;
    }
    public String getBdate() {
        return bdate;
    }
    public void setBdate(String bdate) {
        this.bdate = bdate;
    }
    public String getBremark() {
        return bremark;
    }
    public void setBremark(String bremark) {
        this.bremark = bremark;
    }
      
}

UserDaoImpl.java

package com.zzw.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.zzw.dao.IUserDao;
import com.zzw.entity.Bill;
import com.zzw.entity.User;
import com.zzw.utils.DBUtil;

public class UserDaoImpl implements IUserDao{
    //注册
            public  boolean Register(User user) {
                 String sql="insert into user(uname,upwd,usex) values(?,?,?)" ;
                 Object [] params= {user.getUname(),user.getUpwd(),user.getUsex()};
                 return  DBUtil.executeUpdate(sql, params);
            }
            //查询账户是否存在
            public  boolean isExist(String uname) {
                return Query(uname)==null? false:true;
            }
    //登录
           public boolean Login(String uname,String upwd) {
               return Query(uname,upwd)==null? false:true;
           }
    //根据账号查询用户全部信息
    public   User Query(String uname) {
         User user= null;
         ResultSet rs = null; 
         try {
             String sql="select * from user where uname =?" ;
             Object [] params= {uname};
             rs=DBUtil.executeQuery(sql, params);
             if(rs.next()) {
                 String name=rs.getString("uname");
                 String pwd=rs.getString("upwd");
                 String sex=rs.getString("usex");
                 user= new User(name,pwd,sex);
             }
         }catch(SQLException e) {
             e.printStackTrace();
         }catch(Exception e) {
             e.printStackTrace();
         }finally {
             try {
                    //先开的后关,后开的先关
                if(rs!=null)rs.close();
                if(DBUtil.pstmt!=null)DBUtil.pstmt.close();
                if(DBUtil.connection !=null)DBUtil.connection.close();
                }catch(SQLException e) {
                    e.printStackTrace();
                }finally {
                    
                }
         }
         return user;
    }
    //根据账户密码确定是否存在
    public   User Query(String uname,String upwd) {
         User user= null;
         ResultSet rs = null; 
         try {
             String sql="select * from user where uname =? and upwd=?" ;
             Object [] params= {uname,upwd};
             rs=DBUtil.executeQuery(sql, params);
             if(rs.next()) {
                 String name=rs.getString("uname");
                 String pwd=rs.getString("upwd");
                 String sex=rs.getString("usex");
                 user= new User(name,pwd,sex);
             }
         }catch(SQLException e) {
             e.printStackTrace();
         }catch(Exception e) {
             e.printStackTrace();
         }finally {
             try {
                   //先开的后关,后开的先关
               if(rs!=null)rs.close();
               if(DBUtil.pstmt!=null)DBUtil.pstmt.close();
               if(DBUtil.connection !=null)DBUtil.connection.close();
               }catch(SQLException e) {
                   e.printStackTrace();
               }finally {
                   
               }
         }
         return user;
    }
    //记账
    public  boolean AddBill(Bill bill) {
        String sql="insert into bill(btype,bmoney,bdate,bremark) values(?,?,?,?)" ;
         Object [] params= {bill.getBtype(),bill.getBmoney(),bill.getBdate(),bill.getBremark()};
         return  DBUtil.executeUpdate(sql, params);
    }
}

IUserDao.java

package com.zzw.dao;

import com.zzw.entity.Bill;
import com.zzw.entity.User;

public interface IUserDao {
    //注册
    public  boolean Register(User user) ;
    //查询账户是否存在
    public  boolean isExist(String uname) ;
    //登录
    public  boolean Login(String uname,String upwd) ;
    //根据帐号查询用户全部信息
    public   User Query(String uname) ;
    //记账
    public  boolean AddBill(Bill bill);
}

 

 

 

下图为数据库中bill表

技术图片

登录成功后进入首页

 

技术图片

 

 

 

 

技术图片

 

 技术图片

 

 技术图片

 

以上是关于家庭记账本的记账功能实现的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序--家庭记账本开发--06

go-家庭收支记账软件例子

GitHub上传家庭记账本

简单实现家庭记账程序(java)

微信小程序家庭记账本开发进度五

Golang家庭收支记账软件项目