RedPocket类
import java.math.BigDecimal;import java.math.RoundingMode;public class RedPacket { final BigDecimal MIN = new BigDecimal("0.01"); final BigDecimal MAX = new BigDecimal("200"); private BigDecimal amount; private int count; public RedPacket(BigDecimal amount, int count) { this.amount = amount; this.count = count; } public synchronized BigDecimal getRandomMoney() { if (count == 1) { BigDecimal temp = amount; //将总金额全给第十位玩家,然后把总金额置为零 amount = BigDecimal.ZERO; return temp; } else { //经过本线程抢红包后最多允许剩下的钱数 BigDecimal maxRemind = new BigDecimal(count - 1).multiply(MAX); //经过本线程抢红包后最少允许剩下的钱数 BigDecimal minRemind = new BigDecimal(count - 1).multiply(MIN); //本线程抢红包金额的最小值 BigDecimal minAmount = MIN.max(amount.subtract(maxRemind)); BigDecimal maxAmount = MAX.min(amount.subtract(minRemind)); BigDecimal curAmount = maxAmount.subtract(minAmount) .multiply(BigDecimal.valueOf(Math.random())) .add(minAmount) .setScale(2, RoundingMode.HALF_UP); amount = amount.subtract(curAmount); count--; return curAmount; } }}
UserTherd类
import java.math.BigDecimal;import java.util.HashMap;import java.util.Map;public class UserThread implements Runnable { private final RedPacket rePacket; public UserThread(RedPacket rePacket) { this.rePacket = rePacket; } Map<String, BigDecimal> map = new HashMap<>(); @Override public void run() { BigDecimal money = rePacket.getRandomMoney(); if (money.equals(BigDecimal.ZERO)) { System.out.println(Thread.currentThread().getName() + "不好意思,您手慢了!"); } else { System.out.println(Thread.currentThread().getName() + "抢到了" + money + "元"); map.put(Thread.currentThread().getName(), money); } } public Map<String, BigDecimal> getMap() { return map; }}
sale类
public class Sale { private String saleVolume; private String saleMoney; public Sale(String saleVolume, String saleMoney) { this.saleVolume = saleVolume; this.saleMoney = saleMoney; } public String getSaleVolume() { return saleVolume; } public void setSaleVolume(String saleVolume) { this.saleVolume = saleVolume; } public String getSaleMoney() { return saleMoney; } public void setSaleMoney(String saleMoney) { this.saleMoney = saleMoney; }}
主函数
import java.io.IOException;import java.math.BigDecimal;import java.nio.file.Files;import java.nio.file.Paths;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.stream.Collectors;import java.util.stream.Stream;public class Demo { public static void redEnvelope(BigDecimal amount) { final BigDecimal MIN = new BigDecimal("0.01"); final BigDecimal MAX = new BigDecimal("200"); if (amount == null || amount.compareTo(MIN.multiply(new BigDecimal("10"))) < 0 || amount.compareTo(MAX.multiply(new BigDecimal("10"))) > 0) { throw new IllegalArgumentException("每人最多抢200元,最少抢0.01元"); } RedPacket rePacket = new RedPacket(amount, 10); UserThread userThread = new UserThread(rePacket); Thread t1 = new Thread(userThread); Thread t2 = new Thread(userThread); Thread t3 = new Thread(userThread); Thread t4 = new Thread(userThread); Thread t5 = new Thread(userThread); Thread t6 = new Thread(userThread); Thread t7 = new Thread(userThread); Thread t8 = new Thread(userThread); Thread t9 = new Thread(userThread); Thread t10 = new Thread(userThread); Thread t11 = new Thread(userThread); Thread t12 = new Thread(userThread); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); t6.start(); t7.start(); t8.start(); t9.start(); t10.start(); t11.start(); t12.start(); try { t1.join(); t2.join(); t3.join(); t4.join(); t5.join(); t6.join(); t7.join(); t8.join(); t9.join(); t10.join(); t11.join(); t12.join(); } catch (InterruptedException e) { e.printStackTrace(); } //找出最大值赋值给maxMoney BigDecimal maxMoney = BigDecimal.ZERO; Map<String, BigDecimal> map = userThread.getMap(); for (Map.Entry<String, BigDecimal> entry : map.entrySet()) { if (maxMoney.compareTo(entry.getValue()) < 0) { maxMoney = entry.getValue(); } } //找到最大值对应key,输出最大值 for (Map.Entry<String, BigDecimal> entry : map.entrySet()) { if (entry.getValue().equals(maxMoney)) { System.out.println(entry.getKey() + "手气最佳,抢了" + maxMoney + "元"); } } } public static void statistic(String path) throws IOException { Map<String, Double> moneyMap = new HashMap<>(); double num1, num2, num3; /* 统计第一季度销售额最高的产品占比 */ try (Stream<String> lines = Files.lines(Paths.get(path))) { num1 = lines.filter(x -> x.startsWith("2020年01月|SKU03|") || x.startsWith("2020年02月|SKU03|") || x.startsWith("2020年03月|SKU03|")) .map(x -> x.substring(x.indexOf("¥") + 1).replaceAll(",", "")) .mapToDouble(Double::valueOf) .sum(); num1 = num1 / 10000.0; moneyMap.put("小米手机", num1); } try (Stream<String> lines = Files.lines(Paths.get(path))) { num2 = lines.filter(x -> x.startsWith("2020年01月|SKU02|") || x.startsWith("2020年02月|SKU02|") || x.startsWith("2020年03月|SKU02|")) .map(x -> x.substring(x.indexOf("¥") + 1).replaceAll(",", "")) .mapToDouble(Double::valueOf) .sum(); num2 = num2 / 10000.0; moneyMap.put("苹果电脑", num2); } try (Stream<String> lines = Files.lines(Paths.get(path))) { num3 = lines.filter(x -> x.startsWith("2020年01月|SKU01|") || x.startsWith("2020年02月|SKU01|") || x.startsWith("2020年03月|SKU01|")) .map(x -> x.substring(x.indexOf("¥") + 1).replaceAll(",", "")) .mapToDouble(Double::valueOf) .sum(); num3 = num3 / 10000.0; moneyMap.put("樱桃键盘", num3); } double maxNum = 0; for (Map.Entry<String, Double> entry : moneyMap.entrySet()) { if (maxNum < entry.getValue()) { maxNum = entry.getValue(); } } for (Map.Entry<String, Double> entry : moneyMap.entrySet()) { if (maxNum == entry.getValue()) { System.out.print("第一季度销量最多的是" + entry.getKey() + ":" + entry.getValue() + "万元。"); } } double result = (maxNum / (num1 + num2 + num3)) * 100.0; System.out.println("占比" + result + "%"); /* 第二季度苹果电脑的销售额环比第一季度销售额的增幅(%) */ double dummy = 0; try (Stream<String> lines = Files.lines(Paths.get(path))) { dummy = lines.filter(x -> x.startsWith("2020年04月|SKU02|") || x.startsWith("2020年05月|SKU02|") || x.startsWith("2020年06月|SKU02|")) .map(x -> x.substring(x.indexOf("¥") + 1).replaceAll(",", "")) .mapToDouble(Double::valueOf) .sum(); dummy = dummy / 10000.0; double growthMoney = ((dummy - num2) / num2) * 100; System.out.println("第二季度苹果电脑的销售额环比第一季度销售额的增幅: " + growthMoney + "%"); } } public static void main(String[] args) throws IOException { redEnvelope(new BigDecimal("600")); statistic("D:\\WorkSpace\\IDEA\\Test\\src\\月度销量统计.txt"); }}