项目要求:使用 Java 开发 零钱通项目 , 可以完成收益入账,消费,查看明细,退出系统等功能。
由于不管选择什么,都需要至少执行一次,所以考虑do...while循环结构。
boolean loop = true; 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 退\t出"); System.out.print("请选择1-4:"); }while(loop); System.out.println("=======退出零钱通=======");
使用switch选择结构来实现选择1-4的结果
int n = scanner1.nextInt(); //switch结构 switch (n) { case 1: System.out.println("1 零钱明细"); break; case 2: System.out.print("2 收益入账"); break; case 3: System.out.print("3 消费"); break; case 4: System.out.println("4 退\t出"); loop = false; break; }
这里使用字符串拼接,定义detail变量
String detail = "--------------零钱明细--------------"; //在上面case 1的情况下直接输出detail case 1: System.out.println(detail); System.out.println(); break;
这里需要定义两个变量,一个是表示收益的money变量,另一个表示余额balance变量。
另外时间信息需要调用包 import java.util.Date;
double money = 0.0; double balance = 0.0; Date date = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //修改上面case 2的情况,得到detail case 2: System.out.print("收益入账金额:"); money = scanner1.nextDouble(); //money需要校验 if (money <= 0) { System.out.println("收益入账金额要大于0"); break; } balance += money; date = new Date(); //System.out.println(sdf.format(date)); detail += "\n收益入账\t" + money + "\t" + sdf.format(date) + "\t" + balance; break;
与收益入账类似,
//修改上面case 3的情况,得到detail case 3: System.out.print("消费金额:"); money = scanner1.nextDouble(); System.out.print("消费说明:"); note = scanner1.next(); //需要校验消费是否超出余额 if (money > balance) { System.out.println("消费超出余额"); break; } balance -= money; date = new Date(); detail += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance; break;
如果输入结果不是y或n,将一直询问,所以需要用一个循环来实现,并且在循环中判断输入字符。
//在case 4 中进行修改 case 4: System.out.println("4 退\t出"); String choice = ""; while (true) { System.out.println("你确定要退出吗?y/n"); choice = scanner1.next(); if ("y".equals(choice) || "n".equals(choice)) { break; } } if ("y".equals(choice)) { loop = false; } break;
此外如果输入的数字不在1-4范围内,需要提示错误信息。故在switch中添加default情况。
default: System.out.println("选择有误,请重新选择");
将上述菜单界面,4个功能都拆解成类的方法,相应的变量都定义为类的属性。最后调用菜单方法即可。把各个方法都封装起来,极大地提高了代码的复用性,便于维护。
package com.zixue.SmallChange.OOP; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; // 面向对象编程 public class SmallChangeSysOOP { //1.显示菜单 boolean loop = true; Scanner scanner1 = new Scanner(System.in); //2.零钱通明细 String detail = "--------------零钱明细--------------"; //3.收益入账 double money = 0; double balance = 0.0; Date date = null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); //4. 消费 String note = null; // 完成显示菜单 public void mainMenu() { do { System.out.println("==========请选择零钱通(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 退\t出"); System.out.print("请选择1-4:"); int n = scanner1.nextInt(); //switch结构 switch (n) { case 1: this.detail(); break; case 2: this.income(); break; case 3: this.consume(); break; case 4: this.exit(); break; default: System.out.println("选择有误,请重新选择"); } } while (loop); } // 完成零钱通明细 public void detail() { System.out.println(detail); System.out.println(); } // 完成收益入账 public void income(){ System.out.print("收益入账金额:"); money = scanner1.nextDouble(); //money需要校验 if (money <= 0) { System.out.println("收益入账金额要大于0"); return; } balance += money; date = new Date(); //System.out.println(sdf.format(date)); detail += "\n收益入账\t" + money + "\t" + sdf.format(date) + "\t" + balance; } // 完成消费 public void consume(){ System.out.print("消费金额:"); money = scanner1.nextDouble(); System.out.print("消费说明:"); note = scanner1.next(); if (money > balance) { System.out.println("消费超出余额"); return; } balance -= money; date = new Date(); detail += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance; } // 完成退出 public void exit(){ System.out.println("4 退\t出"); String choice = ""; while (true) { System.out.println("你确定要退出吗?y/n"); choice = scanner1.next(); if ("y".equals(choice) || "n".equals(choice)) { break; } } if ("y".equals(choice)) { loop = false; System.out.println("=======退出零钱通======="); } } }
package com.zixue.SmallChange.OOP; public class SmallChangeSysAPP { public static void main(String[] args) { new SmallChangeSysOOP().mainMenu(); } }