java基础项目–零钱通
-
项目开发流程说明
-
项目需求说明
- 使用java开发零钱通项目,可以完成收益入账,消费,查看明细,退出系统等功能
- 项目的界面
-
项目代码实现改进
-
用户输入4退出时,给出提示“你确定要退出吗?y/n”,必须驶入正确的y/n,否则循环输入指令,知道输入y或者n -
在收益入账和消费时,判断金额是否合理,并给出相应的提示 -
package com.lyn.b.object;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSys {
public static void main(String[] args) {
boolean loop = true;
String key = "";
String node = "";
String details = "========退出零钱通明细========";
double money = 0;
double balance = 0;
Date date = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
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.println("请选择(1~4):");
key = new Scanner(System.in).nextLine();
switch (key) {
case "1":
System.out.println(details);
break;
case "2":
System.out.println("收益入账金额");
money = new Scanner(System.in).nextDouble();
if (money <= 0) {
System.out.println("收益金额范围需要大于0");
break;
}
balance += money;
date = new Date();
details += "\n收益入账\t" + money + "\t" + simpleDateFormat.format(date) + "\t余额" + balance;
break;
case "3":
System.out.println("3.消费金额:");
money = new Scanner(System.in).nextDouble();
if (money < 0 || balance < money) {
System.out.println("消费金额应该在0-" + balance + "之间");
break;
}
System.out.println("消费说明:");
node = new Scanner(System.in).nextLine();
balance -= money;
date = new Date();
details += "\n" + node + "\t-" + money + "\t" + simpleDateFormat.format(date) + "\t余额" + balance;
break;
case "4":
System.out.println("你确定要退出吗?y/n");
String choice = "";
while (true) {
choice = new Scanner(System.in).nextLine();
if ("y".equals(choice) || "n".equals(choice)) {
break;
}
System.out.println("必须输入正确的y/n");
}
if ("y".equals(choice)) {
loop = false;
}
break;
default:
System.out.println("菜单选择有误,请重新选择");
}
} while (loop);
System.out.println("========退出零钱通明细========");
}
}
-
将面向过程的代码修改成面向对象的方法 -
package com.lyn.b.object;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSysOOP {
boolean loop = true;
String key = "";
String node = "";
String details = "========退出零钱通明细========";
double money = 0;
Scanner sc = new Scanner(System.in);
double balance = 0;
Date date = null;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
public void mainMenu() {
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.println("请选择(1~4):");
key = sc.nextLine();
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);
System.out.println("========退出零钱通明细========");
}
public void detail() {
System.out.println(details);
}
public void income() {
System.out.println("收益入账金额");
money = sc.nextDouble();
if (money <= 0) {
System.out.println("收益金额范围需要大于0");
return;
}
balance += money;
date = new Date();
details += "\n收益入账\t" + money + "\t" + simpleDateFormat.format(date) + "\t余额" + balance;
}
public void pay() {
System.out.println("3.消费金额:");
money = sc.nextDouble();
if (money < 0 || balance < money) {
System.out.println("消费金额应该在0-" + balance + "之间");
return;
}
System.out.println("消费说明:");
node = new Scanner(System.in).nextLine();
balance -= money;
date = new Date();
details += "\n" + node + "\t-" + money + "\t" + simpleDateFormat.format(date) + "\t余额" + balance;
}
public void exit() {
System.out.println("你确定要退出吗?y/n");
String choice = "";
while (true) {
choice = sc.nextLine();
if ("y".equals(choice) || "n".equals(choice)) {
break;
}
System.out.println("必须输入正确的y/n");
}
if ("y".equals(choice)) {
loop = false;
}
}
}
package com.lyn.b.object;
public class SmallChangeSysAPP {
public static void main(String[] args) {
new SmallChangeSysOOP().mainMenu();
}
}
|