赞
踩
作为java初学者,程序里面可能会有一些写的不好的地方,请见谅。作为一个从C++转Java的初学者,还不会使用Java中的文件读写,所以后面的代码中,是将数据存储在一个列表中的,而一般涉及大量数据存储的程序,一般是将数据存储在数据库或者文件中的。好啦,不说了,我们开始吧~
定义一个Bills类用来存储记录,记录一条账务,我们需要有:账务类别、金额、使用的账户、收入或支出类型、时间、备注。
直接看代码吧。
- public class Bills {
- private String name; //类别
- private double total; //金额
- private String account; //账户
- private String type; //类型
- private String time; //时间
- private String desc; //备注
-
- public Bills() {
- }
-
- public Bills(String name, double total, String account, String type, String time, String desc) {
- this.name = name;
- this.total = total;
- this.account = account;
- this.type = type;
- this.time = time;
- this.desc = desc;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public double getTotal() {
- return total;
- }
-
- public void setTotal(double total) {
- this.total = total;
- }
-
- 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 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;
- }
- }
记账程序主要有添加账务、删除账务、查询账务、退出系统这几个基本功能。菜单我们直接打印就行了。
- public static void showMain()
- {
- System.out.println("----------------------随手记----------------------");
- System.out.println("1.添加账务 2.删除账务 3.查询账务 4.退出系统");
- System.out.println("请输入功能序号【1-4】");
- }
有了菜单后,我们还需要根据菜单选择对应的功能,然后执行该功能。所以我们需要用户输入自己的选择,然后使用switch-case语句判断。
- public static void run()
- {
- showMain();
-
- boolean flag=true;
- //创建扫描器
- Scanner scanner=new Scanner(System.in);
- while(flag)
- {
- //获得输入的选项
- int op=scanner.nextInt();
- 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("退出系统");
- }
在开始对于数据的操作之前,我们还需要创建一个集合来存储数组,而数组中就是我们初始化的账务数据。
- static List<Bills> billList=new ArrayList<>();
-
- 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("交通支出",100,"交行","支出","2021-03-20","坐高铁"));
- }
创建一个Bills对象来存储用户输入的临时数据,然后将这条数据追加到集合中。
- private static void addBills()
- {
- Scanner inScanner=new Scanner(System.in);
- Bills bills=new Bills();
-
- System.out.println("请输入类别:");
- bills.setName(inScanner.next());
- System.out.println("请输入账户:");
- bills.setAccount(inScanner.next());
- System.out.println("请输入支出类型:");
- bills.setType(inScanner.next());
- System.out.println("请输入金额:");
- bills.setTotal(inScanner.nextDouble());
- System.out.println("请输入时间:");
- bills.setTime(inScanner.next());
- System.out.println("请输入备注:");
- bills.setDesc(inScanner.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();
- }
我们需要先设置打印的格式。
- 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备注");
- 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());
- }
- }
打印所有记录。
- private static void selectAll()
- {
- print(billList);
- }
- private static void selectByDate()
- {
- //创建一个时间格式化的对象
- SimpleDateFormat format=new SimpleDateFormat("yyy-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> billsList=billList.stream().filter(bills -> {
- String time=bills.getTime();
- //把字符串解析成具体时间
- try
- {
- Date startDate=format.parse(start);
- Date endDate=format.parse(end);
- Date timeDate=format.parse(time);
- if(timeDate.before(endDate)&&timeDate.after(startDate))
- {
- return true;
- }
- }
- catch (ParseException e)
- {
- e.printStackTrace();
- }
- return false;
- }).collect(Collectors.toList());
-
- print(billsList);
-
- }
- private static void selectByType()
- {
- System.out.println("随手记>>账务查询>>收入和支出的类型");
- System.out.println("请输入:收入或者支出");
- Scanner scanner=new Scanner(System.in);
- //收入,支出
- String type=scanner.next();
- //集合stream流,集合的筛选
- List<Bills> billsList=billList.stream()
- .filter(bills -> {
- String type1=bills.getType();
- return type1.equals(type);
- }).collect(Collectors.toList());
- print(billsList);
- }
- private static void delBills()
- {
- Scanner inScanner=new Scanner(System.in);
- int id=inScanner.nextInt();
- billList.remove(id-1);
- System.out.println("删除成功!");
- showMain();
- }
Bills.java
- package cn.tx;
-
- public class Bills {
- private String name; //类别
- private double total; //金额
- private String account; //账户
- private String type; //类型
- private String time; //时间
- private String desc; //备注
-
- public Bills() {
- }
-
- public Bills(String name, double total, String account, String type, String time, String desc) {
- this.name = name;
- this.total = total;
- this.account = account;
- this.type = type;
- this.time = time;
- this.desc = desc;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public double getTotal() {
- return total;
- }
-
- public void setTotal(double total) {
- this.total = total;
- }
-
- 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 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.tx;
-
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Scanner;
- import java.util.List;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
-
- public class MainMenu {
-
- //创建一个集合来存储数组
- static List<Bills> billList=new ArrayList<>();
-
- //类加载时会第一时间执行,在billList里面加入初始化数据
- 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("交通支出",100,"交行","支出","2021-03-20","坐高铁"));
- }
-
- 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();
-
- boolean flag=true;
- //创建扫描器
- Scanner scanner=new Scanner(System.in);
- while(flag)
- {
- //获得输入的选项
- int op=scanner.nextInt();
- 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 inScanner=new Scanner(System.in);
- int id=inScanner.nextInt();
- billList.remove(id-1);
- System.out.println("删除成功!");
- showMain();
- }
-
- private static void addBills()
- {
- Scanner inScanner=new Scanner(System.in);
- Bills bills=new Bills();
-
- System.out.println("请输入类别:");
- bills.setName(inScanner.next());
- System.out.println("请输入账户:");
- bills.setAccount(inScanner.next());
- System.out.println("请输入支出类型:");
- bills.setType(inScanner.next());
- System.out.println("请输入金额:");
- bills.setTotal(inScanner.nextDouble());
- System.out.println("请输入时间:");
- bills.setTime(inScanner.next());
- System.out.println("请输入备注:");
- bills.setDesc(inScanner.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 selectAll()
- {
- print(billList);
- }
-
- private static void selectByDate()
- {
- //创建一个时间格式化的对象
- SimpleDateFormat format=new SimpleDateFormat("yyy-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> billsList=billList.stream().filter(bills -> {
- String time=bills.getTime();
- //把字符串解析成具体时间
- try
- {
- Date startDate=format.parse(start);
- Date endDate=format.parse(end);
- Date timeDate=format.parse(time);
- if(timeDate.before(endDate)&&timeDate.after(startDate))
- {
- return true;
- }
- }
- catch (ParseException e)
- {
- e.printStackTrace();
- }
- return false;
- }).collect(Collectors.toList());
-
- print(billsList);
-
- }
-
- private static void selectByType()
- {
- System.out.println("随手记>>账务查询>>收入和支出的类型");
- System.out.println("请输入:收入或者支出");
- Scanner scanner=new Scanner(System.in);
- //收入,支出
- String type=scanner.next();
- //集合stream流,集合的筛选
- List<Bills> billsList=billList.stream()
- .filter(bills -> {
- String type1=bills.getType();
- return type1.equals(type);
- }).collect(Collectors.toList());
- print(billsList);
- }
-
- 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备注");
- 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());
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。