java小项目之管家婆

package cn.hncu.mygjp;

public class MainApp {
    public static void main(String[] args) {
        new MainView().run();
    }
}
package cn.hncu.mygjp;

import java.util.List;
import java.util.Scanner;

public class MainView {
    Scanner scanner;
    // ZhangWuController controller = new ZhangWuController();
    ZhangWuDao dao = new ZhangWuDao();

    /*
     * 实现界面效果 接收用户的输入 根据数据,调用不同的功能方法
     */
    public void run() {
        scanner = new Scanner(System.in);
        System.out.println("---------------管家婆家庭记账软件---------------");
        while (true) {
            System.out.println("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统");
            System.out.println("请输入要操作的功能序号[1-5]:");
            int choose = scanner.nextInt();
            switch (choose) {
            case 1:
                //添加方法
                addZhangWu();
                break;
            case 2:
                //编辑方法
                editZhangWu();
                break;
            case 3:
                //删除方法
                deleteZhangWu();
                break;
            case 4:
                // 查询方法
                selectZhangWu();
                break;
            case 5:
                System.exit(0);
            default:
                System.out.println("输入有误,请重新输入");
            }
        }
    }

    private void deleteZhangWu() {
        selectAll();
        System.out.println("请输入需要删除的账务ID");
        scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        dao.deleteZhangWu(choose);
        System.out.println("删除账务成功");
    }

    private void editZhangWu() {
        selectAll();
        System.out.println("选择的是编辑账务功能,请输入以下内容");
        System.out.print("请输入ID:");
        int zwid = scanner.nextInt();
        scanner = new Scanner(System.in);
        System.out.println("输入分类名称:");
        String flname = scanner.next();
        System.out.println("输入金额:");
        double money = scanner.nextDouble();
        System.out.println("输入账户:");
        String zhanghu = scanner.next();
        System.out.println("输入日期:格式XXXX-XX-xx");
        String createtime = scanner.next();
        System.out.println("输入具体描述:");
        String description = scanner.next();
        ZhangWu zw = new ZhangWu(zwid, flname, money, zhanghu, createtime, description);
        dao.editZhangWu(zw);
        System.out.println("编辑账务成功");
    }

    private void selectZhangWu() {
        System.out.println("1. 查询所有    2. 条件查询");
        scanner = new Scanner(System.in);
        int selectChooser = scanner.nextInt();
        switch (selectChooser) {
        case 1:
            selectAll();
            break;
        case 2:
            select();
            break;
        default:
            System.out.println("输入有误");
        }
    }

    private void select() {
         System.out.println("选择条件查询,输入日期格式XXXX-XX-XX");
         scanner = new Scanner(System.in);
         System.out.print("请输入开始日期:");
         String startDate = scanner.nextLine();
         System.out.print("请输入结果日期:");
         String endDate = scanner.nextLine();
         List list = dao.select(startDate,endDate);
         if (list.size() != 0) {
            print(list);
        }else {
            System.out.println("没有查询到数据");
        }
    }

    private void selectAll() {
        List list = dao.selectAll();
        if(list.size() != 0){//如果集合没有元素就没必要输出了
            print(list);
        }else {
            System.out.println("没有查询到数据");
        }

    }

    private void addZhangWu() {
        System.out.println("选择的是添加账务功能,请输入以下内容");
        scanner = new Scanner(System.in);
        System.out.println("输入分类名称:");
        String flname = scanner.next();
        System.out.println("输入金额:");
        double money = scanner.nextDouble();
        System.out.println("输入账户:");
        String zhanghu = scanner.next();
        System.out.println("输入日期:格式XXXX-XX-xx");
        String createtime = scanner.next();
        System.out.println("输入具体描述:");
        String description = scanner.next();
        // 将用户输入的所有参数,封装成ZhangWu对象
        // 将接收到的数据,调用controller层的方法,传递参数,实现数据添加
        // 第一个数据是ID,传什么数都可以
        ZhangWu zw = new ZhangWu(0, flname, money, zhanghu, createtime, description);
        dao.addZhangWu(zw);
        System.out.println("添加账务成功");
    }
    private void print(List list) {
        //输出表头
         System.out.println("ID\t\t类别\t\t账户\t\t金额\t\t时间\t\t说明");
         //遍历集合,结果输出控制台 
         for(ZhangWu zhangWu :list){//集合的每一个元素都是一行账单(zhangWu)
             System.out.println(zhangWu.getZwid()+"\t\t"+zhangWu.getFlname()+"\t\t"+zhangWu.getZhanghu()+"\t\t"+
                     zhangWu.getMoney()+"\t\t"+zhangWu.getCreatetime()+"\t"+zhangWu.getDescription());
         }
    }
}
package cn.hncu.mygjp;

public class ZhangWu {
    private int zwid;
    private String flname;
    private double money;
    private String zhanghu;
    private String createtime;
    private String description;
    public ZhangWu(){
        super();
    }
    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;
    }
    public String toString() {
        return "ZhangWu [zwid=" + zwid + ", flname=" + flname + ", money=" + money + ", zhanghu=" + zhanghu
                + ", createtime=" + createtime + ", description=" + description + "]";
    }

}
package cn.hncu.mygjp;

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("951218");

        dataSource.setMaxActive(10);// 连接池的最大连接数
        dataSource.setMaxIdle(5);// 最大使用数
        dataSource.setMinIdle(2);// 最小使用数
        dataSource.setInitialSize(10);//
    }

    public static DataSource getDataSource() {
        return dataSource;
    }

}
package cn.hncu.mygjp;

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

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class ZhangWuDao {
    private QueryRunner qRunner = new QueryRunner(JDBCUtils.getDataSource());// 工具类里的

    public void addZhangWu(ZhangWu zw) {
        String sqlString = "INSERT INTO gjp_zhangwu (flname,money,zhanghu,createtime,description) VALUES(?,?,?,?,?)";
        // 创建对象数组,储存5个占位符的实际参数
        // 实际参数来源是传递过来的对象ZhangWu
        Object[] param = { zw.getFlname(), zw.getMoney(), zw.getZhanghu(), zw.getCreatetime(), zw.getDescription() };
        try {
            qRunner.update(sqlString, param);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("添加账务失败");
            throw new RuntimeException();
        }
    }

    public List selectAll() {
        String sql = "SELECT * FROM gjp_zhangwu";
        List list;
        try {
            // 调用qRunner对象的方法,query方法,结果集BeanListHandler
            /* point */list = qRunner.query(sql, new BeanListHandler<>(ZhangWu.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("查询失败");// 本来是需要另一个return的,但这有一个运行时异常就会使程序停止
        }

    }

    public List select(String startDate, String endDate) {
        String sql = "SELECT * FROM gjp_zhangwu WHERE createtime BETWEEN ? AND ?";
        List list;
        try {
            Object[] params = {startDate,endDate};
            list = qRunner.query(sql,new BeanListHandler<>(ZhangWu.class),params);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("条件查询失败");
        }
    }

    public void editZhangWu(ZhangWu zw) {
        String sql = "UPDATE gjp_zhangwu SET flname=?,money=?,zhanghu=?,createtime=?,description=? WHERE zwid=?";
        Object []params = {zw.getFlname(),zw.getMoney(),zw.getZhanghu(),zw.getCreatetime(),zw.getDescription(),zw.getZwid()};
        try {
            qRunner.update(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("编辑失败");
        }
    }

    public void deleteZhangWu(int choose) {
        String sql = "DELETE FROM gjp_zhangwu WHERE zwid=?";
        try {
            qRunner.update(sql, choose);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("删除账务失败");
        }
    }

}

你可能感兴趣的:(小项目)