Skip to content

Commit 2e38005

Browse files
committed
♻️ 重构红包代码
🔒 安全修复
1 parent 58dc50b commit 2e38005

File tree

3 files changed

+85
-43
lines changed

3 files changed

+85
-43
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
<dependency>
9292
<groupId>com.alibaba</groupId>
9393
<artifactId>fastjson</artifactId>
94-
<version>1.1.40</version>
94+
<version>1.2.31</version>
9595
</dependency>
9696

9797
<dependency>

src/main/java/com/crossoverjie/red/RedPacket.java

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,121 +15,129 @@ public class RedPacket {
1515
/**
1616
* 生成红包最小值 1分
1717
*/
18-
private static final int MIN_MONEY = 1 ;
18+
private static final int MIN_MONEY = 1;
1919

2020
/**
2121
* 生成红包最大值 200人民币
2222
*/
23-
private static final int MAX_MONEY = 200 * 100 ;
23+
private static final int MAX_MONEY = 200 * 100;
2424

2525
/**
2626
* 小于最小值
2727
*/
28-
private static final int LESS = -1 ;
28+
private static final int LESS = -1;
2929
/**
3030
* 大于最大值
3131
*/
32-
private static final int MORE = -2 ;
32+
private static final int MORE = -2;
3333

3434
/**
3535
* 正常值
3636
*/
37-
private static final int OK = 1 ;
37+
private static final int OK = 1;
3838

3939
/**
4040
* 最大的红包是平均值的 TIMES 倍,防止某一次分配红包较大
4141
*/
42-
private static final double TIMES = 2.1F ;
42+
private static final double TIMES = 2.1F;
4343

44-
private int recursiveCount = 0 ;
44+
private int recursiveCount = 0;
45+
46+
public List<Integer> splitRedPacket(int money, int count) {
47+
List<Integer> moneys = new LinkedList<>();
48+
49+
//金额检查,如果最大红包 * 个数 < 总金额;则需要调大最小红包 MAX_MONEY
50+
if (MAX_MONEY * count <= money) {
51+
System.err.println("请调大最小红包金额 MAX_MONEY=[" + MAX_MONEY + "]");
52+
return moneys ;
53+
}
4554

46-
private List<Integer> splitRedPacket(int money,int count){
47-
List<Integer> moneys = new LinkedList<>() ;
4855

4956
//计算出最大红包
50-
int max = (int) ((money / count) * TIMES) ;
51-
max = max > MAX_MONEY ? MAX_MONEY : max ;
57+
int max = (int) ((money / count) * TIMES);
58+
max = max > MAX_MONEY ? MAX_MONEY : max;
5259

53-
for (int i = 0 ; i< count ; i++){
60+
for (int i = 0; i < count; i++) {
5461
//随机获取红包
55-
int redPacket = randomRedPacket(money, MIN_MONEY,max,count - i) ;
62+
int redPacket = randomRedPacket(money, MIN_MONEY, max, count - i);
5663
moneys.add(redPacket);
5764
//总金额每次减少
58-
money -= redPacket ;
65+
money -= redPacket;
5966
}
6067

61-
return moneys ;
68+
return moneys;
6269
}
6370

6471
private int randomRedPacket(int totalMoney, int minMoney, int maxMoney, int count) {
6572
//只有一个红包直接返回
66-
if (count == 1){
67-
return totalMoney ;
73+
if (count == 1) {
74+
return totalMoney;
6875
}
6976

70-
if (minMoney == maxMoney){
71-
return minMoney ;
77+
if (minMoney == maxMoney) {
78+
return minMoney;
7279
}
7380

7481
//如果最大金额大于了剩余金额 则用剩余金额 因为这个 money 每分配一次都会减小
75-
maxMoney = maxMoney > totalMoney ? totalMoney : maxMoney ;
82+
maxMoney = maxMoney > totalMoney ? totalMoney : maxMoney;
7683

7784
//在 minMoney到maxMoney 生成一个随机红包
78-
int redPacket = (int) (Math.random() * (maxMoney - minMoney) + minMoney) ;
85+
int redPacket = (int) (Math.random() * (maxMoney - minMoney) + minMoney);
7986

80-
int lastMoney = totalMoney - redPacket ;
87+
int lastMoney = totalMoney - redPacket;
8188

82-
int status = checkMoney(lastMoney,count - 1) ;
89+
int status = checkMoney(lastMoney, count - 1);
8390

8491
//正常金额
85-
if (OK == status){
86-
return redPacket ;
92+
if (OK == status) {
93+
return redPacket;
8794
}
8895

8996
//如果生成的金额不合法 则递归重新生成
90-
if (LESS == status){
91-
recursiveCount ++ ;
97+
if (LESS == status) {
98+
recursiveCount++;
9299
System.out.println("recursiveCount==" + recursiveCount);
93-
return randomRedPacket(totalMoney,minMoney,redPacket,count) ;
100+
return randomRedPacket(totalMoney, minMoney, redPacket, count);
94101
}
95102

96-
if (MORE == status){
97-
recursiveCount ++ ;
103+
if (MORE == status) {
104+
recursiveCount++;
98105
System.out.println("recursiveCount===" + recursiveCount);
99-
return randomRedPacket(totalMoney,redPacket,maxMoney,count) ;
106+
return randomRedPacket(totalMoney, redPacket, maxMoney, count);
100107
}
101108

102-
return redPacket ;
109+
return redPacket;
103110
}
104111

105112
/**
106113
* 校验剩余的金额的平均值是否在 最小值和最大值这个范围内
114+
*
107115
* @param lastMoney
108116
* @param count
109117
* @return
110118
*/
111119
private int checkMoney(int lastMoney, int count) {
112-
double avg = lastMoney / count ;
113-
if (avg < MIN_MONEY){
114-
return LESS ;
120+
double avg = lastMoney / count;
121+
if (avg < MIN_MONEY) {
122+
return LESS;
115123
}
116124

117-
if (avg > MAX_MONEY){
118-
return MORE ;
125+
if (avg > MAX_MONEY) {
126+
return MORE;
119127
}
120128

121-
return OK ;
129+
return OK;
122130
}
123131

124132

125133
public static void main(String[] args) {
126-
RedPacket redPacket = new RedPacket() ;
134+
RedPacket redPacket = new RedPacket();
127135
List<Integer> redPackets = redPacket.splitRedPacket(20000, 100);
128-
System.out.println(redPackets) ;
136+
System.out.println(redPackets);
129137

130-
int sum = 0 ;
138+
int sum = 0;
131139
for (Integer red : redPackets) {
132-
sum += red ;
140+
sum += red;
133141
}
134142
System.out.println(sum);
135143
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.crossoverjie.red;
2+
3+
import org.junit.Test;
4+
5+
import java.util.List;
6+
7+
public class RedPacketTest {
8+
9+
@Test
10+
public void right(){
11+
RedPacket redPacket = new RedPacket() ;
12+
List<Integer> redPackets = redPacket.splitRedPacket(20000, 100);
13+
System.out.println(redPackets) ;
14+
15+
int sum = 0 ;
16+
for (Integer red : redPackets) {
17+
sum += red ;
18+
}
19+
System.out.println(sum);
20+
}
21+
22+
@Test
23+
public void right_(){
24+
RedPacket redPacket = new RedPacket() ;
25+
List<Integer> redPackets = redPacket.splitRedPacket(40000, 2);
26+
System.out.println(redPackets) ;
27+
28+
int sum = 0 ;
29+
for (Integer red : redPackets) {
30+
sum += red ;
31+
}
32+
System.out.println(sum);
33+
}
34+
}

0 commit comments

Comments
 (0)