/**
*name =xiaddongming
*time = 20210801
*
*/
//红包类
public class RedPacket {
/*红包ID/
private int id;
/*红包金额/
private BigDecimal money;
public RedPacket(int id, BigDecimal money) {
super();
this.id = id;
this.money = money;
}
public RedPacket() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
@Override
public String toString() {
return id + "号用户获得" + money+"元";
}
}
/**
-
红包异常类
*/
public class RedpacketException extends Exception {
public RedpacketException() {
// TODO Auto-generated constructor stub
}public RedpacketException(String msg) {
super(msg);
}
** -
红包管类类
/
public class RedPacketManage {
/* 设置每个红包最小金额 */
static final BigDecimal MIN = new BigDecimal(“0.01”);/**
- @double total 总金额
- @int count 红包个数
- @return 返回生成的所有红包金额集合
- */
public static ArrayList genRedPacket(double total, int count) throws RedpacketException {
// 声明临时变量用于存储所有随机的红包对象
ArrayList packets = new ArrayList();
// 计算每个红包分配最低金额一共需要多少钱
double min = MIN.multiply(new BigDecimal(count)).setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
if (min > total) {
// 红包金额不够分配时,抛出异常
throw new RedpacketException(“每个红包金额不能少于0.01元”);
} else if (min == total) {
// 红包金额恰好每人只够分配0.01元,则平均分配
for (int i = 0; i < count; i++) {
// 创建红包对象
RedPacket item = new RedPacket(i + 1, new BigDecimal(“0.01”));
// 将红包加入集合
packets.add(item);
}
} else {
// 当总金额大于每人最少金额之和时,随机分配
// 将总金额包装为BigDecimal
BigDecimal totalMoney = new BigDecimal(total);
//声明临时变量统计当前分配的金额总数
BigDecimal now = new BigDecimal(0);
// 获取每个红包的比例
double[] scale = randomScale(count);
// 每获取一个红包,则红包的个数减1
for (int i = 0; i < count - 1; i++) {
// 获取当前比例红包需要分配的金额
BigDecimal item = totalMoney.multiply(new BigDecimal(scale[i])).setScale(2, BigDecimal.ROUND_HALF_EVEN);
packets.add(new RedPacket(i + 1, item));
//累计已分配金额总数
now = now.add(item);
}
// 剩余的金额给最后一个
//获取剩余的金额
BigDecimal last = totalMoney.subtract(now);
packets.add(new RedPacket(count, last));
}
return packets;
}
/**
- 随机红包金额比例
- @param count 红包的份数
- @return 每份红包的比例数组
*/
private static double[] randomScale(int count) {
// 临时数组存储所有红包的金额比例
double[] scale = new double[count];
Random r = new Random();
double total = 0.0;
for (int i = 0; i < count; i++) {
// 为每一个元素设置一个1-100随机数
scale[i] = r.nextInt(100) + 1;
// 累计所有随机的数值
total += scale[i];
}
// 循环计算每个红包的金额比例
for (int i = 0; i < count; i++) {
scale[i] = scale[i] / total;
}
return scale;
}
}
import static com06.RedPacketManage.genRedPacket;
//测试类
public class Test {
public static void main(String[] args) {
ArrayList list = null;
try {
list = genRedPacket(10, 10);
} catch (Exception e) {
e.printStackTrace();
}
BigDecimal t = new BigDecimal(0);
for (RedPacket rp : list) {
System.out.println(rp);
t= t.add(rp.getMoney());
}
System.out.println(t);
}
}