Java小项目2------记账小系统
要求:::
主页面显示
--------记账本----------
- 添加业务 2.删除业务 3.查询业务 4.退出系统
请输入编号:::
按1进入添加业务
----------进入添加业务页面----------
请输入ID(这里需要我们手动进行输入)
请输入类型(这里需要我们手动进行输入)
请输入银行(这里需要我们手动进行输入)
请输入类别(这里需要我们手动进行输入)
请输入金额(这里需要我们手动进行输入)
请输入时间(这里需要我们手动进行输入)
请输入备注(这里需要我们手动进行输入)
添加成功!
(返回主页面)
按2删除业务
(根据ID进行删除)
-------进入删除业务页面------
请输入要删除业务的编号
(输入)
删除成功or 该编号不存在
按3查询业务
--------进入查询业务页面-------
- 全部查询 2.按id查询 3.按时间范围查询 4.按类别查询
按1 输出所有的业务
按2 请输入编号 (输入) 显示该业务or该编号存在
按3 请输入开始时间(输入) 请输入结束时间(输入)
显示or该时间段内无记账记录
按4 请输入要查询业务的类别: 支出或者收入 (输入) 显示
按4退出业务
直接结束运行
思路::其实这个项目和之前的点菜系统是很相似的,有几个新的知识点::
1、根据类别查询集合list的时候需要使用stream流
List<Count> ll= List.stream.filter(
count ->{
String style=count.getStyle();
Return style.equals(ss)}
).collect(Collector.toList)
这里Count是我创建的账户类,style是类别属性,ss是我的输入。
2、根据时间范围进行查询的时候使用到了 SimpleDateFormat类(该类可以对时间的格式进行统一)。在进行查询时我们需要对输入的开始时间、结束时间以及list类中账户时间都进行统一使用SimpleDateFormat类中prase方法。然后在进行判断时我们使用到了after和before两个方法。
3、在该项目中我们还使用到了按4退出,之前点菜系统是一直循环的。可以使用一个boolean类型的数据进行标记,刚开始设置为true,在case 4中设置为false。
/*
一个用来描述记账内容的类
*/
public class Count {
private int id;
private String type;
private String account;
private String style;
private double price;
//private Time time;
private String date;
private String other;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getOther() {
return other;
}
public void setOther(String other) {
this.other = other;
}
public Count(int id, String type, String account, String style, double price, String date, String other) {
this.id = id;
this.type = type;
this.account = account;
this.style = style;
this.price = price;
this.date = date;
this.other = other;
}
public Count() { //无参构造方法,添加业务是可以使用到
}
}
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 app {
//存储账务的集合list
static List<Count> list=new ArrayList<>();
public static void main(String [] args){
init();
Scanner num=new Scanner(System.in);
boolean flag=true; //用于退出while循环
//主页显示
while(flag) {
show();
//用户需要输入
int n=num.nextInt();
//对不同编号继续不同的操作
switch(n){
case 1:
//添加业务
addCount();
break;
case 2:
delCount();
//删除业务,根据id进行删除
break;
case 3:
//查询业务:又分为 1 全部查询 2 按id查询 3 按时间查询 4 按类别查询
System.out.println("-----进入查询业务页面------");
System.out.println("1. 全部查询 2.按id查询 3.按时间范围查询 4.按类别查询");
System.out.println("请输入编号::");
Scanner ss=new Scanner(System.in);
int nn=ss.nextInt();
switch (nn){
case 1:
selectAll();
break;
//全部查询
case 2:
//按id查询
selectId();
break;
case 3:
selectDate();
//按时间查询
break;
case 4:
selectStyle();
//按类别查询
break;
}
case 4:
//退出系统
flag=false;
break;
default:
System.out.println("请重新输入");
}
}
}
/*
显示主页面
*/
public static void show(){
System.out.println("-------记账本-------");
System.out.println("1.添加业务 "+"2.删除业务 "+"3.查询业务 "+"4.退出系统");
System.out.println("请输入编号:::");
}
/*
对账务数据进行初始化
*/
public static void init(){
Count c1=new Count(1,"吃饭","中行","支出",231.00,"2020-1-1","家庭聚餐");
list.add(c1);
Count c2=new Count(2,"红包","妈妈","收入",1000.00,"2020-2-1","过年");
list.add(c2);
Count c3=new Count(3,"买礼物","中行","支出",200.00,"2020-2-4","情人节");
list.add(c3);
}
/*
添加业务
*/
public static void addCount(){
System.out.println("-----进入添加业务页面-----");
Count cc=new Count();
Scanner s=new Scanner(System.in);
System.out.println("请输入ID");
int id=s.nextInt();
cc.setId(id);
System.out.println("请输入类型");
String type=s.next();
cc.setType(type);
System.out.println("请输入银行");
String account=s.next();
cc.setAccount(account);
System.out.println("请输入类别");
String style=s.next();
cc.setStyle(style);
System.out.println("请输入金额");
Double price=(double)s.nextFloat();
cc.setPrice(price);
System.out.println("请输入时间");
String date=s.next();
cc.setDate(date);
System.out.println("请输入备注");
String other=s.next();
cc.setOther(other);
System.out.println("添加成功!");
}
/*
删除业务:根据id进行删除
*/
public static void delCount(){
System.out.println("-----进入删除业务页面------");
System.out.println("请输入要删除业务的编号::");
Scanner s=new Scanner(System.in);
int i=s.nextInt();
try{
list.remove(i);
System.out.println("删除成功啦!!");}
catch (Exception e){
System.out.println("该编号不存在");
}
}
/*
查询业务:全部查询
*/
public static void selectAll(){
for (int i = 0; i <list.size() ; i++) {
Count c=list.get(i);
System.out.println(c.getId()+" "+c.getType()+" "+
c.getAccount()+" "+c.getStyle()+" "+c.getPrice()
+" "+c.getDate()+" "+c.getOther());
}
}
/*
查询业务:按id查询
*/
public static void selectId(){
System.out.println("请输入要查询账户的编号::");
Scanner ss=new Scanner(System.in);
int nn=ss.nextInt();
try {
System.out.println(list.get(nn-1).getId() + " " + list.get(nn-1).getType() + " " +
list.get(nn-1).getAccount() + " " + list.get(nn-1).getStyle() + " " + list.get(nn-1).getPrice()
+ " " + list.get(nn-1).getDate() + " " + list.get(nn-1).getOther());
}
catch(Exception e)
{
System.out.println("该编号不存在");
}
}
/*
查询业务:按时间范围进行查询
*/
public static void selectDate(){
//创建一个时间格式化的对象
SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd");
System.out.println("请输入开始时间");
Scanner ss=new Scanner(System.in);
String start=ss.next();
System.out.println("请输入结束时间");
Scanner ss1=new Scanner(System.in);
String end=ss1.next();
//使用stream流
List<Count> lll=list.stream().filter(
count -> {
String date=count.getDate();
//将字符串格式的时间进行解析
try {
Date startDate= time.parse(start);
Date endDate= time.parse(end);
Date dateDate= time.parse(date);
if(dateDate.after(startDate)&&dateDate.before(endDate)) //若当前时间在开始时间之后,在结束时间之前,return true
{
return true;
}
} catch (ParseException e) {
System.out.println("该时间段内无记账记录");
}
return false;
}
).collect(Collectors.toList());
for (int i = 0; i <lll.size() ; i++) {
System.out.println(lll.get(i).getId() + " " + lll.get(i).getType() + " " +
lll.get(i).getAccount() + " " + lll.get(i).getStyle() + " " + lll.get(i).getPrice()
+ " " + lll.get(i).getDate() + " " + lll.get(i).getOther());
}
}
/*
查询业务:按类别style查询
*/
public static void selectStyle(){
System.out.println("请输入要查询业务的类别: 支出或者收入");
Scanner ss=new Scanner(System.in);
String s=ss.next();
//使用集合的stream流 过滤 断言
List<Count> ll=list.stream().filter(
count ->{
String type1=count.getStyle();
return type1.equals(s);
}
).collect(Collectors.toList());
for (int i = 0; i <ll.size() ; i++) {
System.out.println(ll.get(i).getId() + " " + ll.get(i).getType() + " " +
ll.get(i).getAccount() + " " + ll.get(i).getStyle() + " " + ll.get(i).getPrice()
+ " " + ll.get(i).getDate() + " " + ll.get(i).getOther());
}
}
}