P335_0334韩顺平Java_零钱通介绍

P335_0334韩顺平Java_零钱通介绍

  1. 先完成显示菜单,并可以选择。
  2. 完成零钱通明细。
  3. 完成收益入账。
  4. 消费。
  5. 退出。

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();
    }
}

参考资料

[1] 【零基础 快速学Java】韩顺平 零基础30天学会Java
[2]
[3]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值