iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Java模仿微信如何实现零钱通简易功能
  • 236
分享到

Java模仿微信如何实现零钱通简易功能

2023-06-22 01:06:17 236人浏览 安东尼
摘要

本篇文章为大家展示了Java模仿微信如何实现零钱通简易功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1. 需求描述使用Java 开发零钱通项目, 模仿微信实现简易功能,可以完成收益入账,消费,查

本篇文章为大家展示了Java模仿微信如何实现零钱通简易功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1. 需求描述

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

预期界面:(实际可能不同)

Java模仿微信如何实现零钱通简易功能

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;

效果演示:

Java模仿微信如何实现零钱通简易功能

保证入账>0

Java模仿微信如何实现零钱通简易功能

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;

效果演示:

Java模仿微信如何实现零钱通简易功能

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;

效果演示:

Java模仿微信如何实现零钱通简易功能

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

效果演示

Java模仿微信如何实现零钱通简易功能

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 menupublic class SmallChangeSysApp {    public static void main(String[] args) {        new SmallChangeSysOOP().mainMenu();    }}

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

import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner;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.2 OOP的好处

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

上述内容就是Java模仿微信如何实现零钱通简易功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注编程网精选频道。

--结束END--

本文标题: Java模仿微信如何实现零钱通简易功能

本文链接: https://www.lsjlt.com/news/301807.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Java模仿微信如何实现零钱通简易功能
    本篇文章为大家展示了Java模仿微信如何实现零钱通简易功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。1. 需求描述使用Java 开发零钱通项目, 模仿微信实现简易功能,可以完成收益入账,消费,查...
    99+
    2023-06-22
  • Java模仿微信实现零钱通简易功能(两种版本)
    目录1. 需求描述2. 需求分析3. 实现零钱通主要功能3.1 写一个菜单3.2 零钱通明细3.3 收益入账3.4 消费3.5 用户退出改进3.6 改进金额判断4. 面向过程版实现5...
    99+
    2022-11-12
  • Android如何实现仿微信@好友功能
    这篇文章主要介绍Android如何实现仿微信@好友功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!先上个效果图就是这么个功能1. 分析需求输入@跳转到联系人界面,选中一个或者多个好友返回到当前界面按退格键删除整块内...
    99+
    2023-05-30
    android
  • C++如何实现简易通讯录功能
    这篇文章主要讲解了“C++如何实现简易通讯录功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++如何实现简易通讯录功能”吧!实现功能提示:这里可以添加本文要记录的大概内容:通过c++语法...
    99+
    2023-07-02
  • Android如何实现仿微信右滑返回功能
    这篇文章将为大家详细讲解有关Android如何实现仿微信右滑返回功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。先上效果图,如下:先分析一下功能的主要技术点,右滑即手势判断,当滑到一直距离时才执行返回,...
    99+
    2023-05-30
    android
  • Android如何实现仿微信朋友圈全文、收起功能
    小编给大家分享一下Android如何实现仿微信朋友圈全文、收起功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!效果图具体代码(详细解释在代码注释中都有,这里就省...
    99+
    2023-05-30
    android
  • 如何利用HTML5+css3+jquery+weui实现仿微信聊天界面功能
    这篇文章主要介绍如何利用HTML5+css3+jquery+weui实现仿微信聊天界面功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!最新因项目需要,就利用HTML5+css3+jquery+weui做了一个仿微信...
    99+
    2023-06-09
  • 微信小程序服务通知功能如何实现
    这篇文章主要介绍“微信小程序服务通知功能如何实现”,在日常操作中,相信很多人在微信小程序服务通知功能如何实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”微信小程序服务通知功能如何实现”的疑惑有所帮助!接下来...
    99+
    2023-06-26
  • 如何通过html5 DeviceOrientation实现微信摇一摇功能
    这篇文章将为大家详细讲解有关如何通过html5 DeviceOrientation实现微信摇一摇功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。在HTML...
    99+
    2022-10-19
  • 微信小程序如何实现简单倒计时功能
    本篇内容介绍了“微信小程序如何实现简单倒计时功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!任务描述:计时器任务要求:案例描述:设计一个实...
    99+
    2023-06-30
  • 利用java如何实现一个短信通信功能
    本篇文章为大家展示了利用java如何实现一个短信通信功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。短信信息的发送目前已经是项目中必不可少的部分,我们怎么通过web页面来实现把信息推送到别人手机上...
    99+
    2023-05-31
    java 短信通信 ava
  • Android如何实现仿微信语音消息的录制和播放功能
    小编给大家分享一下Android如何实现仿微信语音消息的录制和播放功能,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一、简述效果:实现功能:长按Button时改变Button显示文字,弹出Dialog(动态更新音量),动态...
    99+
    2023-05-30
    android
  • 微信小程序如何实现简单的计算器功能
    这篇文章主要介绍微信小程序如何实现简单的计算器功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体内容如下wxml<view class='content'> &nb...
    99+
    2023-06-20
  • 如何通过PHP实现微信小程序的高级功能?
    如何通过PHP实现微信小程序的高级功能?随着微信小程序的快速发展,越来越多的开发者开始关注如何通过PHP实现微信小程序的高级功能。PHP是一种非常强大的后端编程语言,能够与微信小程序进行交互,实现一些复杂的功能和业务逻辑。在本文中,我将分享...
    99+
    2023-10-27
    PHP 微信小程序 高级功能
  • 如何使用Java实现一个简易版的多级菜单功能
    小编给大家分享一下如何使用Java实现一个简易版的多级菜单功能,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!正文1,首先是数据库的设计DROP TABL...
    99+
    2023-06-26
  • 使用java如何实现一个微信H5支付功能
    这篇文章将为大家详细讲解有关使用java如何实现一个微信H5支付功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。前面做了app微信支付的回调处理,现在需要做微信公众号的支付,花了一天多时间...
    99+
    2023-05-31
    h5支付 java
  • Java如何实现微信公众平台朋友圈分享功能
    这篇文章将为大家详细讲解有关Java如何实现微信公众平台朋友圈分享功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。  其实分享的方法在微信官网有较为详细的文档说明,现就其中一些比较绕的步骤进行总结,有问...
    99+
    2023-05-30
    java
  • nodejs中如何实现socket服务端和客户端简单通信功能
    小编给大家分享一下nodejs中如何实现socket服务端和客户端简单通信功能,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!本文实例讲述了通过node.js的net模块实现nodejs s...
    99+
    2022-10-19
  • 如何利用微信小程序和php实现即时通讯聊天功能
    目录一、PHP7安装Swoole扩展1、自定义安装2、宝塔面板安装PHP swoole扩展二、配置nginx反向代理三、微信小程序socket合法域名配置四、效果演示和代码1、小程序...
    99+
    2022-11-13
  • 微信小程序如何实现通过保存图片分享到朋友圈功能
    这篇文章给大家分享的是有关微信小程序如何实现通过保存图片分享到朋友圈功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。说明首先说明一点,小程序内是不能直接分享到朋友圈的。所以只能...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作