Java Red Envelope rules

Source: Internet
Author: User

Java Red Envelope rules

Lucky Red Envelopes:

Rule: Maximum amount: Total amount/number * multiple

Minimum Amount: 0.01

The last red envelope is the full amount-the claim amount

Random allocation

 Packagecom.utils;ImportJava.math.BigDecimal;ImportJava.math.RoundingMode;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.Random;/*** Red Envelopes Distribution Tool class * */ Public classCashredpackutils {/*** A red envelope at least the number of split*/     Public Static Final intredpack_min_quantity = 1; /*** A red envelope maximum number of splits*/     Public Static Final intredpack_max_quantity = 100; /*** Decimal bit length*/    Private Static Final intScale = 2; /*** Discarded decimal digit processing mode*/    Private Static FinalRoundingmode Rounding_mode =Roundingmode.half_even; /*** Red envelope magnification*/    Private Static FinalBigDecimal times =NewBigDecimal ("3"); /*** Single red envelope Minimum Amount*/     Public Static FinalBigDecimal Single_red_min_money =NewBigDecimal ("0.01"); /*** Maximum amount of a single red envelope*///private static final BigDecimal Single_red_max_money = new BigDecimal ("+");    /*** Recursive calculation of red envelope amount when the maximum consecutive error, more than this number will return the minimum value*/    Private Static Final intError_max_num = 5; /*** Recursive calculation of red envelope amount consecutive error number initial value*/    Private Static Final intError_init_num = 1; /*** Decimal place when calculating the amount, 1000 for 3 decimal places*/    Private Static Final intFraction_length = 1000; /*** Distribute Red envelopes *@paramRedmoney Red Envelope Total amount *@paramnum Bonus Number *@return     */     Public StaticList<bigdecimal> Splitredpackes (BigDecimal Redmoney,intnum) {
list<bigdecimal> redinfolist = new arraylist<>();
Red envelopes Wrong
if (num < redpack_min_quantity | | num > redpack_max_quantity)
      return redinfolist;

//Checksum: Amount greater than 0 if(Redmoney.compareto (bigdecimal.zero)! = 1) { returnredinfolist; } if(Num <= 1) {redinfolist.add (Redmoney); returnredinfolist; } Random Random=NewRandom (); for(inti = 0; i < num; i++) {//System.out.println ("\ n" + (I+1) + "Packet Information:"); intSurplusnum = Num-i;//amount of red envelopes not allocatedBigDecimal Curredmoney =fightluckredpacked (Redmoney, Surplusnum, Random, error_init_num); Redinfolist.add (Curredmoney); Redmoney=redmoney.subtract (Curredmoney); Mandoassert.nottrue (Redmoney.compareto (Bigdecimal.zero)= =-1, "red envelope amount is wrong");//System.out.println ("Red Envelope amount:" + Curredmoney + ", Remaining:" + Redmoney); } returnredinfolist; } /*** Lucky Red Envelopes *@paramRedmoney Red Envelope amount *@paramnum Bonus Number *@paramrandom Number Generation Object *@paramerrornum Error Test *@returnsingle Red envelope amount*/ Private StaticBigDecimal fightluckredpacked (BigDecimal Redmoney,intnum, Random random,interrornum) { if(Num <= 1) { returnRedmoney; } if(ErrorNum >error_max_num) { //The random amount generates an error more than the upper limit and returns the minimum value returnSingle_red_min_money; } //maximum amount per red envelope = amount of total remaining/unallocated amount * red envelope magnification intAvgredmaxmoney = Redmoney.divide (NewBigDecimal (num), rounding_mode). Multiply (times). Intvalue () *fraction_length; BigDecimal Curredmoney=NewBigDecimal (Random.nextint (Avgredmaxmoney) * 1.00/fraction_length + ""). Setscale (scale, rounding_mode); if(Curredmoney.compareto (Single_red_min_money) = =-1) { //Red Envelope minimum value: Less than the minimum bonus amount, recalculate returnfightluckredpacked (Redmoney, num, random, + +)errornum); } /*if (Curredmoney.compareto (single_red_max_money) = = 1) {//Red envelope maximum value is determined by return fightluckredpacked (Curre Dmoney, num, random, ++errornum); }*/ //minimum reserved Bonus amountBigDecimal Surplusminredmoney = single_red_min_money.multiply (NewBigDecimal (num-1)); //In addition to the current amount remaining in red envelopesBigDecimal Surplusredmoney =redmoney.subtract (Curredmoney); if(Surplusminredmoney.compareto (Surplusredmoney) = = 1) { returnfightluckredpacked (Redmoney, num, random, + +)errornum); } returnCurredmoney; } Public Static voidMain (string[] args) {System.out.println (Splitredpackes (NewBigDecimal ("100"), 20)); }}

Java Red Envelope rules

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.