模板类:
import java.text.SimpleDateFormat;
import java.util.Date; //(获取时间的包)
import java.util.Scanner; //(输入的包)
public class SmallChange {
//成员变量
int choice; //选项项
boolean loop = true; //循环条件
double money = 0; //金钱
double balance = 0; //余额
String thing; //消费说明
String detail = "----------零钱通明细-----------";
Date time = null; //获取时间
String sure; //是否退出
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //时间格式化
Scanner key = new Scanner(System.in); //声明Scanner
//成员方法
public void Menu() {
do{
System.out.println("===========零钱通(OOP版)============");
System.out.println("\t\t1 零钱通明细");
System.out.println("\t\t2 收益入账");
System.out.println("\t\t3 消费");
System.out.println("\t\t4 退出");
System.out.print("请选择(1-4):");
choice = key.nextInt();
switch(choice){
case 1: this.detail();break;
case 2: this.income();break;
case 3: this.cost();break;
case 4: this.exit();break;
default :
System.out.println("输入有误!");
}
}
while(loop);
}
public void detail(){
System.out.println(detail);
}
public void income(){
time = new Date();
System.out.print("收益入账金额:");
money =key.nextDouble();
if(money<=0)
{
System.out.println("收益出现了错误!");
}
else {
balance = balance + money;
detail = detail + "\n收益入账\t+" + money + "\t" + dateformat.format(time) + "\t" + "余额:" + balance;
//温馨提示:这里运用了字符串拼接
}
}
public void cost(){
time = new Date();
System.out.print("消费金额:");
money = key.nextDouble();
if(balance<money)
{
System.out.println("余额不足!");
}
else {
System.out.print("消费说明:");
thing = key.next();
balance -= money;
detail += "\n" + thing + "\t-" + money + "\t" + dateformat.format(time) + "\t" + "余额:" + balance;
}
}
public void exit(){
while(true){
System.out.println("你确认是否要退出?y/n");
sure = key.next();
if("y".equals(sure) || "n".equals(sure)) {
break;
}
}
if(sure.equals("y")){
loop = false;
System.out.println("退出了零钱通...");
}
}
}
测试类:
package com.baidu.www.SmallChange;
public class SmallChangeTest {
public static void main(String[] args) {
new SmallChange().Menu();
}
}
效果图: