最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java模仿微信實(shí)現(xiàn)零錢通簡(jiǎn)易功能(兩種版本)

 更新時(shí)間:2021年12月13日 17:01:35   作者:言之命至9012  
本文主要介紹了使用Java開發(fā)零錢通項(xiàng)目, 模仿微信實(shí)現(xiàn)簡(jiǎn)易功能,可以完成收益入賬,消費(fèi),查看明細(xì),退出系統(tǒng)等功能。文中一共介紹了兩種實(shí)現(xiàn)方法,快來(lái)學(xué)習(xí)吧

最近剛剛復(fù)習(xí)了一下Java的面向?qū)ο笕筇匦?這里跟著hsp做個(gè)小零錢通實(shí)踐一下,以下記錄了學(xué)習(xí)和編寫過(guò)程

1. 需求描述

使用Java 開發(fā)零錢通項(xiàng)目, 模仿微信實(shí)現(xiàn)簡(jiǎn)易功能,可以完成收益入賬,消費(fèi),查看明細(xì),退出系統(tǒng)等功能,先按照一般方法寫,后期在改進(jìn)為OOP

預(yù)期界面:(實(shí)際可能不同)

2. 需求分析

面對(duì)這樣一個(gè)需求,先化繁為簡(jiǎn)

  1. 寫一個(gè)菜單
  2. 完成零錢通明細(xì).
  3. 完成收益入賬
  4. 消費(fèi)
  5. 退出
  6. 用戶輸入4退出時(shí),給出提示"你確定要退出嗎? y/n",必須輸入正確的y/n ,否則循環(huán)輸入指令,直到輸入y 或者 n
  7. 在收益入賬和消費(fèi)時(shí),判斷金額是否合理,并給出相應(yīng)的提示

3. 實(shí)現(xiàn)零錢通主要功能

3.1 寫一個(gè)菜單

先完成顯示菜單,并可以選擇菜單,并且給出對(duì)應(yīng)提示

    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 零錢通明細(xì)

思路

(1) 可以把收益入賬和消費(fèi)保存到數(shù)組

(2) 可以使用對(duì)象

(3) 簡(jiǎn)單的話可以使用String拼接

這里直接采取第三種方式

改變一下switch的case1

 String details = "-----------------零錢通明細(xì)------------------";
   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;

效果演示:

保證入賬>0

3.4 消費(fèi)

定義新的變量

 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;

效果演示:

3.5 用戶退出改進(jìn)

給出確認(rèn),是否要退出

用戶輸入4退出時(shí),給出提示"你確定要退出嗎? y/n",必須輸入正確的y/n ,

否則循環(huán)輸入指令,直到輸入y 或者 n

(1) 定義一個(gè)變量 choice, 接收用戶的輸入

(2) 使用 while + break, 來(lái)處理接收到的輸入時(shí) 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;

效果演示:

3.6 改進(jìn)金額判斷

收入時(shí)

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

支出時(shí)

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

效果演示

4. 面向過(guò)程版實(shí)現(xiàn)

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) 可以把收益入賬和消費(fèi),保存到數(shù)組 (2) 可以使用對(duì)象 (3) 簡(jiǎn)單的話可以使用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. 優(yōu)化成OOP版

很多東西可以直接復(fù)制過(guò)來(lái)變成方法,把原來(lái)的改過(guò)來(lái)是簡(jiǎn)單的

5.1 實(shí)現(xiàn)OOP版

那么先有一個(gè)執(zhí)行的主類SmallChangeSysApp

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

還有一個(gè)類專門是對(duì)象,我們叫它為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.2 OOP的好處

OOP版主函數(shù)很簡(jiǎn)單,只要new這個(gè)對(duì)象就可以了,關(guān)于這個(gè)對(duì)象的其他方法也好屬性也好,不用放在主函數(shù)里面,那樣在主函數(shù)也可以自由加上想加得到內(nèi)容,未來(lái)假如有他人要用,不用把整個(gè)文件拷過(guò)去,只要把類交給對(duì)方即可,這樣擴(kuò)展和可讀性大大提升,要加什么功能就再寫方法原先的擴(kuò)展功能很麻煩,要來(lái)回切

以上就是Java模仿微信實(shí)現(xiàn)零錢通簡(jiǎn)易功能(兩種版本)的詳細(xì)內(nèi)容,更多關(guān)于Java的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java常見基礎(chǔ)數(shù)據(jù)結(jié)構(gòu)

    Java常見基礎(chǔ)數(shù)據(jù)結(jié)構(gòu)

    這篇文章主要介紹了Java常見數(shù)據(jù)結(jié)構(gòu)面試題,帶有答案及解釋,希望對(duì)廣大的程序愛(ài)好者有所幫助,同時(shí)祝大家有一個(gè)好成績(jī),需要的朋友可以參考下,希望可以幫助到你
    2021-07-07
  • 結(jié)合Service層講解DAO層的異常處理操作

    結(jié)合Service層講解DAO層的異常處理操作

    這篇文章主要介紹了結(jié)合Service層講解DAO層的異常處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java捕獲ThreadPoolExecutor內(nèi)部線程異常的四種方法

    Java捕獲ThreadPoolExecutor內(nèi)部線程異常的四種方法

    這篇文章主要為大家詳細(xì)介紹了Java捕獲ThreadPoolExecutor內(nèi)部線程異常的四種方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下
    2025-03-03
  • java啟動(dòng)jar包修改JVM默認(rèn)內(nèi)存問(wèn)題

    java啟動(dòng)jar包修改JVM默認(rèn)內(nèi)存問(wèn)題

    這篇文章主要介紹了java啟動(dòng)jar包修改JVM默認(rèn)內(nèi)存問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法

    Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)游戲抽獎(jiǎng)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • spring?boot?使用Mybatis-plus查詢方法解析

    spring?boot?使用Mybatis-plus查詢方法解析

    這篇文章主要介紹了spring?boot?使用Mybatis-plus查詢方法解析,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • java爬取豆瓣電影示例解析

    java爬取豆瓣電影示例解析

    這篇文章主要介紹了java爬取豆瓣電影示例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 配置idea將Java與數(shù)據(jù)庫(kù)連接起來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖書管理系統(tǒng)

    配置idea將Java與數(shù)據(jù)庫(kù)連接起來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖書管理系統(tǒng)

    這篇文章主要給大家介紹了關(guān)于配置idea將Java與數(shù)據(jù)庫(kù)連接起來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖書管理系統(tǒng)的相關(guān)資料,本文從基于Java的圖書管理系統(tǒng)的背景、系統(tǒng)設(shè)計(jì)、數(shù)據(jù)庫(kù)設(shè)計(jì)和系統(tǒng)實(shí)現(xiàn)等方面進(jìn)行了詳細(xì)的研究,需要的朋友可以參考下
    2023-12-12
  • JAVA面向?qū)ο笾^承?super入門解析

    JAVA面向?qū)ο笾^承?super入門解析

    在JAVA類中使用super來(lái)引用父類的成分,用this來(lái)引用當(dāng)前對(duì)象,如果一個(gè)類從另外一個(gè)類繼承,我們new這個(gè)子類的實(shí)例對(duì)象的時(shí)候,這個(gè)子類對(duì)象里面會(huì)有一個(gè)父類對(duì)象。怎么引用里面的父類對(duì)象呢?用super來(lái)引用,this指當(dāng)前對(duì)象的引用,super是當(dāng)前對(duì)象里面的父對(duì)象的引用
    2022-01-01
  • SpringCloud Config分布式配置中心使用教程介紹

    SpringCloud Config分布式配置中心使用教程介紹

    springcloud config是一個(gè)解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個(gè)部分,server端提供配置文件的存儲(chǔ)、以接口的形式將配置文件的內(nèi)容提供出去,client端通過(guò)接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用
    2022-12-12

最新評(píng)論

潢川县| 象州县| 阜南县| 枝江市| 昌乐县| 石柱| 公安县| 麻江县| 海安县| 恭城| 克山县| 崇文区| 清流县| 隆安县| 伊吾县| 逊克县| 新干县| 柳州市| 山东省| 邵阳县| 洮南市| 大庆市| 海门市| 高唐县| 吉安县| 页游| 河西区| 昌都县| 普格县| 临武县| 西华县| 和平县| 德安县| 镇赉县| 靖西县| 休宁县| 马公市| 瓮安县| 宁海县| 临洮县| 胶州市|