当前位置: 首页 > article >正文

P335_0334韩顺平Java_零钱通介绍

目录

  • P335_0334韩顺平Java_零钱通介绍
  • 代码
    • 过程编程
    • OOP(Object-Oriented Project)
  • 参考资料

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]


http://www.kler.cn/a/321171.html

相关文章:

  • 单片机智能家居火灾环境安全检测
  • A030-基于Spring boot的公司资产网站设计与实现
  • 跟我学C++中级篇——Design Patterns的通俗说法
  • JDBC-Mysql 时区问题详解
  • 本地 / 网络多绑定用例总结
  • Queuing 表(buffer表)的优化实践 | OceanBase 性能优化实践
  • 华为NAT ALG技术的实现
  • AttributeError: ‘Sequential‘ object has no attribute ‘predict_classes‘如何解决
  • 【Python报错已解决】ModuleNotFoundError: No module named ‘psutil’
  • Android——运行时动态申请权限
  • [Redis][Hash]详细讲解
  • 828华为云征文 | 在华为云X实例上部署微服务架构的文物大数据管理平台的实践
  • linux命令:显示已安装在linux内核的模块的详细信息的工具modinfo详解
  • 物理学基础精解【7】
  • Docker 容器技术:颠覆传统,重塑软件世界的新势力
  • 【RAG研究1】导言-我打算如何对RAG进行全面且深入的研究
  • 【后端开发】JavaEE初阶——计算机是如何工作的???
  • 职业技能大赛-单元测试笔记(assertThat)分享
  • [SDX35]SDX35如何查看GPIO的Base值
  • 力扣随机一题——所有元音按顺序排序的最长字符串
  • Linux嵌入式驱动开发指南(速记版)---Linux基础篇
  • 【计算机组成原理】实验一:运算器输入锁存器数据写实验
  • Windows系统IP地址设置
  • 小白业主选瓷砖不知道质量好坏怎么办?三种方法交给你
  • 【PyTorch】张量操作与线性回归
  • 【学习笔记】网络设备(华为交换机)基础知识7——查看硬件信息 ① display device 命令详解