1. package edu.mama.ls23.homework;
    2. import java.io.BufferedReader;
    3. import java.io.FileReader;
    4. import java.io.IOException;
    5. import java.io.Reader;
    6. import java.math.BigDecimal;
    7. import java.math.RoundingMode;
    8. import java.nio.charset.Charset;
    9. import java.util.ArrayList;
    10. import java.util.List;
    11. import java.util.Map;
    12. import java.util.Set;
    13. /**
    14. * 第二阶段PK作业
    15. */
    16. public class Homework {
    17. private static final Charset UTF8 = Charset.forName("UTF-8");
    18. /**
    19. * 第一题:12个人抢总额为amount的10个拼手气红包
    20. *
    21. * 要求:
    22. * 1、12个线程同时抢红包,10个抢到,2个抢不到
    23. * 2、抢到的每人最多能抢200元,最少也能抢0.01元
    24. * 3、红包金额保留2位小数,10个人全部抢完不能剩余
    25. * 4、打印每个人红包的金额
    26. * 5、打印手气最佳者的线程名和金额
    27. *
    28. * @param amount 拼手气红包总金额
    29. */
    30. public static void redEnvelope(BigDecimal amount) {
    31. //TODO 创建红包任务
    32. RedEnvelopeRunnable task = new RedEnvelopeRunnable(amount,10);
    33. //创建12个线程抢红包
    34. Thread people1 = new Thread(task,"people1");
    35. Thread people2 = new Thread(task,"people2");
    36. Thread people3 = new Thread(task,"people3");
    37. Thread people4 = new Thread(task,"people4");
    38. Thread people5 = new Thread(task,"people5");
    39. Thread people6 = new Thread(task,"people6");
    40. Thread people7 = new Thread(task,"people7");
    41. Thread people8 = new Thread(task,"people8");
    42. Thread people9 = new Thread(task,"people9");
    43. Thread people10 = new Thread(task,"people10");
    44. Thread people11 = new Thread(task,"people11");
    45. Thread people12 = new Thread(task,"people12");
    46. people1.start();
    47. people2.start();
    48. people3.start();
    49. people4.start();
    50. people5.start();
    51. people6.start();
    52. people7.start();
    53. people8.start();
    54. people9.start();
    55. people10.start();
    56. people11.start();
    57. people12.start();
    58. try {
    59. people1.join();
    60. people2.join();
    61. people3.join();
    62. people4.join();
    63. people5.join();
    64. people6.join();
    65. people7.join();
    66. people8.join();
    67. people9.join();
    68. people10.join();
    69. people11.join();
    70. people12.join();
    71. } catch (InterruptedException e) {
    72. e.printStackTrace();
    73. }
    74. //遍历map
    75. Set<Map.Entry<String, BigDecimal>> entrySet = task.getMap().entrySet();
    76. BigDecimal maxAmount = BigDecimal.ZERO;
    77. String name = null;
    78. for (Map.Entry<String, BigDecimal> entry : entrySet) {
    79. String key = entry.getKey();
    80. BigDecimal value = entry.getValue();
    81. if (value.compareTo(maxAmount) > 0) {
    82. name = key;
    83. maxAmount = value;
    84. }
    85. }
    86. System.out.println(name + "手气最佳,抢了" + maxAmount);
    87. }
    88. /**
    89. * 第二题:读取path指定的文件中的数据并完成以下统计
    90. *
    91. * 要求:
    92. * 1、统计2020年第一季度销量最高的商品的销售总额,及其占同期所有商品销售额的占比
    93. * 2、统计2020年第二季度苹果电脑的销售额环比第一季度销售额的增幅(%)
    94. */
    95. public static void statistic(String path) throws IOException {
    96. //TODO
    97. List<Order> orderList = new ArrayList<>();
    98. try(Reader reader = new FileReader(path, UTF8)) {
    99. //一次处理一行
    100. BufferedReader br = new BufferedReader(reader);
    101. String line = br.readLine();
    102. while (line != null ) {
    103. // System.out.println(line + "第一次");
    104. if (!line.equals("月份|商品编号|商品名称|销量|销售额") && !line.equals("")) {
    105. String[] a = line.split("\\|");
    106. a[4] = a[4].replaceAll("¥", "");
    107. a[4] = a[4].replaceAll(",", "");
    108. // System.out.println(a[4]);
    109. Order order = new Order(a[0],a[1],a[2],Integer.parseInt(a[3]),new BigDecimal(a[4]));
    110. orderList.add(order);
    111. }
    112. line = br.readLine();
    113. }
    114. }
    115. //销量
    116. int SoldNum1 = 0;
    117. int SoldNum2 = 0;
    118. int SoldNum3 = 0;
    119. //销售额
    120. BigDecimal soldAmount1 = BigDecimal.ZERO;
    121. BigDecimal soldAmount2 = BigDecimal.ZERO;
    122. BigDecimal soldAmount3 = BigDecimal.ZERO;
    123. BigDecimal soldAmount4 = BigDecimal.ZERO;
    124. for (Order order : orderList) {
    125. if (order.getMonth().equals("2020年01月") || order.getMonth().equals("2020年02月") || order.getMonth().equals("2020年03月")) {
    126. if (order.getProductName().equals("小米手机")) {
    127. //小米手机销售量
    128. SoldNum1 += order.getSoldNum();
    129. //小米手机销售额
    130. soldAmount1 = soldAmount1.add(order.getSoldAmount());
    131. }
    132. if (order.getProductName().equals("苹果电脑")) {
    133. SoldNum2 += order.getSoldNum();
    134. soldAmount2 = soldAmount2.add(order.getSoldAmount());
    135. }
    136. if (order.getProductName().equals("樱桃键盘")) {
    137. SoldNum3 += order.getSoldNum();
    138. soldAmount3 = soldAmount3.add(order.getSoldAmount());
    139. }
    140. }
    141. if (order.getMonth().equals("2020年04月") || order.getMonth().equals("2020年05月") || order.getMonth().equals("2020年06月")) {
    142. if (order.getProductName().equals("苹果电脑")) {
    143. soldAmount4 = soldAmount4.add(order.getSoldAmount());
    144. }
    145. }
    146. }
    147. //第一季度销售总额
    148. BigDecimal soldAmountSum = soldAmount1.add(soldAmount2.add(soldAmount3));
    149. BigDecimal soldAmount = BigDecimal.ZERO;
    150. if (SoldNum1 > SoldNum2) {
    151. soldAmount = soldAmount1;
    152. } else if (SoldNum2 > SoldNum3) {
    153. soldAmount = soldAmount2;
    154. }else {
    155. soldAmount = soldAmount3;
    156. }
    157. BigDecimal soldAmountSumPercent = soldAmount.divide(soldAmountSum,4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
    158. System.out.println("第一季度销量最高销售额是" + soldAmount + ",占同期所有商品销售额的占比" + soldAmountSumPercent + "%");
    159. System.out.println("第二季度苹果电脑的销售额环比第一季度销售额的增幅" + soldAmount4.subtract(soldAmount2).divide(soldAmount2,2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)) + "%");
    160. }
    161. public static void main(String[] args) throws IOException {
    162. redEnvelope(new BigDecimal("286.33"));
    163. statistic("LS23/res/月度销量统计.txt");
    164. }
    165. }

    RedEnvelopeRunnable类

    1. package edu.mama.ls23.homework;
    2. import java.math.BigDecimal;
    3. import java.math.RoundingMode;
    4. import java.util.HashMap;
    5. import java.util.Map;
    6. public class RedEnvelopeRunnable implements Runnable{
    7. //规定每人最多最少抢多少
    8. private final BigDecimal MIN = new BigDecimal("0.01");
    9. private final BigDecimal MAX = new BigDecimal("200");
    10. //存每个人抢的红包
    11. private Map<String, BigDecimal> map = new HashMap<>();
    12. //红包
    13. private BigDecimal redEnvelope;
    14. //红包分几份
    15. private int num;
    16. //同步锁
    17. private final String LOCK = "ANYTHING";
    18. public RedEnvelopeRunnable(BigDecimal redEnvelope, int num) {
    19. this.redEnvelope = redEnvelope;
    20. this.num = num;
    21. }
    22. @Override
    23. public void run() {
    24. //同步锁
    25. synchronized (LOCK) {
    26. //计算经过本次抢红包后最多允许剩下的钱数
    27. BigDecimal maxRemind = new BigDecimal(num - 1).multiply(MAX);
    28. //计算经过本次抢红包后最少允许剩下的钱数
    29. BigDecimal minRemind = new BigDecimal(num - 1).multiply(MIN);
    30. if (num != 0){
    31. //本次抢红包金额的最小值
    32. BigDecimal minAmount = MIN.max(redEnvelope.subtract(maxRemind));
    33. BigDecimal maxAmount = MAX.min(redEnvelope.subtract(minRemind));
    34. BigDecimal curAmount = maxAmount.subtract(minAmount)
    35. .multiply(BigDecimal.valueOf(Math.random()))
    36. .add(minAmount)
    37. .setScale(2, RoundingMode.HALF_UP);
    38. redEnvelope = redEnvelope.subtract(curAmount);
    39. System.out.println(Thread.currentThread().getName() + "抢了" + curAmount);
    40. // System.out.println("红包还剩" + redEnvelope);
    41. num = num - 1;
    42. map.put(Thread.currentThread().getName(), curAmount);
    43. } else {
    44. System.out.println(Thread.currentThread().getName() + "手慢没抢到红包");
    45. map.put(Thread.currentThread().getName(), BigDecimal.ZERO);
    46. }
    47. }
    48. }
    49. public Map<String, BigDecimal> getMap() {
    50. return map;
    51. }
    52. }

    Order类

    1. package edu.mama.ls23.homework;
    2. import java.math.BigDecimal;
    3. //商城订单
    4. public class Order {
    5. //月份
    6. private String month;
    7. //商品编号
    8. private String productCode;
    9. //商品名称
    10. private String productName;
    11. //销售量
    12. private int soldNum;
    13. //销售额
    14. private BigDecimal soldAmount = BigDecimal.ZERO;
    15. public Order(String month, String productCode, String productName, int soldNum, BigDecimal soldAmount) {
    16. this.month = month;
    17. this.productCode = productCode;
    18. this.productName = productName;
    19. this.soldNum = soldNum;
    20. this.soldAmount = soldAmount;
    21. }
    22. public String getMonth() {
    23. return month;
    24. }
    25. public void setMonth(String month) {
    26. this.month = month;
    27. }
    28. public String getProductCode() {
    29. return productCode;
    30. }
    31. public void setProductCode(String productCode) {
    32. this.productCode = productCode;
    33. }
    34. public String getProductName() {
    35. return productName;
    36. }
    37. public void setProductName(String productName) {
    38. this.productName = productName;
    39. }
    40. public int getSoldNum() {
    41. return soldNum;
    42. }
    43. public void setSoldNum(int soldNum) {
    44. this.soldNum = soldNum;
    45. }
    46. public BigDecimal getSoldAmount() {
    47. return soldAmount;
    48. }
    49. public void setSoldAmount(BigDecimal soldAmount) {
    50. this.soldAmount = soldAmount;
    51. }
    52. }

    结果
    image.png