package com.wyj.four;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
/**
* 零钱通小项目
*/
public class Demo4_Changethrough {
public static void main(String[] args) {
SimpleDateFormat formatter1= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date1 = new Date(System.currentTimeMillis());
boolean flag = true;
Scanner in = new Scanner(System.in);
Change_through change_through = new Change_through();
do {
init();
int options = in.nextInt();
switch (options) {
case 1:
change_through.detail();
break;
case 2:
System.out.println("请输入收入的金额:");
double money = in.nextDouble();
String time = formatter1.format(date1);
change_through.Earnings_booked(money,time);
break;
case 3:
System.out.println("请输入购买的商品:");
String name = in.next();
System.out.println("请输入消费的金额:");
double money1 = in.nextDouble();
String time2 = formatter1.format(date1);
change_through.Consumer_spending(money1,name,time2);
break;
case 4:
flag = false;
System.out.println("欢迎使用零钱通!");
break;
default:
System.out.println("输入有误,请重新输入");
}
} while (flag);
}
public static void init() {
System.out.println("------------零钱通菜单-----------");
System.out.println(" 1.零钱通明细");
System.out.println(" 2.收益入账");
System.out.println(" 3.消 费");
System.out.println(" 4.退 出");
System.out.println("请输入您的输入选项(1-4):");
}
}
/**
* 1.显示菜单并可以选择
* 2.完成零钱通的明细
*/
class Change_through{
private double consumption;//消费
private double earnings;//收益
private double balance;//余额
List<String > list4 = new ArrayList<String>();//将消费和收益记录进去
public Change_through() {
}
//收益入账
public void Earnings_booked(double money,String time){
//在堆内存上改变钱的数目
this.setEarnings(money);
this.balance += this.earnings;
list4.add("收益入账:"+"+"+money +" "+time + " 余额为:"+this.balance);
}
//消费支出
public void Consumer_spending(double money,String name,String time){
if(money > this.balance){
System.out.println("不好意思,您的账户余额不足!");
}else{
this.setConsumption(money);
this.balance -= this.consumption;
list4.add(name+" "+"-"+money +" "+time + " 余额为:"+this.balance);
}
}
//零钱通明细
public void detail(){
System.out.println("------------零钱通明细-------------");
if(list4.isEmpty()){
System.out.println("请您先消费或支出,才能展示您的明细");
}else{
for (String s : list4) {
System.out.println(s);
}
}
}
public double getConsumption() {
return consumption;
}
public void setConsumption(double consumption) {
this.consumption = consumption;
}
public double getEarnings() {
return earnings;
}
public void setEarnings(double earnings) {
this.earnings = earnings;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
演示效果: