我打算逐步将小鲨鱼记账本优化,这次没有用到数据库,等出到2.0版本再用
本次的核心是查找数据用了stream流(我的另一篇简单介绍了什么是Stream流)
用到的比较简单,用于对Java基础的巩固加深,下一次的2.0版正好再对数据库的知识进行复习
如果有机会出3.0就把学的前段一些知识再用上(...够呛了,我努力找找资源)
这是1.0版本的视频(讲的挺好的,我把源码分享给大家,源码是我自己打的哦)
每天1个java练习小项目,java7日项目思维养成_哔哩哔哩_bilibilihttps://www.bilibili.com/video/BV1Aq4y1W79E?p=3*这是账单对象:
package app;
//这是一个账单对象
//ID 类别 账户 类型 金额 时间 备注
//创建类一般流程 用private;
// 之后无参有参构造函数;
//用getter和setter
public class Object_Bill {private String name;//吃饭 娱乐private String account;private String type;private double total;private String time;private String desc;public Object_Bill() { //无参构造器}//有参构造器public Object_Bill(String name, double total, String account, String type, String time, String desc) {this.name = name;this.account = account;this.type = type;this.total = total;this.time = time;this.desc = desc;}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;}}
*这是主体:
package app;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Project_01 {//创建一个集合来储存,数组static List<Object_Bill> billsList=new ArrayList<>();//通过静态代码块来首先加载一些数据//只会实现一次,就算你创建了20个对象,只有第一个对象会执行//为了现实3;所以先用静态代码块static{billsList.add(new Object_Bill("吃饭支出",247,"交行","支出","2021-01-23","家庭聚会"));billsList.add(new Object_Bill("工资收入",23443,"现金","收入","2022-02-11","发工资"));billsList.add(new Object_Bill("服装支出",666,"微信","支出","2021-01-23","买衣服"));billsList.add(new Object_Bill("股票收入",8888,"交行","收入","2022-01-23","股票大涨"));billsList.add(new Object_Bill("礼金支出",888,"农行","支出","2022-1-01","给长辈买礼物"));}public static void main(String[] args) {menu();run();}public static void menu(){//1.首先显示主界面System.out.println("------小鲨鱼随手记--------");System.out.println("1.添加账务 2.删除账务 3.查询账务 4.退出系统");System.out.println("选择上述服务");}public static void run(){//之后获取输入的文字ScannerScanner sc=new Scanner(System.in);boolean flag=true;while(flag) {int num=sc.nextInt();switch (num) {case 1:System.out.println("你选择的是添加账务");add();break;case 2:System.out.println("你选择的是删除账务");delete();break;case 3:System.out.println("你选择的是查询账务");select();break;case 4:System.out.println("你选择的是退出系统");flag=false;//用标记退出系统,break退出Switch;while退出whilebreak;default:System.out.println("请重新输入");}}}public static void add(){// public Object_Bill(String name, double total, String account, String type, String time, String desc)Scanner scs=new Scanner(System.in);Object_Bill bill =new Object_Bill();System.out.println("请输入支出收入类型:");bill.setType(scs.next());System.out.println("请输入类别:");bill.setName(scs.next());System.out.println("请输入账户:");bill.setAccount(scs.next());System.out.println("请输入金额:");bill.setTotal(scs.nextDouble());System.out.println("请输入时间:");bill.setTime(scs.next());System.out.println("请输入备注:");bill.setDesc(scs.next());billsList.add(bill);System.out.println("添加账务成功!");menu();}public static void delete(){Scanner sc=new Scanner(System.in);System.out.println("选择你要删除的序号数:");int num =sc.nextInt();Object_Bill bill=billsList.get(num-1);billsList.remove(num-1);System.out.println("删除账务成功!删除的是:");System.out.println("ID\t\t类别\t\t\t账户\t\t类型\t\t金额\t\t\t时间\t\t\t\t备注");System.out.println(num+"\t\t"+bill.getName()+"\t\t\t"+bill.getTotal()+"\t\t"+bill.getAccount()+"\t\t"+bill.getType()+"\t\t\t"+bill.getTime()+"\t\t\t\t"+bill.getDesc());menu();}public static void select(){System.out.println("账务查询具体功能如下");System.out.println("1.查询所有 2.按照时间区间来查询 3.收入和支出的类型查询");System.out.println("选择上述服务类型");Scanner sc1=new Scanner(System.in);int num1=sc1.nextInt();switch (num1){case 1:selectAll();break;case 2:selectByDate();break;case 3:selectByType();break;}menu();}private static void selectAll(){printAll(billsList);}private static void selectByDate() {//将字符串反向解析为时间SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");System.out.println("小鲨鱼--账务查询--收入和支出类型");Scanner scanner = new Scanner(System.in);System.out.println("输入开始时间");String start = scanner.next();System.out.println("输入结束时间");String end = scanner.next();//还是用stream流List<Object_Bill> billDate= billsList.stream().filter(bills->{String time=bills.getTime();try {Date startDate= format.parse(start);//用try catch 抛出异常Date endDate= format.parse(end);Date dateTime=format.parse(time);if (dateTime.before(endDate)&&dateTime.after(startDate)) {return true;}} catch (ParseException e) {e.printStackTrace();}return false;}).collect(Collectors.toList());printAll(billDate);}private static void selectByType(){System.out.println("小鲨鱼--账务查询--收入和支出类型");System.out.println("输入收入或者支出");Scanner scanner = new Scanner(System.in);String type = scanner.next();//Stream流(lamda表达式)//进行过滤//集合Stream
// Stream<Object_Bill> stream=billsList.stream();
// stream.filter(bills->{
// String type1=bills.getType();
// return type.equals(type1);//equals比较
// });List<Object_Bill> billsList1=billsList.stream().filter(bills -> {String type1 = bills.getType();return type.equals(type1);})//equals比较.collect(Collectors.toList());//这是把流转换为集合printAll(billsList1);}public static void printAll(List<Object_Bill> billsList){System.out.println("ID\t\t类别\t\t\t账户\t\t类型\t\t金额\t\t\t时间\t\t\t\t备注");for (int i = 0; i <billsList.size() ; i++) {Object_Bill bill=billsList.get(i);System.out.println(i+1+"\t\t"+bill.getName()+"\t\t\t"+bill.getTotal()+"\t\t"+bill.getAccount()+"\t\t"+bill.getType()+"\t\t\t"+bill.getTime()+"\t\t\t\t"+bill.getDesc());}}}