方法一:
package com.ketang.LingQianTong;
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 note = " ";
String details = "====================零钱通明细===================";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm");
do {
System.out.println("\n=======================菜单======================");
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.println("收益入账金额:");
money = scanner.nextDouble();
if (money < 0) {
System.out.println("输入金额不正确!!!");
break;
}
balance += money;
date = new Date();
details += "\n收益入账\t" + money + "\t" + sdf.format(date) + "\t" + balance;
break;
case "3" :
System.out.println("消费金额:");
money = scanner.nextDouble();
if(money < 0 || money > balance) {
System.out.println("消费金额应该大于0元且小于余额!!!");
break;
}
//钱数的校验
System.out.println("消费说明:");
note = scanner.next();
balance -= money;
details += "\n" + note + "\t" + "-" + money + "\t" + sdf.format(date) + "\t" + balance;
break;
case "4" :
System.out.println("4 退出 END");
String choice = "";
while(true) {
System.out.println("END? 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);
}
}
方法二(OOP 面向对象编程):
SmallChangeSysApp:
package com.ketang.LingQianTong.OOP;
public class SmallChangeSysApp {
public static void main(String[] args) {
new SmallChangeSysOOP().mainMenu();
}
}
SmallChangeSysOOP:
package com.ketang.LingQianTong.OOP;
//该类是完成零钱通各个功能的类
//使用OOP(面向对象编程)
//将各个功能对应一个方法
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 note = " ";
String details = "====================零钱通明细===================";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm");
//显示菜单
public void mainMenu() {
System.out.println("显示零钱通的菜单。。。");
do {
System.out.println("\n=======================菜单(OOP)======================");
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.println("收益入账金额:");
money = scanner.nextDouble();
if (money < 0) {
System.out.println("输入金额不正确!!!");
return;
}
balance += money;
date = new Date();
details += "\n收益入账\t" + money + "\t" + sdf.format(date) + "\t" + balance;
}
//消费
public void pay() {
System.out.println("消费金额:");
money = scanner.nextDouble();
if(money < 0 || money > balance) {
System.out.println("消费金额应该大于0元且小于余额!!!");
return;
}
//钱数的校验
System.out.println("消费说明:");
note = scanner.next();
balance -= money;
details += "\n" + note + "\t" + "-" + money + "\t" + sdf.format(date) + "\t" + balance;
}
public void exit() {
System.out.println("4 退出 END");
String choice = "";
while(true) {
System.out.println("END? Y/N");
choice = scanner.next();
if("y".equals(choice) || "n".equals(choice)) {
break;
}
}
if(choice.equals("y")) {
loop = false;
}
}
}