1. 程式人生 > >微信紅包實現方法

微信紅包實現方法

package utils;

import com.weixin.model.returnCode.LeftMoneyPackage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DecimalFormat;
import java.util.Random;

public class Test {

    private static Logger logger = LoggerFactory.getLogger(Test.class);

    public static boolean
getRandomMoney(LeftMoneyPackage _leftMoneyPackage) { DecimalFormat decimalFormat = new DecimalFormat("0.00"); // remainSize 剩餘的紅包數量 // remainMoney 剩餘的錢 if (_leftMoneyPackage.remainSize == 1) { _leftMoneyPackage.remainSize--; // return (double) Math.round(_leftMoneyPackage.remainMoney * 100) / 100;
logger.info("領取金錢:"+decimalFormat.format(_leftMoneyPackage.remainMoney)); logger.info("剩餘紅包為:"+_leftMoneyPackage.remainSize); _leftMoneyPackage.remainMoney -= _leftMoneyPackage.remainMoney; logger.info("紅包已經領取完"); return true; } Random r = new
Random(); double min = 0.01; // double max = _leftMoneyPackage.remainMoney / _leftMoneyPackage.remainSize * 2; double money = r.nextDouble() * max; money = money <= min ? 0.01: money; money = Math.floor(money * 100) / 100; _leftMoneyPackage.remainSize--; _leftMoneyPackage.remainMoney -= money; logger.info("領取金錢:"+decimalFormat.format(money)); logger.info("剩餘紅包為:"+_leftMoneyPackage.remainSize); return getRandomMoney(_leftMoneyPackage); } public static void main(String[] args) { LeftMoneyPackage leftMoneyPackage = new LeftMoneyPackage(10.00,3); getRandomMoney(leftMoneyPackage); } }

結果截圖:

這裡寫圖片描述