Mathematics foundation for programmers [6. Enumerating blockchains, virtual currencies, currency package constraints, inner loop version] (Java version)

This case is the actual technical application of the coin bag:

You can take a look at the blockchain coin package. Back then, the teacher paid me and I thought about it for a long time.

There are many tools for virtual currency coin packs, so what technology do they use to achieve it. Let's talk about the practical application of the virtual currency inner loop of mathematics [enumeration] to everyone.

Test language: [Java]

 

1. Create currency types (I wrote 4 for easy testing)

Infrastructure [BaseWallet]

package com.item.btype;

import java.math.BigDecimal;
import java.util.Date;

public class BaseWallet {
    private String Id;
    private String UserId;
    private Date LastDate;
    private BigDecimal AllCount;
    private BigDecimal DisCount;


    /**
     * 钱包ID
     */
    public String getId() {
        return Id;
    }

    public void setId(String id) {
        Id = id;
    }

    /**
     * 用户编号
     */
    public String getUserId() {
        return UserId;
    }

    public void setUserId(String userId) {
        UserId = userId;
    }

    /**
     * 最后修改时间
     */
    public Date getLastDate() {
        return LastDate;
    }

    public void setLastDate(Date lastDate) {
        LastDate = lastDate;
    }

    /**
     * 钱包总数量
     */
    public BigDecimal getAllCount() {
        return AllCount;
    }

    public void setAllCount(BigDecimal allCount) {
        AllCount = allCount;
    }

    /**
     * 冻结数量
     */
    public BigDecimal getDisCount() {
        return DisCount;
    }

    public void setDisCount(BigDecimal disCount) {
        DisCount = disCount;
    }
}

Transaction currency【BG】

package com.item.btype;

public class BGWallet extends BaseWallet {
}

Bitcoin【BTC】

package com.item.btype;

public class BTCWallet extends BaseWallet {
}

Ethereum【ETH】

package com.item.btype;

public class ETHWallet extends BaseWallet {
}

Nenmo Coin【OMG】

package com.item.btype;

public class OMGWallet extends BaseWallet {
}

2. Create a currency enumeration

package com.item.btype;

/**
 * @author TeacherFu
 * @date 2021年2月28日12:07:09
 */
public enum WalletEnum {
    BG(1, "BG", "市场币"),
    BTC(1000, "BTC", "比特币"),
    ETC(1001, "ETC", "以太坊"),
    OMG(1002, "OMG", "嫩模币"),
    ;

    /**
     * 根据名字换编号
     *
     * @param Name
     * @return
     */
    public static int GetId(String Name) {
        WalletEnum[] values = WalletEnum.values();
        for (WalletEnum c : WalletEnum.values()) {
            if (c.getName().equals(Name)) {
                return c.getId();
            }
        }
        return -1;
    }

    /**
     * 根据ID换名字
     * @param Id
     * @return
     */
    public static String GetName(int Id) {
        for (WalletEnum c : WalletEnum.values()) {
            if (c.getId() == Id) {
                return c.getName();
            }
        }
        return null;
    }

    /**
     * 编号
     */
    private int Id;
    /**
     * 币种英文名
     */
    private String Name;
    /**
     * 币种中文名
     */
    private String Info;

    /**
     * 构造方法
     *
     * @param Id
     * @param Name
     * @param Info
     */
    WalletEnum(int Id, String Name, String Info) {
    }

    public int getId() {
        return Id;
    }

    public void setId(int id) {
        Id = id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getInfo() {
        return Info;
    }

    public void setInfo(String info) {
        Info = info;
    }
}

3. Coin pack selector

package com.item.btype;

import java.math.BigDecimal;

/**
 * 币包操作选择器
 */
public class SwitchWallet {
    /**
     * 接口层获取移动端的操作币种编号<br/>
     *
     * @param WalletEnumId
     * @param UserId
     * @return 用户当前所查询币种的信息
     */
    public static BaseWallet UserWalletById(int WalletEnumId, String UserId) {
        /**
         * 根据WalletEnumId标识进行case选择
         */
        switch (WalletEnumId) {
            case 1:
                return new BGWallet();
            case 1001:
                return new BTCWallet();
            case 1002:
                return new ETHWallet();
            case 1003:
                return new OMGWallet();
            default:return null;
        }
    }
}

4. Controller accepts data

package com.item.btype;

import java.math.BigDecimal;

public class WalletController {
    /**
     * 充当接口层
     * @return
     */
    public Resful AddChange(int WalletEnumId, String Token, BigDecimal ChangeCount){
        if(WalletEnum.GetName(WalletEnumId)==null){
            return new Resful("币种不符");
        }
        /**
         * Token兑换UserId
         * String UserId=redis.TokenToUserId(Token);
         */
        String UserId="asd2asdzpasodospasodpaos";
        /**
         * 获取用户某币种钱包信息,使用父类表承接
         */
        BaseWallet bw=SwitchWallet.UserWalletById(WalletEnumId,UserId);
        //已经获取到对应表的信息,其它功能在业务逻辑层完成即可。
        return new Resful("成功",bw.getAllCount());
    }
}

5. Summary:

a) The idea of ​​the coin package is to use enumeration as the coin package constraint to simplify code operations in the collection of parent-child relationships.

 

The next content:【】:【】

Starting from the high-rise building, programmers have basic mathematics, from elementary school [what is mathematics] to [discrete mathematics] (mainly graph theory). Let's grow up step by step and work together.

Guess you like

Origin blog.csdn.net/feng8403000/article/details/114214901