package com.hspedu20;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSysOOP {
private double monney;//收入
private double balance;//余额
private double cost;//支出
private String thing;//支出项目
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public double getMonney() {
return monney;
}
public void setMonney(double monney) {
this.monney = monney;
}
public String getThing() {
return thing;
}
public void setThing(String thing) {
this.thing = thing;
}
public String showdate() {//返回日期字符串
Date date = new Date();
SimpleDateFormat date1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return date1.format(date);
}
public String addDetail = "";
public void AddDetail() {//收入-余额
this.balance += getMonney();
this.addDetail += "收益入账\t" + "+" + getMonney() + "\t" + showdate() + "\t" + "余额:" + this.balance + "\n";
}
public String costDetail = "";
public void costDetail() {//支出-余额
if (this.balance < getCost()) {
System.out.println("余额不足");
return;
}
this.balance -= getCost();
this.costDetail += getThing() + " \t" + "-" + getCost() + "\t" + showdate() + "\t" + "余额:" + this.balance + "\n";
}
public void showAll() {
System.out.println(this.addDetail + this.costDetail);
}
public void showMenu() {//菜单
System.out.println("---------------零钱通菜单---------------");
System.out.println("1、零钱通明细");
System.out.println("2、收益入账");
System.out.println("3、消费");
System.out.println("4、退出");
System.out.println("请选择1~4");
}
public void showSelect() {//主要操作
Scanner scanner = new Scanner(System.in);
int select = 0;
boolean chance = true;
while (chance) {
showMenu();
select = scanner.nextInt();
switch (select) {
case 1:
showAll();
break;
case 2:
System.out.println("请输入收入:");
double monney = scanner.nextDouble();
setMonney(monney);
AddDetail();
break;
case 3:
System.out.println("请输入消费项目:");
String thing = scanner.next();
setThing(thing);
System.out.println("请输入消费:");
double cost = scanner.nextDouble();
setCost(cost);
costDetail();
break;
case 4:
chance = false;
break;
default:
System.out.println("输入不规范,请重新输入");
break;
}
}
}
}
package com.hspedu20;
public class SmallChangeSysApp {
public static void main(String[] args) {
new SmallChangeSysOOP().showSelect();
}
}
基本功能可以实现,还有很多细节没完善。