管家婆项目

各个类之间的关系

管家婆项目_第1张图片

包和类

管家婆项目_第2张图片

VIEW层 信息录入提示

package cfg.view;

import cfg.controller.ZhangWuController;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
import cfg.domain.Gjp_zhangwu;
import cfg.domain.Zhangdan;
import cfg.utils.C3P0xmlUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;


//view层  信息录入提示层
public class MainView {
    private ZhangWuController controller= new ZhangWuController();
    //
    QueryRunner qr = new QueryRunner(C3P0xmlUtils.getDatesource());

    /*
    实现页面效果图做出来
    接收用户输入
    根据数据调用不同的方法
     */
    public void run(){
        //1 创建一个scanner对象 死循环
        Scanner sc = new Scanner(System.in);

        while (true){
            System.out.println("-------------------------欢迎登录管家婆软件----------------------------");
            System.out.println("1 添加账务  2 编辑账务  3 删除账务  4 查询账务  5 退出");
            System.out.println("请输入您要选择的功能");
            int choose = sc.nextInt();
            switch (choose){
                case 1:
                    //选择的是添加账目 调用添加账目的方法
                    addZhangmu();
                    break;
                case 2:
                    //选择要编辑的账务
                    updateZhangWu();
                    break;
                case 3:
                    //选择要删除的账务
                    deleteZhangWu();
                    break;
                case 4:
                    //选择要查询的账务
                    selectZhangWu();
                    break;
                case 5:
                    System.out.println("欢迎您再次使用\n退出成功");
                    System.exit(0);
                default:
                    System.out.println("请按照对应的功能输入1-5之间的数字");
            }
        }
    }

    //功能2 修改账务
    public void updateZhangWu(){
        boolean flag=true;
//        建立循环  如果输入的序列号在数据库中不存在  循环
        while (flag){


            System.out.println("您选择的是修改账务功能 请输入您要修改的序列号");
            Scanner sc = new Scanner(System.in);
            int zwid = sc.nextInt();//输入的账务ID

            //查询语句  判断数据库里有没有这个id
            String sql01="select * from gjp_zhangwu where zwid=?";
            try {
                List quer = qr.query(sql01, new BeanListHandler<>(Zhangdan.class), zwid);
                if (quer.size()==0){
                    flag=true;
                    System.out.println("您输入的账单ID不存在");
                }else {

                    //建一个开关  如果下面选项不是1--6之间的数字  循环  重新输入
                    flag=false;
                    boolean fff=true;
                    while (fff){
                        System.out.println("1 分类名称  2 金额  3 创建时间   4 账目描述   5 支付方式 6 返回");
                        System.out.println("请输入要修改的类别");
                        int up = sc.nextInt();
                        switch (up){
                            case 1:
                                System.out.println("请输入新的分类名称:");
                                String name = sc.next();
                                controller.updateZhangWu(name,zwid);
                                break;

                            case 2:
                                System.out.println("请输入金额:");
                                double money = sc.nextDouble();
                                controller.updateZhangWu(money,zwid);
                                break;

                            case 3:
                                System.out.println("请输入创建时间:xxxx-xx-xx");
                                String createtime = sc.next();
                                controller.updateTimeZhangWu(createtime,zwid);
                                break;

                            case 4:
                                System.out.println("请输入账务描述");
                                String description = sc.next();
                                controller.updateMSZhangWu(description,zwid);
                                break;

                            case 5:
                                System.out.println("请输入支出方式");
                                String zhanghu = sc.next();
                                controller.updateZHZhangWu(zhanghu,zwid);
                                break;

                            case 6:
                                System.out.println("返回成功");
                                continue;

                            default:
                                fff=true;
                                System.out.println("请按照对应的功能输入1-6之间的数字");
                        }
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        }
    
    //  功能3 删除账务 定义方法 删除 实现思想 接收用户的输入  输入主键  调用控制层的方法 delete 传入主键
    public void deleteZhangWu(){
        System.out.println("您选择的是删除账务功能 请输入序列号");
        int zwid = new Scanner(System.in).nextInt();
        //调用控制层的controller
        controller.deleteZhangWu(zwid);
        System.out.println("删除成功");
    }
    
    //功能1.添加账目
    public void  addZhangmu(){
        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("请输入消费时间 格式:xxxx-xx-xx");
        String createtime = sc.next();
        System.out.println("请输入账目描述");
        String description = sc.next();
        System.out.println("请输入支付方式");
        String zhanghu = sc.next();
        //将接受到的数据传给conntroller层  传递参数  实现添加数据
        Gjp_zhangwu zw = new Gjp_zhangwu(flname,money,createtime,description,zhanghu);
        controller.addZhangWu(zw);
        System.out.println("添加数据成功");

    }

    //功能4 查询账务
    public void selectZhangWu(){
        System.out.println("您选择的是添加账务功能,请输入要查询方式");
            System.out.println("1 按id编号查询   2 查询所有");
        Scanner sc = new Scanner(System.in);
        int chose = sc.nextInt();
        switch (chose){
            case 1:
                System.out.println("请输入您要选择的编号");
                int zwid = sc.nextInt();
                controller.selectZhangWu(zwid);

                break;
            case 2:
                controller.seletcAllZhangWu();
                break;
        }

    }

}



Controller层 接收上层数据传输到下一层
package cfg.controller;

import cfg.domain.Gjp_zhangwu;
import cfg.service.ZhangWuService;

public class ZhangWuController {
   private ZhangWuService service = new ZhangWuService();
   //调用service的方法  删除
    public void deleteZhangWu(int zwid){
        service.deleteZhangWu(zwid);
    }

    //添加数据 调用service方法  添加
    public void addZhangWu(Gjp_zhangwu zw){
        service.addZhangWu(zw);
    }

    //查询 按条件查询
    public void selectZhangWu(int zwid){
        service.selectZhangWu(zwid);
    }
    //查询 查询所有
    public void seletcAllZhangWu(){
        service.seletcAllZhangWu();
    }

    //修改1 修改名字
    public void updateZhangWu(String name,int zwid){
        service.updateZhangWu(name,zwid);
    }

    //修改2 金额
    public void updateZhangWu(double money,int zwid){
        service.updateZhangWu(money,zwid);
    }

    //修改3 时间
    public void updateTimeZhangWu(String createtime,int zwid){
        service.updateTimeZhangWu(createtime,zwid);
    }
    
    //修改3 账务描述
    public void updateMSZhangWu(String description, int zwid) {
        service.updateMSZhangWu(description,zwid);
    }

    //修改4 支出方式
    public void updateZHZhangWu(String zhanghu, int zwid) {
        service.updateZHZhangWu(zhanghu,zwid);
    }
}



Service层 业务层数据传输到下一层
package cfg.service;

import cfg.dao.ZhangWuDao;
import cfg.domain.Gjp_zhangwu;

//业务层
public class ZhangWuService {
    private ZhangWuDao dao=new  ZhangWuDao();
    /*
    定义方法  实现账务删除功能
    id传入
     */

    //删除
    public void deleteZhangWu(int zwid){
        dao.deleteZhangWu(zwid);
    }

    //添加数据
    public void addZhangWu(Gjp_zhangwu zw){
        dao.addZhangWu(zw);
    }

    //查询数据
    public void selectZhangWu(int zwid){
        dao.selectZhangWu(zwid);
    }

    //查询所有
    public void seletcAllZhangWu(){
        dao.seletcAllZhangWu();
    }

    //修改账户信息
    public void updateZhangWu(String name,int zwid){
        dao.updateZhangWu(name,zwid);
    }
    //修改账户金额
    public void updateZhangWu(double money,int zwid){
        dao.updateZhangWu(money,zwid);
    }

    //修改账户时间
    public void updateTimeZhangWu(String createtime,int zwid){
        dao.updateTimeZhangWu(createtime,zwid);
    };


    //修改账户描述
    public void updateMSZhangWu(String description,int zwid){
        dao.updateMSZhangWu(description,zwid);
    };

    //修改支付方式
    public void updateZHZhangWu(String zhanghu,int zwid){
        dao.updateZHZhangWu(zhanghu,zwid);
    };

}



Dao层 操作数据库 对业务层的数据进行处理
package cfg.dao;

import cfg.domain.Gjp_zhangwu;
import cfg.domain.Zhangdan;
import cfg.utils.C3P0xmlUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

//财务数据层
public class ZhangWuDao {
    //service 都依赖dao
    //获取数据库连接 QueerRunner
    QueryRunner qr = new QueryRunner(C3P0xmlUtils.getDatesource());
    public void deleteZhangWu(int zwid){

        //拼写SQL语句  删除一条信息
        String sql="delete from gjp_zhangwu where zwid=? ";
        //执行
        try {
            qr.update(sql,zwid);
        } catch (SQLException e) {
            System.out.println(e);
            throw new RuntimeException("账务删除失败");
        }
    }

    //添加一条数据
    public void addZhangWu(Gjp_zhangwu zw){
        //添加数据
        String sql="INSERT INTO gjp_zhangwu (flname,money,createtime,description,zhanghu)VALUES(?,?,?,?,?)";
        Object [] params={zw.getFlname(),zw.getMoney(),zw.getCreatetime(),zw.getDescription(),zw.getZhanghu()};
        try {
            qr.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("添加数据失败");
        }
    }

    //按条件查询数据
    public void selectZhangWu(int zwid){
        //查询数据
        String sql="SELECT * FROM gjp_zhangwu WHERE zwid=?";
        try {
            Zhangdan qu = qr.query(sql, new BeanHandler<>(Zhangdan.class),zwid);
            System.out.println(qu.getZwid()+"||"+qu.getFlname()+"||"+qu.getMoney()+"||"+qu.getCreatetime()+"||"+qu.getDescription()+"||"+qu.getZhanghu());
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("查询失败");
        }
    }

    // 查询所有数据
    public void  seletcAllZhangWu(){
        String sql="SELECT * FROM gjp_zhangwu ";
        try {
            List qq = qr.query(sql, new BeanListHandler<>(Zhangdan.class));
            for (Zhangdan zd:qq){
                System.out.println(zd);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("查询所有数据失败");
        }
    }

    //修改信息
    public void updateZhangWu(String name,int zwid){
        String sql="update gjp_zhangwu set flname=? where zwid=?";
        
        try {
            qr.update(sql,name,zwid);
            System.out.println("修改账户名称成功");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("修改信息失败");
        }
    }

    //修改金额
    public void updateZhangWu(double money,int zwid){
        String sql="update gjp_zhangwu set money=? where zwid=?";
        try {
            qr.update(sql,money,zwid);
            System.out.println("修改金额成功");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("修改金额失败");
        }
    }

    //修改时间
    public void updateTimeZhangWu(String createtime, int zwid){
        String sql="update gjp_zhangwu set createtime=? where zwid=?";
        try {
            qr.update(sql,createtime,zwid);
            System.out.println("修改时间成功");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("修改时间失败");
        }
    }

    //修改账户描述
    public void updateMSZhangWu(String description, int zwid){
        String sql="update gjp_zhangwu set description=? where zwid=?";
        try {
            qr.update(sql,description,zwid);
            System.out.println("修改描述成功");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("修改描述失败");
        }
    }

    //修改支付方式
    public void updateZHZhangWu(String zhanghu, int zwid){
        String sql="update gjp_zhangwu set zhanghu=? where zwid=?";
        try {
            qr.update(sql,zhanghu,zwid);
            System.out.println("修改支付方式成功");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("支付方式修改失败");
        }
    }

}



Main App层 运行
package cfg.app;

import cfg.view.MainView;

//运行
public class MainApp {
    public static void main(String[] args) {
        new MainView().run();
    }
}



domain层Gjp_zhangwu 方便后期调用方法 不是必须类
方法重写 获得set get方法 有参构造器
package cfg.domain;
//账务类
public class Gjp_zhangwu {
//    zwid INT PRIMARY KEY AUTO_INCREMENT,
//    flname VARCHAR(60),
//    money DOUBLE,
//#创建时间
//    createtime VARCHAR(60),
//#描述
//    description VARCHAR(60),
//#支出方式  用zhanghu表示
//    zhanghu VARCHAR(60)

    private int zwid;
    private String flname;
    private double money;
    private String createtime;
    private String description;
    private String zhanghu;

//创建有参构造方法

    public Gjp_zhangwu(String flname, double money, String createtime, String description, String zhanghu) {
        this.zwid = zwid;
        this.flname = flname;
        this.money = money;
        this.createtime = createtime;
        this.description = description;
        this.zhanghu = zhanghu;
    }

    //创建set  get 方法

    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 getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getZhanghu() {
        return zhanghu;
    }

    public void setZhanghu(String zhanghu) {
        this.zhanghu = zhanghu;
    }


    //toString

    @Override
    public String toString() {
        return "Gjp_zhangwu{" +
                "zwid=" + zwid +
                ", flname='" + flname + '\'' +
                ", money=" + money +
                ", createtime='" + createtime + '\'' +
                ", description='" + description + '\'' +
                ", zhanghu='" + zhanghu + '\'' +
                '}';
    }
}



Zhangdan类 用于后期结果集的调用 如:XXX.calss
需要重写方法 获得set get方法 表中的变量即数据库中的列名
package cfg.domain;

public class Zhangdan {
    private int zwid;
    private String flname;
    private double money;
    private String createtime;
    private String description;
    private String zhanghu;

    //重写

    @Override
    public String toString() {
        return "Zhangdan{" +
                "zwid=" + zwid +
                ", flname='" + flname + '\'' +
                ", money=" + money +
                ", createtime='" + createtime + '\'' +
                ", description='" + description + '\'' +
                ", zhanghu='" + zhanghu + '\'' +
                '}';
    }

    //get  set


    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 getCreatetime() {
        return createtime;
    }

    public void setCreatetime(String createtime) {
        this.createtime = createtime;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getZhanghu() {
        return zhanghu;
    }

    public void setZhanghu(String zhanghu) {
        this.zhanghu = zhanghu;
    }
}



Untils工具包 C3p0xml工具类
package cfg.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class C3P0xmlUtils {
//    1.在成员位置创建一个静态的ComboPooleDataSource对象
   private static ComboPooledDataSource dataSource =new ComboPooledDataSource();
//    *   2 把c3p0xml文件复制到src  目录下

// * . 3 定义静态方法  从的ComboPooleDataSource对象中获取Connection 并返回
    public static Connection getCollection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException("获得数据库连接失败"+e);
        }
    }
    //定义一个静态方法 可以返回连接池
    public static ComboPooledDataSource getDatesource(){
        return dataSource;
    }
// *   4 定义一个释放资源的方法
    public void close(ResultSet rs, Statement stat,Connection conn){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (stat!=null){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

你可能感兴趣的:(管家婆项目)