目录
- P335_0334韩顺平Java_零钱通介绍
- 代码
- 过程编程
- OOP(Object-Oriented Project)
 
- 参考资料
P335_0334韩顺平Java_零钱通介绍
- 先完成显示菜单,并可以选择。
- 完成零钱通明细。
- 完成收益入账。
- 消费。
- 退出。
PS:判断时尽量用不正确的条件去过滤,用正确条件过滤太繁琐

代码
过程编程
package smallchange;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChange {
    public static void main(String[] args) {
        boolean loop = true;
        Scanner scanner = new Scanner(System.in);
        String key = ""; // enter string
        String details = "----------- Details -----------";
        String exit = new String();
        double money = 0;
        double balance = 0;
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm");
        String note = "";
        do {
            System.out.println("\n\n\n=========== Menu ===========");
            System.out.println("\t\t# 1 Details.");
            System.out.println("\t\t# 2 Income.");
            System.out.println("\t\t# 3 Consume.");
            System.out.println("\t\t# 4 Exit.");
            System.out.print("# Please enter your selection(1~4):");
            key = scanner.next();
            switch (key) {
                case "1":
                    System.out.println(details);
                    break;
                case "2":
                    while (true) {
                        System.out.print("# The account of income is:");
                        money = scanner.nextDouble();
                        if (money <= 0) {
                            System.out.println("# Error, The account of income need greater than 0!");
                        } else {
                            break;
                        }
                    }
                    balance += money;
                    date = new Date();
                    details += "\nIncome: +" + money + "\tTime:" + sdf.format(date) + "\tBalance:" + balance;
                    break;
                case "3":
                    while (true) {
                        System.out.print("# The account of consume is:");
                        money = scanner.nextDouble();
                        if (money <= 0 || money > balance) {
                            System.out.println("# Error, The account of consume need greater than 0 and less than " + balance);
                        } else {
                            break;
                        }
                    }
                    balance -= money;
                    System.out.print("# The description of consume is:");
                    note = scanner.next();
                    details += "\nConsume: -" + money + "\tTime:" + sdf.format(date) + "\tBalance:" + balance + "\tNote:" + note;
                    break;
                case "4":
//                    enter in
                    while (true) {
                        System.out.println("# Are sure you want exit?(y\\n)");
                        exit = scanner.next();
                        if (exit.equals("y") || exit.equals("n")) {
                            break;
                        } else {
                            System.out.println("# Error, please enter again!");
                        }
                    }
//                    exit or not
                    if (exit.equals("y")) {
                        loop = false;
                        System.out.println("=========== Exit ===========");
                    } else if (exit.equals("n")) {
                        continue;
                    }
                    break;
                default:
                    System.out.println("# Error, please enter your selection again!");
            }
        } while (loop);
    }
}
OOP(Object-Oriented Project)
package smallchange;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeOOP {
    private boolean loop = true;
    private final Scanner scanner = new Scanner(System.in);
    private String key = ""; // enter string to select function.
    private String details = "----------- Details -----------";
    private String exit = "";// enter y\n to exit
    private double money = 0;
    private double balance = 0;
    private Date date = null;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm");
    private String note = "";
    public void mainMenu() {
        do {
            System.out.println("\n=========== Menu ===========");
            System.out.println("\t\t# 1 Details.");
            System.out.println("\t\t# 2 Income.");
            System.out.println("\t\t# 3 Consume.");
            System.out.println("\t\t# 4 Exit.");
            System.out.print("# Please enter your selection(1~4):");
            key = scanner.next();
            switch (key) {
                case "1":
                    this.details();
                    break;
                case "2":
                    this.income();
                    break;
                case "3":
                    this.consume();
                    break;
                case "4":
                    this.exit();
                    break;
                default:
                    System.out.println("# Error, please enter your selection again!");
            }
        } while (loop);
    }
    public void details() {
        System.out.println(details);
    }
    public void income() {
        while (true) {
            System.out.print("# The account of income is:");
            money = scanner.nextDouble();
            if (money <= 0) {
                System.out.println("# Error, The account of income need greater than 0!");
            } else {
                break;
            }
        }
        balance += money;
        date = new Date();
        details += "\nIncome: +" + money + "\tTime:" + sdf.format(date) + "\tBalance:" + balance;
    }
    public void consume() {
        while (true) {
            System.out.print("# The account of consume is:");
            money = scanner.nextDouble();
            if (money <= 0 || money > balance) {
                System.out.println("# Error, The account of consume need greater than 0 and less than " + balance);
            } else {
                break;
            }
        }
        balance -= money;
        System.out.print("# The description of consume is:");
        note = scanner.next();
        details += "\nConsume: -" + money + "\tTime:" + sdf.format(date) + "\tBalance:" + balance + "\tNote:" + note;
    }
    public void exit() {
        //                    enter in
        while (true) {
            System.out.println("# Are sure you want exit?(y\\n)");
            exit = scanner.next();
            if (exit.equals("y") || exit.equals("n")) {
                break;
            } else {
                System.out.println("# Error, please enter again!");
            }
        }
//                    exit or not
        if (exit.equals("y")) {
            loop = false;
            System.out.println("=========== Exit ===========");
        }
    }
}
package smallchange;
public class Test {
    public static void main(String[] args) {
        SmallChangeOOP smallChangeOOP = new SmallChangeOOP();
        smallChangeOOP.mainMenu();
    }
}



















