资料来源:https://www.bilibili.com/video/BV1Aq4y1W79E?p=3

1、Bills

  1. package cn.tx.menu;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import lombok.NoArgsConstructor;
  5. // 每笔账单
  6. @Data
  7. @NoArgsConstructor
  8. @AllArgsConstructor
  9. public class Bills {
  10. // 类别
  11. private String name;
  12. // 金额
  13. private double total;
  14. // 账户
  15. private String account;
  16. // 类型
  17. private String type;
  18. // 时间
  19. private String time;
  20. // 备注
  21. private String desc;
  22. }

2、MainMenu

  1. package cn.tx.menu;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. import java.util.Scanner;
  8. import java.util.stream.Collectors;
  9. /**
  10. * 随手记开发系统
  11. */
  12. public class MainMenu {
  13. // 创建一个集合来存储账单
  14. static List<Bills> billList = new ArrayList<Bills>();
  15. // 类加载的时候会第一时间执行静态代码块,在billsList里面加入初始化的数据
  16. static {
  17. billList.add(new Bills("吃饭支出", 247, "交行", "支出", "2021-03-02", "家庭聚餐"));
  18. billList.add(new Bills("吃饭支出", 247, "交行", "支出", "2021-03-02", "家庭聚餐"));
  19. billList.add(new Bills("工资收入", 12345, "现金", "收入", "2021-03-15", "开工资了"));
  20. billList.add(new Bills("服装支出", 1998, "现金", "支出", "2021-04-02", "买衣服"));
  21. billList.add(new Bills("吃饭支出", 325, "现金", "支出", "2021-06-18", "朋友聚餐"));
  22. billList.add(new Bills("股票收入", 8000, "工行", "收入", "2021-07-28", "股票大涨"));
  23. billList.add(new Bills("股票收入", 5000, "工行", "收入", "2021-08-28", "股票又大涨"));
  24. billList.add(new Bills("工资收入", 5000, "交行", "收入", "2021-09-28", "又开工资了"));
  25. billList.add(new Bills("礼金支出", 5000, "现金", "支出", "2021-11-28", "朋友结婚"));
  26. billList.add(new Bills("其他支出", 1560, "现金", "支出", "2021-12-29", "丢钱了"));
  27. billList.add(new Bills("交通支出", 2300, "交行", "支出", "2020-10-29", "油价还在涨啊"));
  28. billList.add(new Bills("吃饭支出", 1000, "工行", "支出", "2020-09-29", "又吃饭"));
  29. billList.add(new Bills("工资收入", 1000, "现金", "收入", "2020-11-30", "开资"));
  30. billList.add(new Bills("交通支出", 2000, "现金", "支出", "2020-07-30", "机票好贵"));
  31. billList.add(new Bills("工资收入", 5000, "现金", "收入", "2020-05-30", "又开资"));
  32. }
  33. public static void main(String[] args) {
  34. run();
  35. }
  36. public static void showMain(){
  37. System.out.println("------------随手记------------");
  38. System.out.println("1.添加账务 2.删除账务 3.查询账务 4.退出系统");
  39. System.out.println("请输入功能序号【1-4】");
  40. }
  41. public static void run(){
  42. showMain();
  43. // 创建一个flag标志
  44. boolean flag = true;
  45. // 创建扫描器
  46. Scanner scanner = new Scanner(System.in);
  47. while (flag){
  48. // 获取输入的选项
  49. int op = scanner.nextInt();
  50. // 判断1、2、3、4
  51. switch(op){
  52. case 1:
  53. addBills();
  54. break;
  55. case 2:
  56. delBills();
  57. break;
  58. case 3:
  59. select();
  60. break;
  61. case 4:
  62. flag = false;
  63. break;
  64. default:
  65. System.out.println("请重新输入:");
  66. }
  67. }
  68. System.out.println("退出系统");
  69. }
  70. /**
  71. * 删除账务
  72. */
  73. private static void delBills() {
  74. Scanner scanner = new Scanner(System.in);
  75. System.out.println("请输入要删除账务的ID:");
  76. int id = scanner.nextInt();
  77. billList.remove(id-1);
  78. System.out.println("删除成功");
  79. showMain();
  80. }
  81. /**
  82. * 添加账务
  83. */
  84. private static void addBills() {
  85. Scanner scanner = new Scanner(System.in);
  86. Bills bills = new Bills();
  87. System.out.println("请输入类别:");
  88. bills.setName(scanner.next());
  89. System.out.println("请输入账户:");
  90. bills.setAccount(scanner.next());
  91. System.out.println("请输入支出收入类型:");
  92. bills.setType(scanner.next());
  93. System.out.println("请输入金额:");
  94. bills.setTotal(scanner.nextDouble());
  95. System.out.println("请输入时间:");
  96. bills.setTime(scanner.next());
  97. System.out.println("请输入备注:");
  98. bills.setDesc(scanner.next());
  99. billList.add(bills);
  100. System.out.println("添加账务成功!");
  101. showMain();
  102. }
  103. /**
  104. * 支持三种查询
  105. * 1、查询所有
  106. * 2、按着时间区间来查询
  107. * 3、收入和支出的类型查询
  108. */
  109. private static void select() {
  110. System.out.println("随手记 >> 账务查询");
  111. System.out.println("1. 查询所有,2. 按着时间区间来查询,3. 收入和支出的类型查询");
  112. // 创建扫描器
  113. Scanner scanner = new Scanner(System.in);
  114. int op = scanner.nextInt();
  115. switch (op){
  116. case 1:
  117. selectAll();
  118. break;
  119. case 2:
  120. selectByDate();
  121. break;
  122. case 3:
  123. selectByType();
  124. break;
  125. }
  126. // 返回到主菜单
  127. showMain();
  128. }
  129. private static void selectByDate() {
  130. // 创建时间格式化的对象
  131. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  132. System.out.println("随手记 >> 账务查询 >> 按着时间区间来查询");
  133. // 创建扫描器
  134. Scanner scanner = new Scanner(System.in);
  135. System.out.println("请输入:开始时间");
  136. String start = scanner.next();
  137. System.out.println("请输入:结束时间");
  138. String end = scanner.next();
  139. List<Bills> bills1 = billList.stream().filter(bills -> {
  140. String time = bills.getTime();
  141. // 把字符串解析成具体的时间
  142. try {
  143. Date startDate = dateFormat.parse(start);
  144. Date endDate = dateFormat.parse(end);
  145. Date timeDate = dateFormat.parse(time);
  146. if((timeDate.before(endDate) && timeDate.after(startDate)) || timeDate.equals(startDate) || timeDate.equals(endDate)){
  147. return true;
  148. }
  149. } catch (ParseException e) {
  150. e.printStackTrace();
  151. }
  152. return false;
  153. }).collect(Collectors.toList());
  154. print(bills1);
  155. }
  156. private static void selectByType() {
  157. System.out.println("随手记 >> 账务查询 >> 收入和支出的类型查询");
  158. System.out.println("1. 收入,2. 支出");
  159. // 创建扫描器
  160. Scanner scanner = new Scanner(System.in);
  161. String type = scanner.next();
  162. // 筛选一下集合中type是支出或收入的数据
  163. List<Bills> bills1 = billList.stream().filter(bills -> bills.getType().equals(type)).collect(Collectors.toList());
  164. print(bills1);
  165. }
  166. private static void selectAll() {
  167. print(billList);
  168. }
  169. public static void print(List<Bills> billsList){
  170. System.out.println("ID\t\t类别\t\t账户\t\t类型\t\t金额\t\t时间\t\t\t备注");
  171. for (int i = 0; i < billsList.size(); i++) {
  172. Bills bills = billsList.get(i);
  173. System.out.println((i+1) + "\t\t" + bills.getName() + "\t\t" + bills.getAccount() + "\t\t" + bills.getType()
  174. + "\t\t" + bills.getTotal() + "\t\t" + bills.getTime() + "\t\t" + bills.getDesc());
  175. }
  176. }
  177. }