RedPocket类

  1. import java.math.BigDecimal;
  2. import java.math.RoundingMode;
  3. public class RedPacket {
  4. final BigDecimal MIN = new BigDecimal("0.01");
  5. final BigDecimal MAX = new BigDecimal("200");
  6. private BigDecimal amount;
  7. private int count;
  8. public RedPacket(BigDecimal amount, int count) {
  9. this.amount = amount;
  10. this.count = count;
  11. }
  12. public synchronized BigDecimal getRandomMoney() {
  13. if (count == 1) {
  14. BigDecimal temp = amount;
  15. //将总金额全给第十位玩家,然后把总金额置为零
  16. amount = BigDecimal.ZERO;
  17. return temp;
  18. } else {
  19. //经过本线程抢红包后最多允许剩下的钱数
  20. BigDecimal maxRemind = new BigDecimal(count - 1).multiply(MAX);
  21. //经过本线程抢红包后最少允许剩下的钱数
  22. BigDecimal minRemind = new BigDecimal(count - 1).multiply(MIN);
  23. //本线程抢红包金额的最小值
  24. BigDecimal minAmount = MIN.max(amount.subtract(maxRemind));
  25. BigDecimal maxAmount = MAX.min(amount.subtract(minRemind));
  26. BigDecimal curAmount = maxAmount.subtract(minAmount)
  27. .multiply(BigDecimal.valueOf(Math.random()))
  28. .add(minAmount)
  29. .setScale(2, RoundingMode.HALF_UP);
  30. amount = amount.subtract(curAmount);
  31. count--;
  32. return curAmount;
  33. }
  34. }
  35. }

UserTherd类

  1. import java.math.BigDecimal;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. public class UserThread implements Runnable {
  5. private final RedPacket rePacket;
  6. public UserThread(RedPacket rePacket) {
  7. this.rePacket = rePacket;
  8. }
  9. Map<String, BigDecimal> map = new HashMap<>();
  10. @Override
  11. public void run() {
  12. BigDecimal money = rePacket.getRandomMoney();
  13. if (money.equals(BigDecimal.ZERO)) {
  14. System.out.println(Thread.currentThread().getName() + "不好意思,您手慢了!");
  15. } else {
  16. System.out.println(Thread.currentThread().getName() + "抢到了" + money + "元");
  17. map.put(Thread.currentThread().getName(), money);
  18. }
  19. }
  20. public Map<String, BigDecimal> getMap() {
  21. return map;
  22. }
  23. }

sale类

  1. public class Sale {
  2. private String saleVolume;
  3. private String saleMoney;
  4. public Sale(String saleVolume, String saleMoney) {
  5. this.saleVolume = saleVolume;
  6. this.saleMoney = saleMoney;
  7. }
  8. public String getSaleVolume() {
  9. return saleVolume;
  10. }
  11. public void setSaleVolume(String saleVolume) {
  12. this.saleVolume = saleVolume;
  13. }
  14. public String getSaleMoney() {
  15. return saleMoney;
  16. }
  17. public void setSaleMoney(String saleMoney) {
  18. this.saleMoney = saleMoney;
  19. }
  20. }

主函数

  1. import java.io.IOException;
  2. import java.math.BigDecimal;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.Stream;
  10. public class Demo {
  11. public static void redEnvelope(BigDecimal amount) {
  12. final BigDecimal MIN = new BigDecimal("0.01");
  13. final BigDecimal MAX = new BigDecimal("200");
  14. if (amount == null
  15. || amount.compareTo(MIN.multiply(new BigDecimal("10"))) < 0
  16. || amount.compareTo(MAX.multiply(new BigDecimal("10"))) > 0) {
  17. throw new IllegalArgumentException("每人最多抢200元,最少抢0.01元");
  18. }
  19. RedPacket rePacket = new RedPacket(amount, 10);
  20. UserThread userThread = new UserThread(rePacket);
  21. Thread t1 = new Thread(userThread);
  22. Thread t2 = new Thread(userThread);
  23. Thread t3 = new Thread(userThread);
  24. Thread t4 = new Thread(userThread);
  25. Thread t5 = new Thread(userThread);
  26. Thread t6 = new Thread(userThread);
  27. Thread t7 = new Thread(userThread);
  28. Thread t8 = new Thread(userThread);
  29. Thread t9 = new Thread(userThread);
  30. Thread t10 = new Thread(userThread);
  31. Thread t11 = new Thread(userThread);
  32. Thread t12 = new Thread(userThread);
  33. t1.start();
  34. t2.start();
  35. t3.start();
  36. t4.start();
  37. t5.start();
  38. t6.start();
  39. t7.start();
  40. t8.start();
  41. t9.start();
  42. t10.start();
  43. t11.start();
  44. t12.start();
  45. try {
  46. t1.join();
  47. t2.join();
  48. t3.join();
  49. t4.join();
  50. t5.join();
  51. t6.join();
  52. t7.join();
  53. t8.join();
  54. t9.join();
  55. t10.join();
  56. t11.join();
  57. t12.join();
  58. } catch (InterruptedException e) {
  59. e.printStackTrace();
  60. }
  61. //找出最大值赋值给maxMoney
  62. BigDecimal maxMoney = BigDecimal.ZERO;
  63. Map<String, BigDecimal> map = userThread.getMap();
  64. for (Map.Entry<String, BigDecimal> entry : map.entrySet()) {
  65. if (maxMoney.compareTo(entry.getValue()) < 0) {
  66. maxMoney = entry.getValue();
  67. }
  68. }
  69. //找到最大值对应key,输出最大值
  70. for (Map.Entry<String, BigDecimal> entry : map.entrySet()) {
  71. if (entry.getValue().equals(maxMoney)) {
  72. System.out.println(entry.getKey() + "手气最佳,抢了" + maxMoney + "元");
  73. }
  74. }
  75. }
  76. public static void statistic(String path) throws IOException {
  77. Map<String, Double> moneyMap = new HashMap<>();
  78. double num1, num2, num3;
  79. /*
  80. 统计第一季度销售额最高的产品占比
  81. */
  82. try (Stream<String> lines = Files.lines(Paths.get(path))) {
  83. num1 = lines.filter(x -> x.startsWith("2020年01月|SKU03|") ||
  84. x.startsWith("2020年02月|SKU03|") || x.startsWith("2020年03月|SKU03|"))
  85. .map(x -> x.substring(x.indexOf("¥") + 1).replaceAll(",", ""))
  86. .mapToDouble(Double::valueOf)
  87. .sum();
  88. num1 = num1 / 10000.0;
  89. moneyMap.put("小米手机", num1);
  90. }
  91. try (Stream<String> lines = Files.lines(Paths.get(path))) {
  92. num2 = lines.filter(x -> x.startsWith("2020年01月|SKU02|") ||
  93. x.startsWith("2020年02月|SKU02|") || x.startsWith("2020年03月|SKU02|"))
  94. .map(x -> x.substring(x.indexOf("¥") + 1).replaceAll(",", ""))
  95. .mapToDouble(Double::valueOf)
  96. .sum();
  97. num2 = num2 / 10000.0;
  98. moneyMap.put("苹果电脑", num2);
  99. }
  100. try (Stream<String> lines = Files.lines(Paths.get(path))) {
  101. num3 = lines.filter(x -> x.startsWith("2020年01月|SKU01|") ||
  102. x.startsWith("2020年02月|SKU01|") || x.startsWith("2020年03月|SKU01|"))
  103. .map(x -> x.substring(x.indexOf("¥") + 1).replaceAll(",", ""))
  104. .mapToDouble(Double::valueOf)
  105. .sum();
  106. num3 = num3 / 10000.0;
  107. moneyMap.put("樱桃键盘", num3);
  108. }
  109. double maxNum = 0;
  110. for (Map.Entry<String, Double> entry : moneyMap.entrySet()) {
  111. if (maxNum < entry.getValue()) {
  112. maxNum = entry.getValue();
  113. }
  114. }
  115. for (Map.Entry<String, Double> entry : moneyMap.entrySet()) {
  116. if (maxNum == entry.getValue()) {
  117. System.out.print("第一季度销量最多的是" + entry.getKey() + ":" + entry.getValue() + "万元。");
  118. }
  119. }
  120. double result = (maxNum / (num1 + num2 + num3)) * 100.0;
  121. System.out.println("占比" + result + "%");
  122. /*
  123. 第二季度苹果电脑的销售额环比第一季度销售额的增幅(%)
  124. */
  125. double dummy = 0;
  126. try (Stream<String> lines = Files.lines(Paths.get(path))) {
  127. dummy = lines.filter(x -> x.startsWith("2020年04月|SKU02|") ||
  128. x.startsWith("2020年05月|SKU02|") || x.startsWith("2020年06月|SKU02|"))
  129. .map(x -> x.substring(x.indexOf("¥") + 1).replaceAll(",", ""))
  130. .mapToDouble(Double::valueOf)
  131. .sum();
  132. dummy = dummy / 10000.0;
  133. double growthMoney = ((dummy - num2) / num2) * 100;
  134. System.out.println("第二季度苹果电脑的销售额环比第一季度销售额的增幅: " + growthMoney + "%");
  135. }
  136. }
  137. public static void main(String[] args) throws IOException {
  138. redEnvelope(new BigDecimal("600"));
  139. statistic("D:\\WorkSpace\\IDEA\\Test\\src\\月度销量统计.txt");
  140. }
  141. }