工具类
作者:互联网
package com.sunsoft.service.share.biz;
import java.util.List;
import java.util.Random;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
*
* 会员管理接口
* @author DaisyDay
* @date: 2018年9月29日21:00:43
* @version: 1.0
*/
@Transactional
@Service("vipUserBiz")
public class VipUserBiz {
public static final String words = "abcdefghijklmnopqrstuvwxyz0123456789";
/**
* 生成随机字符串
* @param num
* @return
*/
public String getCode(int num) {
Random random = new Random();// 生成随机数
// 定义StringBuffer
StringBuffer sb = new StringBuffer();
for (int i = 0; i < num; i++) {
// 生成一个随机数字
int index = random.nextInt(words.length()); // 生成随机数 0 到 length - 1
// 获得字母数字
char c = words.charAt(index);
sb.append(c);
}
System.out.println("get_code==============code:"+sb.toString());
return sb.toString();
}
/**
* 获取不重复邀请码
* @param num
* @return
*/
public String getCode(int i, List<String> packetCodeList) {
if(packetCodeList.isEmpty()) {
return getCode(i);
}
String code= getCode(i);
if(packetCodeList.contains(code)) {
return getCode(i,packetCodeList);
}else {
return code ;
}
}
}
标签:code,getCode,String,num,sb,return,工具 来源: https://blog.csdn.net/qq_42520477/article/details/100187728