零钱通小练习
面对过程!!!!
package com.zhou.java.Demo03;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Text {
public static void main(String[] args) {
boolean loop = true;
String choose = "";
double money;
double balance = 0.0;
Date date = new Date();
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Scanner sc = new Scanner(System.in);
String result = "----------------零钱通明细----------------";
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.print("请选择(1-4):");
String key = sc.next();
switch (key) {
case "1":
System.out.println(result);
break;
case "2":
System.out.print("请输入收益入账:");
money = sc.nextDouble();
if (money > 0) {
balance += money;
result += "\n收益入账\t+" + money + "\t" + sim.format(date) + "\t" + balance;
} else {
System.out.println("你输入的金额不正确");
}
break;
case "3":
System.out.println("消费金额");
money = sc.nextDouble();
String dress = "";
System.out.println("消费地址");
dress = sc.next();
if (money <= balance||money>=0) {
balance -= money;
result += "\n" + dress + "\t-" + money + "\t" + sim.format(date) + "\t" + balance;
} else {
System.out.println("你的余额不足");
}
break;
case "4":
while (true) {
System.out.println("你确定要退出嘛 \n\t Y 确认,N 继续操作");
choose = sc.next();
if ("Y".equals(choose) || "N".equals(choose)) {
break;
}
}
if ("Y".equals(choose)) {
loop = false;
}
System.out.println("谢谢使用零钱通");
break;
default:
System.out.println("菜单选择有误,请重新选择");
}
} while (loop);
}
}
面向对象(OOP)
ChangeOOP
写方法
package com.zhou.java.Demo03.Dome01;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class ChangeOOP {
boolean loop = true;
String choose = "";
double money;
double balance = 0.0;
Date date = new Date();
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Scanner sc = new Scanner(System.in);
String result = "----------------零钱通明细----------------";
public void result(){
System.out.println(result);
}
public void income(){
System.out.print("请输入收益入账:");
money = sc.nextDouble();
if (money > 0) {
balance += money;
result += "\n收益入账\t+" + money + "\t" + sim.format(date) + "\t" + balance;
} else {
System.out.println("你输入的金额不正确");
}
}
public void pay(){
System.out.println("消费金额");
money = sc.nextDouble();
String dress = "";
System.out.println("消费地址");
dress = sc.next();
if (money <= balance||money>=0) {
balance -= money;
result += "\n" + dress + "\t-" + money + "\t" + sim.format(date) + "\t" + balance;
} else {
System.out.println("你的余额不足");
}
}
public void exit(){
while (true) {
System.out.println("你确定要退出嘛 \n\t Y 确认,N 继续操作");
choose = sc.next();
if ("Y".equals(choose) || "N".equals(choose)) {
break;
}
}
if ("Y".equals(choose)) {
loop = false;
System.out.println("谢谢使用零钱通");
}
}
public void minMenu(){
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.print("请选择(1-4):");
String key = sc.next();
switch (key) {
case "1":
result();
break;
case "2":
income();
break;
case "3":
pay();
break;
case "4":
exit();
break;
default:
System.out.println("菜单选择有误,请重新选择");
}
} while (loop);
}
}
Test
调用ChangeOOP中的方法
package com.zhou.java.Demo03.Dome01;
public class Test {
public static void main(String[] args) {
ChangeOOP oop = new ChangeOOP();
oop.minMenu();
}
}
标签:通小,java,sc,money,System,零钱,println,out From: https://www.cnblogs.com/wase/p/17135370.html