随手记系统java项目
//使用jdk1.8新特性 集合查询(筛选)
//集合Stream流
List<Pojo> list= pojoList.stream().filter(pojo -> {
String type2 = pojo.getType();
return type2.equals(type);
}).collect(Collectors.toList());
print(list);


遇到一个问题:如果不是使方法输出而是使用
System.out.println(list);
则会出现这样的情况:
com.tang.@.........


功能类:
package com.tang;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;


public class MainMeau {

//创建一个集合
static List<Pojo> pojoList = new ArrayList<>();

static {
pojoList.add(new Pojo("吃饭支出",247,"交行","支出","2021-03-02","家庭聚餐"));
pojoList.add(new Pojo("吃饭支出",247,"交行","支出","2021-03-02","家庭聚餐"));
pojoList.add(new Pojo("工资收入",12345,"现金","收入","2021-05-07","发工资"));
pojoList.add(new Pojo("服装支出",1998,"现金","支出","2021-06-02","买衣服"));
pojoList.add(new Pojo("股票收入",325,"工行","收入","2021-08-12","股票大涨"));
pojoList.add(new Pojo("股票收入",5000,"工行","收入","2021-09-22","家股票大涨"));
pojoList.add(new Pojo("工资收入",6000,"交行","收入","2021-10-02","发工资"));
pojoList.add(new Pojo("礼金支出",3000,"现金","支出","2021-11-02","朋友结婚"));
}
public static void main(String[] args) {
run();

}
public static void showMeau(){
System.out.println("------------随手记------------");
System.out.println("1.添加账务 2.删除账务 3.查询账务 4.退出系统");
System.out.println("请输入功能序号[1-4]");

}

public static void run() {

showMeau();

boolean flag = true;
//接受用户输入的数据
Scanner op = new Scanner(System.in);

while (flag) {
int i = op.nextInt();
switch (i) {
case 1:
addPojo();
break;

case 2:
deletePojo();
break;

case 3:
select();
break;

case 4:
flag = false;
break;

default:
System.out.println("请重新输入");
}

}
System.out.println("退出系统拜拜");

}

private static void deletePojo() {
System.out.println("请选择要删除哪一个输入序号: 比如1");
Scanner scanner = new Scanner(System.in);
int id = scanner.nextInt();
pojoList.remove(id-1);
System.out.println("删除成功");
showMeau();
}

private static void addPojo() {
Scanner inscanner = new Scanner(System.in);

Pojo pojo =new Pojo();
System.out.println("请输入ID");
pojo.setName(inscanner.next());
System.out.println("请输入金额");
pojo.setTotal(inscanner.nextDouble());
System.out.println("请输入账户");
pojo.setAccount(inscanner.next());
System.out.println("请输入类型");
pojo.setType(inscanner.next());
System.out.println("请输入时间");
pojo.setTime(inscanner.next());
System.out.println("请输入备注");
pojo.setDesc(inscanner.next());

pojoList.add(pojo);
System.out.println("添加账务成功");
showMeau();

}

/*
创建三种查询 1.查询全部 2.根据时间查询 3.根据类型查询
*/
private static void select() {
System.out.println("-----------------查询账务--------------------");
System.out.println("1.查询全部 2.根据时间查询 3.根据类型查询");
//创建扫描器
Scanner scanner = new Scanner(System.in);

int i = scanner.nextInt();

switch (i){
case 1:
selectAll();
break;

case 2:
selectByDate();
break;
case 3:
selectByType();
break;

}
//返回主菜单
showMeau();
}

private static void selectByType() {
//按照类型查询
System.out.println("---------------收入和支出的账务---------------");
System.out.println("请输入: 收入或者支出来查询");

Scanner scanner = new Scanner(System.in);

//收入 支出
String type = scanner.next();
//使用jdk1.8新特性 集合查询
//集合Stream流
List<Pojo> list= pojoList.stream().filter(pojo -> {
String type2 = pojo.getType();
return type2.equals(type);
}).collect(Collectors.toList());
print(list);



}


private static void selectByDate() {
//创建一个时间的个数
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//按照时间查询
System.out.println("---------------根据时间查询账务---------------");
System.out.println("格式为: 2021-02-01");
Scanner scanner = new Scanner(System.in);
System.out.println("请输入: 开始时间");
String start = scanner.next();

System.out.println("请输入: 结束时间");
String end =scanner.next();

List<Pojo> list = pojoList.stream().filter(pojo -> {
String time = pojo.getTime();

try {
Date startDate = format.parse(start);
Date endsDate = format.parse(end);
Date timeDate = format.parse(time);
if (timeDate.after(startDate) && timeDate.before(endsDate)) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;


}).collect(Collectors.toList());
print(list);


}

private static void selectAll() {
print(pojoList);

}

private static void print(List<Pojo> pojoList) {
System.out.println("ID\t\t金额\t\t账户\t\t类型\t\t时间\t\t备注");
for (int i = 0; i < pojoList.size(); i++) {
Pojo pojo = pojoList.get(i);
System.out.println(pojo.getName()+"\t"+pojo.getTotal()+"\t"+pojo.getAccount()+"\t"+pojo.getType()+"\t"+pojo.getTime()+"\t"+pojo.getDesc());
}
}
}


实体类():
package com.tang;

/*
ID 类别 账户 类型 余额 时间 备注
*/
public class Pojo {
private String name;
private String account;
private String type;
private double total;
private String time;
private String desc;

public Pojo(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 Pojo() {

}

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;
}
}

 

 



posted on 2022-03-31 00:04  微笑阿凡达  阅读(125)  评论(0)    收藏  举报