zoukankan      html  css  js  c++  java
  • Java学习笔记51(综合项目:家庭记账系统)

    javaEE的开发模式

    1.什么是模式

    模式在开发过程中总结出的“套路”,总结出的一套约定俗成的设计模式

    2.javaEE经历的模式

    model1模式:

    技术组成:jsp+javaBean

    model1的弊端:随着业务复杂性 导致jsp页面比较混乱

    model2模式

    技术组成:jsp+servlet+javaBean

    model2的优点:开发中 使用各个技术擅长的方面

    servlet:擅长处理java业务代码

    jsp:擅长页面的现实

    MVC:---- web开发的设计模式

    M:Model---模型 javaBean:封装数据

    V:View-----视图 jsp:单纯进行页面的显示

    C:Controller----控制器 Servelt:获取数据--对数据进行封装--传递数据-- 指派显示的jsp页面

    3.javaEE的三层架构

    服务器开发时 分为三层

    web层:与客户端交互

    service层:复杂业务处理

    dao层:与数据库进行交互

    开发实践时 三层架构通过包结构体现

    这个案例几乎用到了前50篇的所有内容,实现一个简易的家庭记账软件

    功能:账务增删改查,按条件查询

    需要的jar包:

    commons-dbcp-1.4.jar

    commons-pool-1.5.6.jar

    mysql-connector-java-5.1.37-bin.jar

    commons-dbutils-1.6.jar

    数据库表创建:

    /*
      创建数据库
      名字 gjp
    */
    CREATE DATABASE gjp;
    
    USE gjp;
    
    CREATE TABLE gjp_zhangwu(
       -- 主键
       zwid INT PRIMARY KEY AUTO_INCREMENT,
       -- 分类名称   
       flname VARCHAR(200),
       -- 金额
       money DOUBLE,
       -- 账户
       zhanghu VARCHAR(100),
       -- 创建日期
       createtime DATE,
       -- 账务描述
       description  VARCHAR(1000)
    );

    加入一些数据:

    -- 写入测试的数据
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (1,'吃饭支出',247,'交通银行','2018-03-02','家庭聚餐');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (2,'工资收入',12345,'现金','2018-03-15','开工资了');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (3,'服装支出',1998,'现金','2018-04-02','买衣服');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (4,'吃饭支出',325,'现金','2018-06-18','朋友聚餐');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (5,'股票收入',8000,'工商银行','2018-10-28','股票大涨');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (6,'股票收入',5000,'工商银行','2018-10-28','股票又大涨');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (7,'工资收入',5000,'交通银行','2018-10-28','又开工资了');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (8,'礼金支出',5000,'现金','2018-10-28','朋友结婚');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (9,'其他支出',1560,'现金','2018-10-29','丢钱了');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (10,'交通支出',2300,'交通银行','2018-10-29','油价还在涨啊');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (11,'吃饭支出',1000,'工商银行','2018-10-29','又吃饭');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (12,'工资收入',1000,'现金','2018-10-30','开资');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (13,'交通支出',2000,'现金','2018-10-30','机票好贵');
    INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (14,'工资收入',5000,'现金','2018-10-30','又开资');

    效果:

    创建工程,新建包,导入jar包,完成后结果:

    domain包创建类:

    保证成员变量名和表的列名一致

    package gjp.domain;
    
    public class Zhangwu {
        private int zwid;
        private String flname;
        private double money;
        private String zhanghu;
        private String createtime;
        private String description;
    
        public Zhangwu() {
        }
    
        public Zhangwu(int zwid, String flname, double money, String zhanghu, String createtime, String description) {
            super();
            this.zwid = zwid;
            this.flname = flname;
            this.money = money;
            this.zhanghu = zhanghu;
            this.createtime = createtime;
            this.description = description;
        }
    
        public int getZwid() {
            return zwid;
        }
    
        public void setZwid(int zwid) {
            this.zwid = zwid;
        }
    
        public String getFlname() {
            return flname;
        }
    
        public void setFlname(String flname) {
            this.flname = flname;
        }
    
        public double getMoney() {
            return money;
        }
    
        public void setMoney(double money) {
            this.money = money;
        }
    
        public String getZhanghu() {
            return zhanghu;
        }
    
        public void setZhanghu(String zhanghu) {
            this.zhanghu = zhanghu;
        }
    
        public String getCreatetime() {
            return createtime;
        }
    
        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        @Override
        public String toString() {
            return "Zhangwu [zwid=" + zwid + ", flname=" + flname + ", money=" + money + ", zhanghu=" + zhanghu
                    + ", createtime=" + createtime + ", description=" + description + "]";
        }
    
    }
    View Code

    tools包创建工具类:

    package gjp.tools;
    //获取数据库连接的工具类
    
    //实现DBCP连接池
    
    import javax.sql.DataSource;
    
    import org.apache.commons.dbcp.BasicDataSource;
    
    public class JDBCUtils {
        private static BasicDataSource dataSource = new BasicDataSource();
        static {
            dataSource.setDriverClassName("com.mysql.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://localhost:3306/gjp");
            dataSource.setUsername("root");
            dataSource.setPassword("xuyiqing");
            dataSource.setInitialSize(10);
            dataSource.setMaxActive(10);
            dataSource.setMaxIdle(5);
            dataSource.setMinIdle(1);
        }
    
        public static DataSource getDataSource() {
            return dataSource;
        }
    }
    View Code

    app包:

    package gjp.app;
    
    import gjp.view.MainView;
    
    //主程序类,用于开启软件程序
    public class MainApp {
        public static void main(String[] args) {
            new MainView().run();
        }
    }
    View Code

    view包:

    package gjp.view;
    
    import java.util.List;
    import java.util.Scanner;
    
    import gjp.controller.ZhangWuController;
    import gjp.domain.Zhangwu;
    
    public class MainView {
        // 用户看到和操作的界面
        // 数据传递给controller层实现
        private ZhangWuController controller = new ZhangWuController();
    
        public void run() {
            // 实现界面效果,接收输入
            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("----------------------家庭记账软件----------------------");
                System.out.println("1.添加账务   2.编辑账务   3.删除账务   4.查询账务   5.退出系统");
                System.out.println("请输入要操作的功能序号(1-5)");
                int choose = sc.nextInt();
                switch (choose) {
                case 1:
                    addZhangWu();
                    break;
                case 2:
                    editZhangWu();
                    break;
                case 3:
                    deleteZhangWu();
                    break;
                case 4:
                    selectZhangWu();
                    break;
                case 5:
                    System.exit(0);
                    break;
                }
            }
        }
        
        public void deleteZhangWu(){
            selectAll();
            System.out.println();
            System.out.println("删除功能,请输入要删除的ID");
            int zwid = new Scanner(System.in).nextInt();
            System.out.println("确定要删除吗?Y/N");
            String flag = new Scanner(System.in).next();
            if(flag.equals("Y")){
                controller.deleteZhangWu(zwid);
                System.out.println("删除账务成功!");
            }else if(flag.equals("N")){
                System.out.println("输入任意键返回");
                new Scanner(System.in).next();
                deleteZhangWu();
            }else{
                System.out.println("输入有误,输入任意键返回");
                new Scanner(System.in).next();
                deleteZhangWu();
            }
        }
        
        public void editZhangWu(){
            selectAll();
            System.out.println();
            System.out.println("编辑功能,请输入数据");
            Scanner sc = new Scanner(System.in);
            System.out.println("输入ID:");
            int zwid = sc.nextInt();
            System.out.println("输入分类名称:");
            String flname = sc.next();
            System.out.println("输入金额:");
            double money = sc.nextDouble();
            System.out.println("输入账户:");
            String zhanghu = sc.next();
            System.out.println("输入日期(格式XXXX-XX-XX):");
            String createtime = sc.next();
            System.out.println("输入具体描述");
            String description = sc.next();
            Zhangwu zw = new Zhangwu(zwid, flname, money, zhanghu, createtime, description);
            controller.editZhangWu(zw);
            System.out.println("账务编辑成功!");
        }
        
        public void addZhangWu(){
            System.out.println("添加账务功能,请输入一下内容");
            Scanner sc = new Scanner(System.in);
            System.out.println("输入分类名称:");
            String flname = sc.next();
            System.out.println("输入金额:");
            double money = sc.nextDouble();
            System.out.println("输入账户:");
            String zhanghu = sc.next();
            System.out.println("输入日期(格式XXXX-XX-XX):");
            String createtime = sc.next();
            System.out.println("输入具体描述");
            String description = sc.next();
            Zhangwu zw = new Zhangwu(0, flname, money, zhanghu, createtime, description);
            controller.addZhangWu(zw);
            System.out.println("添加账务成功!");
        }
    
        public void selectZhangWu() {
            System.out.println("1.查询所有   2.条件查询");
            Scanner sc = new Scanner(System.in);
            int selectChooser = sc.nextInt();
            switch (selectChooser) {
            case 1:
                selectAll();
                break;
            case 2:
                select();
                break;
            }
        }
    
        public void selectAll() {
            List<Zhangwu> list = controller.selectAll();
            if (list.size() != 0) {
                print(list);
            } else {
                System.out.println("没有查询到数据");
            }
        }
    
        public void select() {
            System.out.println("条件查询:输入日期格式XXXX-XX-XX");
            System.out.println("例如:2018-01-22");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入开始日期:");
            String startDate = sc.nextLine();
            System.out.println("请输入结束日期:");
            String endDate = sc.nextLine();
            List<Zhangwu> list = controller.select(startDate, endDate);
            if (list.size() != 0) {
                print(list);
            } else {
                System.out.println("没有查询到数据");
            }
        }
    
        private void print(List<Zhangwu> list) {
            System.out.println("ID		类别		账户		金额		时间		说明");
            for (Zhangwu zw : list) {
                System.out.println(zw.getZwid() + "		" + zw.getFlname() + "		" + zw.getZhanghu() + "		"
                        + zw.getMoney() + "		" + zw.getCreatetime() + "	" + zw.getDescription());
            }
        }
    }
    View Code

    controller包:

    package gjp.controller;
    
    import java.util.List;
    
    import gjp.domain.Zhangwu;
    import gjp.service.ZhangWuService;
    
    public class ZhangWuController {
        // 接收view层数据,传递给service层
        private ZhangWuService service = new ZhangWuService();
        
        public void deleteZhangWu(int zwid){
            service.deleteZhangWu(zwid);
        }
        
        public void addZhangWu(Zhangwu zw){
            service.addZhangWu(zw);
        }
        
        public void editZhangWu(Zhangwu zw){
            service.editZhangWu(zw);
        }
        
        public List<Zhangwu> select(String startDate,String endDate){
            return service.select(startDate, endDate);
        }
        
        public List<Zhangwu> selectAll(){
            return service.selectAll();
        }
        
    }
    View Code

    service包:

    package gjp.service;
    //业务层
    
    import java.util.List;
    
    //接收controller的数据
    //计算后传递给dao层操作数据库
    
    import gjp.dao.ZhangWuDao;
    import gjp.domain.Zhangwu;
    
    public class ZhangWuService {
        private ZhangWuDao dao = new ZhangWuDao();
        
        public void deleteZhangWu(int zwid){
            dao.deleteZhangWu(zwid);
        }
        
        public void editZhangWu(Zhangwu zw){
            dao.editZhangWu(zw);
        }
        
        public void addZhangWu(Zhangwu zw){
            dao.addZhangWu(zw);
        }
        
        public List<Zhangwu> select(String startDate, String endDate) {
            return dao.select(startDate, endDate);
        }
    
        public List<Zhangwu> selectAll() {
            return dao.selectAll();
        }
    }
    View Code

    dao包:

    package gjp.dao;
    //实现对数据库表gjp_zhangwu的增删改查操作
    
    import java.sql.SQLException;
    import java.util.List;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    
    import gjp.domain.Zhangwu;
    import gjp.tools.JDBCUtils;
    
    public class ZhangWuDao {
        private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
    
        public void deleteZhangWu(int zwid){
            try{
            String sql = "DELETE FROM gjp_zhangwu WHERE zwid=?";
            qr.update(sql,zwid);}catch(SQLException ex){
                System.out.println(ex);
                throw new RuntimeException("删除账务失败");
            }
        }
        
        public void editZhangWu(Zhangwu zw) {
            try {
                String sql = "UPDATE gjp_zhangwu SET flname=?,money=?,zhanghu=?,createtime=?,description=? WHERE zwid=?";
                Object[] params = { zw.getFlname(), zw.getMoney(), zw.getCreatetime(), zw.getDescription(), zw.getZwid() };
                qr.update(sql, params);
            } catch (SQLException ex) {
                System.out.println(ex);
                throw new RuntimeException("账户编辑失败");
            }
        }
    
        public void addZhangWu(Zhangwu zw) {
            try {
                String sql = "INSERT INTO gjp_zhangwu (flname,money,zhanghu,createtime,description)VALUES(?,?,?,?,?)";
                Object[] params = { zw.getFlname(), zw.getMoney(), zw.getZhanghu(), zw.getCreatetime(),
                        zw.getDescription() };
                qr.update(sql, params);
            } catch (SQLException ex) {
                System.out.println(ex);
                throw new RuntimeException("账务添加失败");
            }
        }
    
        public List<Zhangwu> select(String startDate, String endDate) {
            try {
                String sql = "SELECT * FROM gjp_zhangwu WHERE createtime BETWEEN ? AND ?";
                Object[] params = { startDate, endDate };
                List<Zhangwu> list = qr.query(sql, new BeanListHandler<Zhangwu>(Zhangwu.class), params);
                return list;
            } catch (SQLException ex) {
                System.out.println(ex);
                throw new RuntimeException("条件查询失败");
            }
        }
    
        public List<Zhangwu> selectAll() {
            try {
                String sql = "SELECT * FROM gjp_zhangwu";
                List<Zhangwu> list = qr.query(sql, new BeanListHandler<Zhangwu>(Zhangwu.class));
                return list;
            } catch (SQLException ex) {
                System.out.println(ex);
                throw new RuntimeException("查询所有账务失败");
            }
        }
    }
    View Code

    结构图:

    本程序并不完整,没有包含输入的判断,使用时候保证输入正确的格式

    完善它并不复杂

    运行效果图:

    完成

  • 相关阅读:
    利用python做矩阵的简单运算(行列式、特征值、特征向量等的求解)
    numpy.linalg.svd函数
    梯度裁剪(Clipping Gradient):torch.nn.utils.clip_grad_norm
    tf.matmul()报错expected scalar type Float but found Double
    1283 最小周长
    1182 完美字符串
    1091 线段的重叠
    1090 3个数和为0
    1087 1 10 100 1000
    1083 矩阵取数问题
  • 原文地址:https://www.cnblogs.com/xuyiqing/p/8329245.html
Copyright © 2011-2022 走看看