资料来源:https://www.bilibili.com/video/BV1Aq4y1W79E?p=3
1、Bills
package cn.tx.menu;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
// 每笔账单
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Bills {
// 类别
private String name;
// 金额
private double total;
// 账户
private String account;
// 类型
private String type;
// 时间
private String time;
// 备注
private String desc;
}
2、MainMenu
package cn.tx.menu;
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;
/**
* 随手记开发系统
*/
public class MainMenu {
// 创建一个集合来存储账单
static List<Bills> billList = new ArrayList<Bills>();
// 类加载的时候会第一时间执行静态代码块,在billsList里面加入初始化的数据
static {
billList.add(new Bills("吃饭支出", 247, "交行", "支出", "2021-03-02", "家庭聚餐"));
billList.add(new Bills("吃饭支出", 247, "交行", "支出", "2021-03-02", "家庭聚餐"));
billList.add(new Bills("工资收入", 12345, "现金", "收入", "2021-03-15", "开工资了"));
billList.add(new Bills("服装支出", 1998, "现金", "支出", "2021-04-02", "买衣服"));
billList.add(new Bills("吃饭支出", 325, "现金", "支出", "2021-06-18", "朋友聚餐"));
billList.add(new Bills("股票收入", 8000, "工行", "收入", "2021-07-28", "股票大涨"));
billList.add(new Bills("股票收入", 5000, "工行", "收入", "2021-08-28", "股票又大涨"));
billList.add(new Bills("工资收入", 5000, "交行", "收入", "2021-09-28", "又开工资了"));
billList.add(new Bills("礼金支出", 5000, "现金", "支出", "2021-11-28", "朋友结婚"));
billList.add(new Bills("其他支出", 1560, "现金", "支出", "2021-12-29", "丢钱了"));
billList.add(new Bills("交通支出", 2300, "交行", "支出", "2020-10-29", "油价还在涨啊"));
billList.add(new Bills("吃饭支出", 1000, "工行", "支出", "2020-09-29", "又吃饭"));
billList.add(new Bills("工资收入", 1000, "现金", "收入", "2020-11-30", "开资"));
billList.add(new Bills("交通支出", 2000, "现金", "支出", "2020-07-30", "机票好贵"));
billList.add(new Bills("工资收入", 5000, "现金", "收入", "2020-05-30", "又开资"));
}
public static void main(String[] args) {
run();
}
public static void showMain(){
System.out.println("------------随手记------------");
System.out.println("1.添加账务 2.删除账务 3.查询账务 4.退出系统");
System.out.println("请输入功能序号【1-4】");
}
public static void run(){
showMain();
// 创建一个flag标志
boolean flag = true;
// 创建扫描器
Scanner scanner = new Scanner(System.in);
while (flag){
// 获取输入的选项
int op = scanner.nextInt();
// 判断1、2、3、4
switch(op){
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 scanner = new Scanner(System.in);
System.out.println("请输入要删除账务的ID:");
int id = scanner.nextInt();
billList.remove(id-1);
System.out.println("删除成功");
showMain();
}
/**
* 添加账务
*/
private static void addBills() {
Scanner scanner = new Scanner(System.in);
Bills bills = new Bills();
System.out.println("请输入类别:");
bills.setName(scanner.next());
System.out.println("请输入账户:");
bills.setAccount(scanner.next());
System.out.println("请输入支出收入类型:");
bills.setType(scanner.next());
System.out.println("请输入金额:");
bills.setTotal(scanner.nextDouble());
System.out.println("请输入时间:");
bills.setTime(scanner.next());
System.out.println("请输入备注:");
bills.setDesc(scanner.next());
billList.add(bills);
System.out.println("添加账务成功!");
showMain();
}
/**
* 支持三种查询
* 1、查询所有
* 2、按着时间区间来查询
* 3、收入和支出的类型查询
*/
private static void select() {
System.out.println("随手记 >> 账务查询");
System.out.println("1. 查询所有,2. 按着时间区间来查询,3. 收入和支出的类型查询");
// 创建扫描器
Scanner scanner = new Scanner(System.in);
int op = scanner.nextInt();
switch (op){
case 1:
selectAll();
break;
case 2:
selectByDate();
break;
case 3:
selectByType();
break;
}
// 返回到主菜单
showMain();
}
private static void selectByDate() {
// 创建时间格式化的对象
SimpleDateFormat dateFormat = 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();
List<Bills> bills1 = billList.stream().filter(bills -> {
String time = bills.getTime();
// 把字符串解析成具体的时间
try {
Date startDate = dateFormat.parse(start);
Date endDate = dateFormat.parse(end);
Date timeDate = dateFormat.parse(time);
if((timeDate.before(endDate) && timeDate.after(startDate)) || timeDate.equals(startDate) || timeDate.equals(endDate)){
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}).collect(Collectors.toList());
print(bills1);
}
private static void selectByType() {
System.out.println("随手记 >> 账务查询 >> 收入和支出的类型查询");
System.out.println("1. 收入,2. 支出");
// 创建扫描器
Scanner scanner = new Scanner(System.in);
String type = scanner.next();
// 筛选一下集合中type是支出或收入的数据
List<Bills> bills1 = billList.stream().filter(bills -> bills.getType().equals(type)).collect(Collectors.toList());
print(bills1);
}
private static void selectAll() {
print(billList);
}
public static void print(List<Bills> billsList){
System.out.println("ID\t\t类别\t\t账户\t\t类型\t\t金额\t\t时间\t\t\t备注");
for (int i = 0; i < billsList.size(); i++) {
Bills bills = billsList.get(i);
System.out.println((i+1) + "\t\t" + bills.getName() + "\t\t" + bills.getAccount() + "\t\t" + bills.getType()
+ "\t\t" + bills.getTotal() + "\t\t" + bills.getTime() + "\t\t" + bills.getDesc());
}
}
}