目录
一.项目需求说明
使用java开发 零钱通项目,可以完成收益入账,消费,查看明细,退出系统等功能
二.项目界面
三.步骤(化繁为简)
1.完成菜单的显示
2.完成零钱通明细
3.完成收益入账
4.消费
5.退出
四.具体步骤及其代码
1.菜单的显示
(可以最后再来看菜单的显示)
public void mainMenu() {
System.out.println("显示菜单");
do {
System.out.println("\n=========零钱通菜单=========");
System.out.println("\t\t1.零钱通明细");
System.out.println("\t\t2.收益入账");
System.out.println("\t\t3.消费情况");
System.out.println("\t\t4.退 出");
System.out.println("请选择1-4");
key = scanner.next();
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);
}
2.完成零钱通明细
String details = "=========零钱通明细=========";
public void detail() {
System.out.println(details);
}
3.完成收益入账
//收益入账
public void income() {
System.out.println("收益入账金额");
money = scanner.nextDouble();
if (money <= 0) {//对金额的校验,收益不可能为负数
System.out.println("收益金额应大于0");
return;
}
balance += money;
date = new Date();
details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
//用字符串拼接来输出结果
}
4.消费
//消费
public void pay() {
System.out.println("消费金额");
money = scanner.nextDouble();
if (money <= 0 || money > balance) {//对金额的校验,收益不可能为负数
System.out.println("消费金额 范围 应该在0 - " + balance);
return;
//尽量避免出现else,因为else会将代码分成两部分
//可读性和维护性不高,不到万不得已最好不使用else
}
balance -= money;
System.out.println("消费说明");
note = scanner.next();
date = new Date();
details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
}
5.退出
public void exit() {
String choice = "";
while (true) {
System.out.println("确认退出吗? y/n");
choice = scanner.next();
if ("y".equals(choice) || "n".equals(choice)) {
break;
} else {
System.out.println("重新确认");
}
}
if (choice.equals("y")) {
loop = false;
}
}
五.完整代码OOP
package oop_;
public class SmallChangeSysApp {
public static void main(String[] args) {
new SmallChangeSysOOP().mainMenu();
}
}
package oop_;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSysOOP {
//属性
boolean loop = true;//判断是否退出
Scanner scanner = new Scanner(System.in);
String key = "";
String details = "=========零钱通明细=========";
double money = 0;
double balance = 0;//收益
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm");
//上面代码是用来日期格式化的
String note = "";//消费原因
//菜单
public void mainMenu() {
System.out.println("显示菜单");
do {
System.out.println("\n=========零钱通菜单=========");
System.out.println("\t\t1.零钱通明细");
System.out.println("\t\t2.收益入账");
System.out.println("\t\t3.消费情况");
System.out.println("\t\t4.退 出");
System.out.println("请选择1-4");
key = scanner.next();
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);
}
//明细
public void detail() {
System.out.println(details);
}
//收益入账
public void income() {
System.out.println("收益入账金额");
money = scanner.nextDouble();
if (money <= 0) {
System.out.println("收益金额应大于0");
return;
}
balance += money;
date = new Date();
details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
}
//消费
public void pay() {
System.out.println("消费金额");
money = scanner.nextDouble();
if (money <= 0 || money > balance) {
System.out.println("消费金额 范围 应该在0 - " + balance);
return;
}
balance -= money;
System.out.println("消费说明");
note = scanner.next();
date = new Date();
details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
}
//退出
public void exit() {
String choice = "";
while (true) {
System.out.println("确认退出吗? y/n");
choice = scanner.next();
if ("y".equals(choice) || "n".equals(choice)) {
break;
} else {
System.out.println("重新确认");
}
}
if (choice.equals("y")) {
loop = false;
}
}
}
六.注意事项
1.对date的new,必须放在方法里,若放在属性中,日期将不会改变
2.对于零钱通明细的输出,可以使用对象,数组,字符串的拼接等方法,但不建议用数组,因为收益入账和消费的次数不确定,因此不能确定数组长度的大小
3.在pay方法中,只用了一个if语句就校验了消费金额是否合法,也可以用if else语句进行校验,但是if else 语句会把代码分成两段,使代码的可读性和维护性降低,所以建议不到万不得已尽量不使用if else 语句
4.建议一小段代码完成一个功能,尽量不要混在一起
5.退出方法中,要确认是否真的要退出,避免误点情况的出现
七.过程编程实现零钱通
package Smallchange;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSys {
public static void main(String[] args) {
double money = 0.0;
double balance = 0.0;//余额
String note = "";//消费原因
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm" );
//上面代码是用来日期格式化的
//在这里不能new Date,否则日期将会确定,不会改变
boolean loop = true;//判断是否退出零钱通系统
//完成零钱通明细
//老韩建议最好不使用数组,因为不知道入账和消费的次数,不能确定数组的大小
String details = "=========零钱通明细=========";
Scanner scanner = new Scanner(System.in);
String key = "";
do {
System.out.println("\n=========零钱通菜单=========");
System.out.println("\t\t1.零钱通明细");
System.out.println("\t\t2.收益入账");
System.out.println("\t\t3.消费情况");
System.out.println("\t\t4.退 出");
System.out.println("请选择1-4");
key = scanner.next();
switch (key){
case "1":
System.out.println(details);
break;
case "2":
System.out.println("收益入账金额");
money = scanner.nextDouble();
if (money <= 0){//对金额的校验,收益不可能为负数
System.out.println("收益金额应大于0");
break;
}
balance += money;
date = new Date();
details += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + balance;
break;
//用字符串拼接来输出结果
case "3":
System.out.println("消费金额");
money = scanner.nextDouble();
if (money <= 0 || money > balance){
System.out.println("消费金额 范围 应该在0 - " + balance);
break;
}
//尽量避免出现else,因为else会将代码分成两部分
//可读性和维护性不高,不到万不得已最好不使用else
balance -= money;
System.out.println("消费说明");
note = scanner.next();
date = new Date();
details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
break;
case "4":
String choice = "";
while (true){
System.out.println("确认退出吗? y/n");
choice = scanner.next();
if ("y".equals(choice) || "n".equals(choice)){
break;
}else {
System.out.println("重新确认");
}
}
if (choice.equals("y")){
loop = false;
}
break;
default:
System.out.println("重写选择");
}
}while (loop);
System.out.println("==========退出零钱通=========");
}
}