package com.aa;
import java.util.LinkedList;
import java.util.List;
public class GetGift {
// 奖品仓库
private List<Gift> gifts = new LinkedList<Gift>();
private List<Gift> abstractGifts = new LinkedList<Gift>();
private List<String> winningUser = new LinkedList<String>();
public List<Gift> fillAbstractGifts(long prizeProbability, long thanksProbability) {
GiftType gt1 = new GiftType("实物奖", 1, prizeProbability);
GiftType gt2 = new GiftType("谢谢惠顾", 1, thanksProbability);
Gift g1 = new Gift("实物奖", gt1);
Gift g2 = new Gift("谢谢惠顾", gt2);
abstractGifts.add(g1);
abstractGifts.add(g2);
return abstractGifts;
}
public GetGift() {
// 生成一堆奖品放进奖品仓库
// 一等奖,一个 ,优先级1,|二等奖,两个优先级,2|。。。20等奖,20个,优先级20
for (int i = 1; i <= 20; i++) {
GiftType gt = new GiftType(i + "等奖", i, i);
for (int j = 1; j <= i; j++) {
gifts.add(new Gift(i + "等奖——第" + j + "号", gt));
}
}
}
// 抽奖
public synchronized Gift getGift() {
int randomNumber = (int)(Math.random() * total());
int priority = 0;
for (Gift g : gifts) {
priority += g.getType().getPriority();
if (priority >= randomNumber) {
// 从奖品库移出奖品
gifts.remove(g);
return g;
}
}
// 抽奖次数多于奖品时,没有奖品
return null;
}
// 抽象奖抽奖
public synchronized Gift getAbstractGifts() {
long randomNumber = (long)(Math.random() * totalAbstractGifts());
long priority = 0;
for (Gift g : abstractGifts) {
priority += g.getType().getPriority();
if (priority >= randomNumber) {
// 从奖品库移出奖品
abstractGifts.remove(g);
return g;
}
}
// 抽奖次数多于奖品时,没有奖品
return null;
}
/**
* @param args
*/
public static void main(String[] args) {
// GetGift gg = new GetGift();
// // 一共生成210个奖品,抽210次,多抽显示null
// for (int i = 0; i < 210; i++) {
// System.out.println(gg.getGift());
// }
/**
*
*/
long prizeProbability = 210;
long thanksProbability = 210;
String userDivceID = "xxxxx";
GetGift gg = new GetGift();
if (gg.winningUser.contains(userDivceID)) {
GiftType gt2 = new GiftType("谢谢惠顾", 1, thanksProbability);
Gift gift2 = new Gift("谢谢惠顾", gt2);
System.out.println("谢谢惠顾抽奖结果:" + gift2);
return;
}
gg.fillAbstractGifts(prizeProbability, thanksProbability);
// for (int i = 0; i < 2; i++) {
Gift abstractGifts2 = gg.getAbstractGifts();
System.out.println(abstractGifts2);
if (abstractGifts2.getId().equals("实物奖")) {
Gift gift = gg.getGift();
System.out.println("实物抽奖结果:" + gift);
gg.winningUser.add(userDivceID);
} else if (abstractGifts2.getId().equals("谢谢惠顾")) {
GiftType gt2 = new GiftType("谢谢惠顾", 1, thanksProbability);
Gift gift2 = new Gift("谢谢惠顾", gt2);
System.out.println("谢谢惠顾抽奖结果:" + gift2);
}
// }
// System.out.println(gg.totalAbstractGifts());
}
// 计算总优先级,内部使用
private int total() {
int result = 0;
for (Gift g : gifts) {
result += g.getType().getPriority();
}
return result;
}
// 计算总优先级,内部使用
private long totalAbstractGifts() {
long result = 0;
for (Gift g : abstractGifts) {
result += g.getType().getPriority();
}
return result;
}
}
// 记录奖品的信息
// 如1等奖共1个,优先级为1最难抽
class GiftType {
// 名字(如1等奖)
private String name;
// 这种奖品的数量,数量越大越容易抽到
private long quantity;
// 这种奖品的优先级,最小为1,数越大越容易抽到
private long priority;
public GiftType(String name, long quantity, long priority) {
this.name = name;
this.quantity = quantity;
this.priority = priority;
}
public long getPriority() {
return priority;
}
@Override
public String toString() {
return "GiftType [name=" + name + ", quantity=" + quantity + ", priority=" + priority + "]";
}
}
// 奖品
class Gift {
// 每个奖品有唯一id,抽奖不会重复,格式为"16等奖——第8号"
private String id;
// 这个奖品的类别
private GiftType type;
public Gift(String id, GiftType type) {
this.id = id;
this.type = type;
}
public GiftType getType() {
return type;
}
public String getId() {
return id;
}
@Override
public String toString() {
return "Gift [id=" + id + ", type=" + type + "]";
}
}