IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> 用Java--零钱通的两种实现 -> 正文阅读

[Java知识库]用Java--零钱通的两种实现


最近刚刚复习了一下Java的面向对象三大特性,这里跟着hsp做个小零钱通实践一下,以下记录了学习和编写过程

1. 需求描述

使用Java 开发零钱通项目, 模仿微信实现简易功能,可以完成收益入账,消费,查看明细,退出系统等功能,先按照一般方法写,后期在改进为OOP
预期界面:(实际可能不同)
image-20211212205025417

2. 需求分析

面对这样一个需求,先化繁为简

  1. 写一个菜单
  2. 完成零钱通明细.
  3. 完成收益入账
  4. 消费
  5. 退出
  6. 用户输入4退出时,给出提示"你确定要退出吗? y/n",必须输入正确的y/n ,否则循环输入指令,直到输入y 或者 n
  7. 在收益入账和消费时,判断金额是否合理,并给出相应的提示

3. 实现零钱通主要功能

3.1 写一个菜单

  • 先完成显示菜单,并可以选择菜单,并且给出对应提示
    public static void main(String[] args) {
        // define related variables
        Scanner scanner = new Scanner(System.in);
        String key = "";
        boolean loop = true;
        do {
            System.out.println("==========Small Change Menu==========");
            System.out.println("\t\t\t1 show change details");
            System.out.println("\t\t\t2 income entry");
            System.out.println("\t\t\t3 consumption");
            System.out.println("\t\t\t4 exit");
            System.out.println("please choose 1-4:");
            key = scanner.next();
            //use switch to control
            switch (key) {
                case "1":
                    System.out.println("1  show change details");
                    break;
                case "2":
                    System.out.println("2 income entry");
                    break;
                case "3":
                    System.out.println("3 consumption");
                    break;
                case "4":
                    System.out.println("4 exit");
                    System.out.println(" you have exit the SmallChange");
                    loop = false;
                    break;
                default:
                    System.out.println("err please choose again");
            }
        } while (loop);
    }

3.2 零钱通明细

  • 思路
    (1) 可以把收益入账和消费保存到数组
    (2) 可以使用对象
    (3) 简单的话可以使用String拼接

这里直接采取第三种方式

改变一下switch的case1

 String details = "-----------------零钱通明细------------------";
   case "1":
                    System.out.println(details);
                    break;

3.3 收益入账

  • 完成收益入账

定义新的变量

 double money = 0;
        double balance = 0;
        Date date = null; // date 是 java.util.Date 类型,表示日期
        //if you don't like the default format of displaying date ,change it with sdf
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

修改switch中的case2

 System.out.print("Income recorded amount:");
                    money = scanner.nextDouble();
                    //the range of money should be limited
                    //give the hits of the illegal money value 就直接break
                    balance += money;
                    //拼接收益入账信息到 details
                    date = new Date(); //Get the current time
                    details += "\n收益入账\t+" + money + "\t" + sdf.format(date)+ "\t" + balance;
                    break;

效果演示:

image-20211212141447783

保证入账>0

image-20211212141645010

3.4 消费

定义新的变量

 String note = "";

修改switch中的case3

  case "3":
                    System.out.print("Consumption amount:");
                    money = scanner.nextDouble();
                    //the range of money should be limited
                    System.out.print("Consumption Description:");
                    note = scanner.next();
                    balance -= money;
                    //Splicing consumption information to details
                    date = new Date();//Get the current time
                    details += "\n"+note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
                    break;

效果演示:

image-20211212143519825

3.5 用户退出改进

给出确认,是否要退出
用户输入4退出时,给出提示"你确定要退出吗? y/n",必须输入正确的y/n ,
否则循环输入指令,直到输入y 或者 n

(1) 定义一个变量 choice, 接收用户的输入
(2) 使用 while + break, 来处理接收到的输入时 y 或者 n
(3) 退出while后,再判断choice是y还是n ,就可以决定是否退出
(4) 建议一段代码完成功能,不混在一起

          case "4":
                    String choice = "";
                    while (true) {
                        //The user is required to enter Y / N, otherwise it will cycle all the time
                        System.out.println("你确定要退出吗? y/n");
                        choice = scanner.next();
                        if ("y".equals(choice) || "n".equals(choice)) {
                            break;
                        }
                        //scheme 2
//                        if("y".equals(choice)) {
//                            loop = false;
//                            break;
//                        } else if ("n".equals(choice)) {
//                            break;
//                        }
                    }
                    if (choice.equals("y")) {
                        loop = false;
                    }
                    break;

效果演示:

image-20211212144851235

3.6 改进金额判断

收入时

 if (money <= 0) {
                        System.out.println("The income entry amount must be greater than 0");
                        break;
                    }

支出时

   if (money <= 0 || money > balance) {
                        System.out.println("Your consumption amount should be 0-" + balance);
                        break;
                    }

效果演示

image-20211212145834312

4. 面向过程版实现

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class SmallChangeSys {
    // try to reduce complexity to simplicity
//1.  First complete the display menu,
// and you can select the menu to give the corresponding prompt
//2.  Complete change details
//3.  Complete income entry
//4.  consumption
//5.  exit
//6.  When the user enters 4 to exit, the prompt "are you sure you want to exit?
// Y / N" will be given. You must enter the correct Y / N,
// otherwise cycle the input instruction until y or n is entered
//7. When the income is recorded and consumed,
// judge whether the amount is reasonable and give corresponding tips
    public static void main(String[] args) {
        // define related variables
        Scanner scanner = new Scanner(System.in);
        String key = "";
        boolean loop = true;
        //2. complete the change details
        //(1) 可以把收益入账和消费,保存到数组 (2) 可以使用对象 (3) 简单的话可以使用String拼接
        String details = "-----------------Change details------------------";
        //3. complete income entry
        double money = 0;
        double balance = 0;
        Date date = null; // date 是 java.util.Date 类型,表示日期
        //if you don't like the default format of displaying date ,change it with sdf
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        //4. consumption
        //define new variable,store the reason why consume
        String note = "";
        do {
            System.out.println("\n==========Small Change Menu==========");
            System.out.println("\t\t\t1 show change details");
            System.out.println("\t\t\t2 income entry");
            System.out.println("\t\t\t3 consumption");
            System.out.println("\t\t\t4 exit");
            System.out.println("please choose 1-4:");
            key = scanner.next();
            //use switch to control
            switch (key) {
                case "1":
                    System.out.println(details);
                    break;
                case "2":
                    System.out.print("Income recorded amount:");
                    money = scanner.nextDouble();
                    //the range of money should be limited
                    //commonly use <if> to judge the wrong situation make the code easy to read
                    //give the hits of the illegal money value 就直接break
                    if (money <= 0) {
                        System.out.println("The income entry amount must be greater than 0");
                        break;
                    }
                    balance += money;
                    //Splicing consumption information to details
                    date = new Date(); //Get the current time
                    details += "\n" + "Income " + "\t" + "+" + money + "\t" + sdf.format(date) + "\t" + balance;
                    break;
                case "3":
                    System.out.print("Consumption amount:");
                    money = scanner.nextDouble();
                    //the range of money should be limited
                    if (money <= 0 || money > balance) {
                        System.out.println("Your consumption amount should be 0-" + balance);
                        break;
                    }
                    System.out.print("Consumption Description:");
                    note = scanner.next();
                    balance -= money;
                    //Splicing consumption information to details
                    date = new Date();//Get the current time
                    details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
                    break;
                case "4":
                    String choice = "";
                    while (true) {
                        //The user is required to enter Y / N, otherwise it will cycle all the time
                        System.out.println("你确定要退出吗? y/n");
                        choice = scanner.next();
                        if ("y".equals(choice) || "n".equals(choice)) {
                            break;
                        }
                        //scheme 2
//                        if("y".equals(choice)) {
//                            loop = false;
//                            break;
//                        } else if ("n".equals(choice)) {
//                            break;
//                        }
                    }
                    if (choice.equals("y")) {
                        loop = false;
                    }
                    break;
                default:
                    System.out.println("err please choose again");

            }
        } while (loop);
        System.out.println(" you have exit the SmallChange");
    }
}

5. 优化成OOP版

很多东西可以直接复制过来变成方法,把原来的改过来是简单的

5.1 实现OOP版

那么先有一个执行的主类SmallChangeSysApp

//Call the object directly and display the main menu
public class SmallChangeSysApp {
    public static void main(String[] args) {
        new SmallChangeSysOOP().mainMenu();
    }
}

还有一个类专门是对象,我们叫它为SmallChangeSysOOP

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * This class is used to complete various functions of zero money pass
 * Using OOP (object-oriented programming)
 * Each function corresponds to a method
 */
public class SmallChangeSysOOP {
    //basic variables
    boolean loop = true;
    Scanner scanner = new Scanner(System.in);
    String key = "";

    //display details
    String details = "-----------------Change details------------------";

    //income
    double money = 0;
    double balance = 0;
    Date date = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    // consume
    String note = "";

    public void mainMenu() {
        do {

            System.out.println("\n================Small Change Menu(OOP)===============");
            System.out.println("\t\t\t1 show change details");
            System.out.println("\t\t\t2 income entry");
            System.out.println("\t\t\t3 consumption");
            System.out.println("\t\t\t4 exit");
            System.out.println("please choose 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("Choose the wrong number please choose again");
            }

        } while (loop);
    }


    public void detail() {
        System.out.println(details);
    }

    public void income() {
        System.out.print("Income recorded amount:");
        money = scanner.nextDouble();

        if (money <= 0) {
            System.out.println("The income entry amount must be greater than 0");
            return; //exit and do not execute next sentence.change break to return
        }
        balance += money;
        date = new Date();
        details += "\nIncome \t+" + money + "\t" + sdf.format(date) + "\t" + balance;
    }

    public void pay() {
        System.out.print("Consumption amount:");
        money = scanner.nextDouble();
        if (money <= 0 || money > balance) {
            System.out.println("Your consumption amount should be 0-" + balance);
            return;
        }
        System.out.print("consumption description:");
        note = scanner.next();
        balance -= money;
        date = new Date();
        details += "\n" + note + "\t-" + money + "\t" + sdf.format(date) + "\t" + balance;
    }

    //退出
    public void exit() {
        //When the user enters 4 to exit, the prompt "are you sure you want to exit?
        // Y / N" will be given. You must enter the correct Y / n
        String choice = "";
        while (true) {
            System.out.println("are you really gonna exit? y/n");
            choice = scanner.next();
            if ("y".equals(choice) || "n".equals(choice)) {
                break;
            }
            //scheme 2
//                        if("y".equals(choice)) {
//                            loop = false;
//                            break;
//                        } else if ("n".equals(choice)) {
//                            break;
//                        }
        }
        if (choice.equals("y")) {
            loop = false;
        }
    }
}

5.3 OOP的好处

OOP版主函数很简单,只要new这个对象就可以了,关于这个对象的其他方法也好属性也好,不用放在主函数里面,那样在主函数也可以自由加上想加得到内容,未来假如有他人要用,不用把整个文件拷过去,只要把类交给对方即可,这样扩展和可读性大大提升,要加什么功能就再写方法原先的扩展功能很麻烦,要来回切

小结:

希望老铁给个一键三连哦

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-12-13 12:40:04  更:2021-12-13 12:40:40 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 5:32:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码