java micro-channel red
Micro-channel is now the people's daily life is inseparable from the App , daily communication, small transfers, video calls easy.
Micro letter envelopes is that people use most of the features, go out to eat with friends after dinner AA micro letter envelopes; holidays, family and friends to send a red envelope ......
This article teaches you use java to be a small red envelope project!
Precautions
Because the points related to the amount, two decimal places, the value type of conversion operation to be noted;
Of which the program is relatively simple, it does not achieve the maximum in each red 0.01 remaining red mean 2 Ploidy;
/*
* Red envelope design requirements:
* 1- red minimum 0.01, maximum 200
* 2- red envelopes of money in denominations of two decimal places
* 3- individual red value is too large to avoid, by setting the coefficient
* 4- envelopes meet the total Amount
* */public class RedPacketUtil {
// red micro-channel maximum and minimum values, and the maximum amount of red coefficient
private static final float MINMUM = 0.01f;
private static final float MAXMUM = 200.00f;
private static final float TIMES = 2.1f;
// determine the current value and quantity are correct
public boolean isRight(float money,int count){
// calculate the current average
float ave = (float)money / count;
if(ave < MINMUM){
return false;
}else if(ave > MAXMUM){
return false;
}
return true;
}
// generated for each specific amount of red envelopes
public float redPacket(float money, float min, float maxs, int count) {
// determine the current number
if(count == 1){
// to ensure that not less than 0.01 yuan red envelope
money = money > MINMUM ? money : MINMUM;
return (float)(Math.round(money * 100)) / 100;
}
float max = maxs > money ? money : maxs;
// generates a single number of envelopes, and to ensure the accuracy of the red
float one = (float) (Math.random()*(max - min) + min);
one = (float)(Math.round(one * 100)) / 100;
float moneyRest = (money - one);
// determine the reasonableness of the current number of red envelopes
if(isRight(moneyRest,count - 1)){
return one;
}else{
// reassign red
float ave = (float)moneyRest / (count-1);
if(ave < MINMUM){
return redPacket(money,min,one,count);
}else if(ave > MAXMUM){
return redPacket(money,one,max,count);
}
}
return one;
}
// split envelopes to generate a specified number of red
public List<Float> splitRedPacket(float money,int count){
// determine the current value and quantity are correct
if(!isRight(money,count)){
return null;
}
// record the number of each of the red envelope
List<Float> rpList = new ArrayList<Float>();
// The maximum amount of a single red envelopes
float max = (float)(money * TIMES)/ count ;
max = max > MAXMUM ? MAXMUM : max;
float one = 0;
// start of each record number of red envelopes
for(int i = 0;i < count;i++){
one = redPacket(money,MINMUM,max,count-i);
rpList.add(one);
money = money - one;
}
return rpList;
}
public static void main(String[] args) {
RedPacketUtil util = new RedPacketUtil();
List<Float> result = util.splitRedPacket(200, 10);
System.out.println(result);
// verify the total amount
float sum = 0;
for(float i : result){
sum = sum + i;
}
System.out.println(sum);
}}