package 项目.零钱通;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChang01 {
//化繁为简(显示菜单,选择菜单,提示信息)
public static void main(String[] args) {
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
String detials = "---------零钱通明细---------";
double money = 0;
double banlance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//日期格式化
String note = "";
do {//用do-while循环是因为要至少显示一次
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.print("请选择(1-4):");
key = scanner.next();
//用switch分支结构
switch (key){
case "1":
System.out.println(detials);
break;
case "2":
System.out.print("收益入账金额:");
money = scanner.nextDouble();
banlance += money;
//金额的合理性判断(找出不正确的条件)
if (money<=0){
System.out.println("您输入的金额有问题");
break;
}
//拼接收益入账信息
date = new Date();//获取当前日期
detials += "\n收益入账\t+"+money+"\t"+sdf.format(date)+"\t"+banlance;
break;
case "3":
System.out.print("请输入您的消费金额:");
money = scanner.nextDouble();
if (money <= 0 || money>banlance){//不能小于零也不能比入账的金额多
System.out.println("您的消费金额应该在0- "+banlance);
}
System.out.print("消费说明:");
note = scanner.next();
banlance -= money;
//拼接消费信息
date = new Date();//获取当前日期
detials += "\n"+note+"\t-"+money+"\t"+sdf.format(date)+"\t"+banlance;
break;
case "4":
String choice = "";
while(true){
System.out.println("您确定要退出? y/n");
choice = scanner.next();
if("y".equals(choice) || "n".equals(choice)){//输入结果是y或n循环才结束
break;
}
}
if ("y".equals(choice)){
loop = false;
}else if ("n".equals(choice)){
loop = true;
}
break;
default:
System.out.println("选择有问题");
}
}while (loop);
System.out.println("-------退出了零钱通项目---------");
}
}
改用面向对象写法(OOP):
package 项目.零钱通;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
//使用面向对象的知识完成零钱通项目
public class SmallChang02 {
public static void main(String[] args) {
// SmallChangOOP oop = new SmallChangOOP();
// oop.mainMenu();
new SmallChangOOP().mainMenu();//匿名对象
}
}
class SmallChangOOP{
//属性
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
String detials = "---------零钱通明细---------";
double money = 0;
double banlance = 0;
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//日期格式化
String note = "";
//将零钱通的各个功能封装成一个方法
public void mainMenu(){
do {//用do-while循环是因为要至少显示一次
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.print("请选择(1-4):");
key = scanner.next();
//用switch分支结构
switch (key){
case "1":
this.detial();
break;
case "2":
this.income();
break;
case "3":
this.pay();
break;
case "4":
this.exit();
break;
default:
System.out.println("选择有问题");
}
}while (loop);
System.out.println("您退出了可爱的零钱通");
}
//详细信息
public void detial(){
System.out.println(detials);
}
//入账
public void income(){
System.out.print("收益入账金额:");
money = scanner.nextDouble();
banlance += money;
//金额的合理性判断(找出不正确的条件)
if (money<=0){
System.out.println("您输入的金额有问题");
return;//退出执行该方法
}
//拼接收益入账信息
date = new Date();//获取当前日期
detials += "\n收益入账\t+"+money+"\t"+sdf.format(date)+"\t"+banlance;
}
//消费
public void pay(){
System.out.print("请输入您的消费金额:");
money = scanner.nextDouble();
if (money <= 0 || money>banlance){//不能小于零也不能比入账的金额多
System.out.println("您的消费金额应该在0- "+banlance);
return;
}
System.out.print("消费说明:");
note = scanner.next();
banlance -= money;
//拼接消费信息
date = new Date();//获取当前日期
detials += "\n"+note+"\t-"+money+"\t"+sdf.format(date)+"\t"+banlance;
}
//退出
public void exit(){
String choice = "";
while(true){
System.out.println("您确定要退出? y/n");
choice = scanner.next();
if("y".equals(choice) || "n".equals(choice)){//输入结果是y或n循环才结束
break;
}
}
if ("y".equals(choice)){
loop = false;
}else if ("n".equals(choice)){
loop = true;
}
}
}