一、面向过程版本
package com.hspedu.smallChange;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSys {
public static void main(String[] args) {
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key="";
String details = "------------------------零钱通明细---------------------";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String note;
String choice;
do {
System.out.println("========================零钱通菜单=====================");
System.out.println("\t\t\t1 零钱通明细");
System.out.println("\t\t\t2 收益入账");
System.out.println("\t\t\t3 消 费");
System.out.println("\t\t\t4 退 出");
System.out.println("请输入1-4选择功能:");
key = scanner.next();
switch (key){
case "1":
System.out.println(details);
break;
case "2":
System.out.print("请输入入账金额:");
money = scanner.nextDouble();
if(money <= 0){
System.out.println("消费金额需要大于等于0元!");
continue;
}
balance += money;
date = new Date();
details += "\n收益入账:\t+" +money + "\t" +sdf.format(date) + "\t余额:\t"+balance;
break;
case "3":
System.out.print("消费金额:");
money = scanner.nextDouble();
if(money <=0 || money >balance){
System.out.println("消费金额应该在0-"+balance);
continue;
}
System.out.print("消费说明:");
note = scanner.next();
balance -=money;
date = new Date();
details +="\n"+ note + "\t\t-"+money +"\t"+sdf.format(date) + "\t余额:\t"+balance;
break;
case "4":
while (true) {
System.out.print("你确定要退出吗?y/n");
choice = scanner.next();
if("y".equals(choice) || "n".equals(choice)) {
break;
}
}
if(choice.equals("y")){
loop = false;
}
break;
default:
System.out.println("请检查输入!你的输入有误!");
}
}while (loop);
}
}
二、面向对象版本
2.1SmallChangeSysOOP类
package com.hspedu.smallChange;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSysOOP {
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key="";
String details = "------------------------零钱通明细---------------------";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String note;
String choice;
public void mainMenu() {
do {
System.out.println("========================零钱通菜单=====================");
System.out.println("\t\t\t1 零钱通明细");
System.out.println("\t\t\t2 收益入账");
System.out.println("\t\t\t3 消 费");
System.out.println("\t\t\t4 退 出");
System.out.println("请输入1-4选择功能:");
key = scanner.next();
switch (key){
case "1":
this.detail();
break;
case "2":
this.income();
break;
case "3":
this.pay();
break;
case "4":
this.exit();
break;
default:
System.out.println("请检查输入!你的输入有误!");
}
}while (loop);
}
public void detail() {
System.out.println(details);
}
public void income() {
System.out.print("请输入入账金额:");
money = scanner.nextDouble();
if(money <= 0){
System.out.println("消费金额需要大于等于0元!");
}else {
balance += money;
date = new Date();
details += "\n收益入账:\t+" +money + "\t" +sdf.format(date) + "\t余额:\t"+balance;
}
}
public void pay() {
System.out.print("消费金额:");
money = scanner.nextDouble();
if(money <=0 || money >balance){
System.out.println("消费金额应该在0-"+balance);
}else {
System.out.print("消费说明:");
note = scanner.next();
balance -=money;
date = new Date();
details +="\n"+ note + "\t\t-"+money +"\t"+sdf.format(date) + "\t余额:\t"+balance;
}
}
public void exit() {
while (true) {
System.out.print("你确定要退出吗?y/n");
choice = scanner.next();
if("y".equals(choice) || "n".equals(choice)) {
break;
}
}
if(choice.equals("y")){
loop = false;
}
}
}
2.2 SmallChangeSysAPP类
package com.hspedu.smallChange;
public class SmallChangeSysAPP {
public static void main(String[] args) {
SmallChangeSysOOP smallChangeSysOOP = new SmallChangeSysOOP();
smallChangeSysOOP.mainMenu();
}
}