Bills.java
package cn.tx1; /** * ID 类别 账户 类型 余额 时间 备注 */ public class Bills { private String name; private String account; private String type; private double total; private String time; private String desc; //全参构造器 public Bills(String name, String account, String type, double total, String time, String desc) { this.name = name; this.account = account; this.type = type; this.total = total; this.time = time; this.desc = desc; } //无参构造器 public Bills() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getType() { return type; } public void setType(String type) { this.type = type; } public double getTotal() { return total; } public void setTotal(double total) { this.total = total; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } }
MainMenu.java
package cn.tx1; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class MainMenu { //创建一个集合来存储 static List<Bills> billsList=new ArrayList<>(); //静态代码块,类加载的时候第一时间执行 “果然还是用到了” static { //String name, String account, String type, double total, String time, String desc billsList.add(new Bills("吃饭支出","建设银行","支出",122247,"2022-1-1","家庭聚餐")); billsList.add(new Bills("吃饭支出","农业银行","支出",111222,"2021-12-11","家庭聚餐")); billsList.add(new Bills("工资收入","农商银行","收入",927227,"2021-11-1","发工资")); billsList.add(new Bills("交通支出","交通银行","支出",999247,"2021-1-1","交通支出")); billsList.add(new Bills("买气球","建设银行","支出",222147,"2025-1-1","买气球")); billsList.add(new Bills("奖金收入","投资银行","收入",908227,"2021-1-1","奖金")); } public static void showMain(){ System.out.println("--------随手记---------"); System.out.println("1、添加账务 2、删除帐务 3、查询账务 4、退出系统"); System.out.println("请输入功能序号【1-4】:"); } public static void main(String[] args) { run(); } public static void run(){ showMain(); //创建一个flag标志,循环条件,变量存在 boolean flag=true; //创建扫描器 Scanner sc=new Scanner(System.in); //获得输入的选项 while (flag){ int num=sc.nextInt(); //判断1、2、3、4 switch (num){ case 1: addBills(); break; case 2: delBills(); break; case 3: select(); break; case 4: flag=false; break; default: System.out.println("请重新输入选项:"); } } System.out.println("退出系统,拜拜!"); } private static void delBills() { Scanner sc2=new Scanner(System.in); int id= sc2.nextInt(); billsList.remove(id-1); System.out.println("删除成功!!"); showMain(); } private static void addBills() { Scanner sc2=new Scanner(System.in); Bills bills=new Bills(); System.out.println("请输入类别:"); bills.setName(sc2.next()); System.out.println("请输入账户:"); bills.setAccount(sc2.next()); System.out.println("请输入收入支出类型:"); bills.setType(sc2.next()); System.out.println("请输入金额::"); bills.setTotal(sc2.nextInt()); System.out.println("请输入时间:"); bills.setTime(sc2.next()); System.out.println("请输入备注:"); bills.setDesc(sc2.next()); billsList.add(bills); System.out.println("财务添加成功!"); } /** * 三种查询 * 1、查询所有 * 2、按照时间区间来查询 * 3、按照收入和支出的类型来查询 */ private static void select(){ System.out.println("随手记>>账务查询"); System.out.println("1、查询所有 2、按照时间区间来查询 3、按照收入和支出的类型来查询"); //创建扫描器 Scanner sc1=new Scanner(System.in); int num=sc1.nextInt(); switch (num){ case 1: selectAll(); break; case 2: selectByData(); break; case 3: selectByType(); break; // default: // System.out.println("亲,请重新输入选项:"); } //展示上一级的主菜单 showMain(); } private static void selectAll(){ System.out.println("查询所有"); print(billsList); } private static void selectByData(){ //创建一个时间格式化对象 [格式化器] SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); System.out.println("按照时间查询"); System.out.println("随手记>>账务查询>>按照时间查询"); //创建扫描器 Scanner sc1=new Scanner(System.in); System.out.println("请输入开始时间:"); String start=sc1.next(); System.out.println("请输入结束时间:"); String end=sc1.next(); //stream 流 List<Bills> billsList1 = billsList.stream().filter(bills -> { String time = bills.getTime(); //把字符串解析成具体时间 try { Date startData = format.parse(start); Date endData = format.parse(end); Date timeData = format.parse(time); if (timeData.before(endData) && timeData.after(startData)) { return true; } } catch (ParseException e) { e.printStackTrace(); } return false; }).collect(Collectors.toList()); print(billsList1); } private static void selectByType(){ System.out.println("按照类型查询"); System.out.println("随手记>>账务查询>>收入和支出类型"); System.out.println("请输入 收入或者支出"); //创建扫描器 Scanner sc1=new Scanner(System.in); String type=sc1.next(); //收入还是支出 //筛选一下集合中type时支出的元素 stream筛选 collectCollectors.toList():将流转换为集合 List<Bills> billsList1 = billsList.stream().filter(bills -> { String type1 = bills.getType(); return type1.equals(type); }).collect(Collectors.toList()); print(billsList1); //filter 过滤 true要,false不要 // stream.filter(bills -> { // String type1= bills.getType(); // return type1.equals(type); // }); } public static void print(List<Bills> billsList){ System.out.println("ID\t\t类别\t\t\t账户\t\t\t类型\t\t金额\t\t\t\t时间\t\t\t\t备注"); for (int i = 0; i < billsList.size(); i++) { Bills bills1=billsList.get(i); System.out.println(i+1+"\t\t"+bills1.getName()+"\t\t"+bills1.getAccount()+"\t\t"+bills1.getType() +"\t\t"+bills1.getTotal()+"\t\t"+bills1.getTime()+"\t\t"+bills1.getDesc()); } } }