内容纲要
328讲 断点调试(debug)、断点调试案例、零钱通案例。
零钱通
- 面向过程
package main.smallChange;
//面向过程编程
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSys {
public static void main(String[] args) {
//打印零钱通菜单
//循环打印菜单表,直到选择退出
Scanner myScanner = new Scanner(System.in);
boolean loop = true;
double balance = 0;
double income = 0;
double consume = 0;
Date date = new Date();//获取时间
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:MM");
String detail = "-------零钱通明细-----";
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:");
int num = myScanner.nextInt();
switch (num) {
case 1:
System.out.println(detail);
System.out.println();
break;
case 2:
System.out.println("---收益入账---");
System.out.println("输入收益入账金额:");
income = myScanner.nextDouble();
if(income <= 0){
System.out.println("收益金额要>0");
break;
}
balance += income; //计算余额。
detail += "\n收益入账\t" + income + " \t\t" + sdf.format(date) +
"\t余额\t" + balance;
break;
case 3:
System.out.println("----消费----");
System.out.println("请输入消费金额");
consume = myScanner.nextDouble();
if(consume <= 0 || consume > balance){
System.out.println("消费金额要大于0且小于余额"+ balance);
break;
}
System.out.println("请输入消费类型");
balance -= consume;//减去消费的金额;
detail += "\n" + myScanner.next() + "\t-" +
consume + "\t" + sdf.format(date) + "\t余额\t" + balance;
break;
case 4:
String str = "";
while (true) {
System.out.println("确定要退出吗?y/n");
str = myScanner.next();
if ("y".equals(str) || "n".equals(str)) {
break;
}
}
if (str.equals("y")) {
loop = false;
}
break;
default:
System.out.println("你输入的有误,请输入1-4");
}
} while (loop);
//打印零钱通内部的表
}
}
- 面向对象 OPP
SmallChangeSysOOP各个功能的类 SmallChangeStsApp调用类
package main.smallChangeSysOOP;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSysOOP {
//打印零钱通菜单
//循环打印菜单表,直到选择退出
Scanner myScanner = new Scanner(System.in);
boolean loop = true;
double balance = 0;
double income = 0;
double consume = 0;
Date date = new Date();//获取时间
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:MM");
String detail = "-------零钱通明细-----";
public void getManue(){
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:");
int num = myScanner.nextInt();
switch (num){
case 1:
getDetail();
break;
case 2:
getIncome();
break;
case 3:
getConsume();
break;
case 4:
exit();
break;
default:
System.out.println("输入有误,请输入1-4");
}
}while(loop);
}
public void getDetail(){
System.out.println(detail);
System.out.println();
return;
}
public void getIncome(){
System.out.println("---收益入账---");
System.out.println("输入收益入账金额:");
income = myScanner.nextDouble();
if(income <= 0){
System.out.println("收益金额要>0");
return;
}
balance += income; //计算余额。
detail += "\n收益入账\t" + income + " \t\t" + sdf.format(date) +
"\t余额\t" + balance;
return;
}
public void getConsume(){
System.out.println("----消费----");
System.out.println("请输入消费金额");
consume = myScanner.nextDouble();
if(consume <= 0 || consume > balance){
System.out.println("消费金额要大于0且小于余额"+ balance);
return;
}
System.out.println("请输入消费类型");
balance -= consume;//减去消费的金额;
detail += "\n" + myScanner.next() + "\t-" +
consume + "\t" + sdf.format(date) + "\t余额\t" + balance;
return;
}
public void exit(){
String str = "";
while (true) {
System.out.println("确定要退出吗?y/n");
str = myScanner.next();
if ("y".equals(str) || "n".equals(str)) {
break;
}
}
if (str.equals("y")) {
loop = false;
}
return;
}
}
package main.smallChangeSysOOP;
public class SmallChangeSysAPP {
public static void main(String[] args) {
SmallChangeSysOOP smallChangeSysOOP = new SmallChangeSysOOP();
smallChangeSysOOP.getManue();
}
}