package edu.mama.ls23.homework;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 第二阶段PK作业
*/
public class Homework {
private static final Charset UTF8 = Charset.forName("UTF-8");
/**
* 第一题:12个人抢总额为amount的10个拼手气红包
*
* 要求:
* 1、12个线程同时抢红包,10个抢到,2个抢不到
* 2、抢到的每人最多能抢200元,最少也能抢0.01元
* 3、红包金额保留2位小数,10个人全部抢完不能剩余
* 4、打印每个人红包的金额
* 5、打印手气最佳者的线程名和金额
*
* @param amount 拼手气红包总金额
*/
public static void redEnvelope(BigDecimal amount) {
//TODO 创建红包任务
RedEnvelopeRunnable task = new RedEnvelopeRunnable(amount,10);
//创建12个线程抢红包
Thread people1 = new Thread(task,"people1");
Thread people2 = new Thread(task,"people2");
Thread people3 = new Thread(task,"people3");
Thread people4 = new Thread(task,"people4");
Thread people5 = new Thread(task,"people5");
Thread people6 = new Thread(task,"people6");
Thread people7 = new Thread(task,"people7");
Thread people8 = new Thread(task,"people8");
Thread people9 = new Thread(task,"people9");
Thread people10 = new Thread(task,"people10");
Thread people11 = new Thread(task,"people11");
Thread people12 = new Thread(task,"people12");
people1.start();
people2.start();
people3.start();
people4.start();
people5.start();
people6.start();
people7.start();
people8.start();
people9.start();
people10.start();
people11.start();
people12.start();
try {
people1.join();
people2.join();
people3.join();
people4.join();
people5.join();
people6.join();
people7.join();
people8.join();
people9.join();
people10.join();
people11.join();
people12.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
//遍历map
Set<Map.Entry<String, BigDecimal>> entrySet = task.getMap().entrySet();
BigDecimal maxAmount = BigDecimal.ZERO;
String name = null;
for (Map.Entry<String, BigDecimal> entry : entrySet) {
String key = entry.getKey();
BigDecimal value = entry.getValue();
if (value.compareTo(maxAmount) > 0) {
name = key;
maxAmount = value;
}
}
System.out.println(name + "手气最佳,抢了" + maxAmount);
}
/**
* 第二题:读取path指定的文件中的数据并完成以下统计
*
* 要求:
* 1、统计2020年第一季度销量最高的商品的销售总额,及其占同期所有商品销售额的占比
* 2、统计2020年第二季度苹果电脑的销售额环比第一季度销售额的增幅(%)
*/
public static void statistic(String path) throws IOException {
//TODO
List<Order> orderList = new ArrayList<>();
try(Reader reader = new FileReader(path, UTF8)) {
//一次处理一行
BufferedReader br = new BufferedReader(reader);
String line = br.readLine();
while (line != null ) {
// System.out.println(line + "第一次");
if (!line.equals("月份|商品编号|商品名称|销量|销售额") && !line.equals("")) {
String[] a = line.split("\\|");
a[4] = a[4].replaceAll("¥", "");
a[4] = a[4].replaceAll(",", "");
// System.out.println(a[4]);
Order order = new Order(a[0],a[1],a[2],Integer.parseInt(a[3]),new BigDecimal(a[4]));
orderList.add(order);
}
line = br.readLine();
}
}
//销量
int SoldNum1 = 0;
int SoldNum2 = 0;
int SoldNum3 = 0;
//销售额
BigDecimal soldAmount1 = BigDecimal.ZERO;
BigDecimal soldAmount2 = BigDecimal.ZERO;
BigDecimal soldAmount3 = BigDecimal.ZERO;
BigDecimal soldAmount4 = BigDecimal.ZERO;
for (Order order : orderList) {
if (order.getMonth().equals("2020年01月") || order.getMonth().equals("2020年02月") || order.getMonth().equals("2020年03月")) {
if (order.getProductName().equals("小米手机")) {
//小米手机销售量
SoldNum1 += order.getSoldNum();
//小米手机销售额
soldAmount1 = soldAmount1.add(order.getSoldAmount());
}
if (order.getProductName().equals("苹果电脑")) {
SoldNum2 += order.getSoldNum();
soldAmount2 = soldAmount2.add(order.getSoldAmount());
}
if (order.getProductName().equals("樱桃键盘")) {
SoldNum3 += order.getSoldNum();
soldAmount3 = soldAmount3.add(order.getSoldAmount());
}
}
if (order.getMonth().equals("2020年04月") || order.getMonth().equals("2020年05月") || order.getMonth().equals("2020年06月")) {
if (order.getProductName().equals("苹果电脑")) {
soldAmount4 = soldAmount4.add(order.getSoldAmount());
}
}
}
//第一季度销售总额
BigDecimal soldAmountSum = soldAmount1.add(soldAmount2.add(soldAmount3));
BigDecimal soldAmount = BigDecimal.ZERO;
if (SoldNum1 > SoldNum2) {
soldAmount = soldAmount1;
} else if (SoldNum2 > SoldNum3) {
soldAmount = soldAmount2;
}else {
soldAmount = soldAmount3;
}
BigDecimal soldAmountSumPercent = soldAmount.divide(soldAmountSum,4, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100));
System.out.println("第一季度销量最高销售额是" + soldAmount + ",占同期所有商品销售额的占比" + soldAmountSumPercent + "%");
System.out.println("第二季度苹果电脑的销售额环比第一季度销售额的增幅" + soldAmount4.subtract(soldAmount2).divide(soldAmount2,2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)) + "%");
}
public static void main(String[] args) throws IOException {
redEnvelope(new BigDecimal("286.33"));
statistic("LS23/res/月度销量统计.txt");
}
}
RedEnvelopeRunnable类
package edu.mama.ls23.homework;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
public class RedEnvelopeRunnable implements Runnable{
//规定每人最多最少抢多少
private final BigDecimal MIN = new BigDecimal("0.01");
private final BigDecimal MAX = new BigDecimal("200");
//存每个人抢的红包
private Map<String, BigDecimal> map = new HashMap<>();
//红包
private BigDecimal redEnvelope;
//红包分几份
private int num;
//同步锁
private final String LOCK = "ANYTHING";
public RedEnvelopeRunnable(BigDecimal redEnvelope, int num) {
this.redEnvelope = redEnvelope;
this.num = num;
}
@Override
public void run() {
//同步锁
synchronized (LOCK) {
//计算经过本次抢红包后最多允许剩下的钱数
BigDecimal maxRemind = new BigDecimal(num - 1).multiply(MAX);
//计算经过本次抢红包后最少允许剩下的钱数
BigDecimal minRemind = new BigDecimal(num - 1).multiply(MIN);
if (num != 0){
//本次抢红包金额的最小值
BigDecimal minAmount = MIN.max(redEnvelope.subtract(maxRemind));
BigDecimal maxAmount = MAX.min(redEnvelope.subtract(minRemind));
BigDecimal curAmount = maxAmount.subtract(minAmount)
.multiply(BigDecimal.valueOf(Math.random()))
.add(minAmount)
.setScale(2, RoundingMode.HALF_UP);
redEnvelope = redEnvelope.subtract(curAmount);
System.out.println(Thread.currentThread().getName() + "抢了" + curAmount);
// System.out.println("红包还剩" + redEnvelope);
num = num - 1;
map.put(Thread.currentThread().getName(), curAmount);
} else {
System.out.println(Thread.currentThread().getName() + "手慢没抢到红包");
map.put(Thread.currentThread().getName(), BigDecimal.ZERO);
}
}
}
public Map<String, BigDecimal> getMap() {
return map;
}
}
Order类
package edu.mama.ls23.homework;
import java.math.BigDecimal;
//商城订单
public class Order {
//月份
private String month;
//商品编号
private String productCode;
//商品名称
private String productName;
//销售量
private int soldNum;
//销售额
private BigDecimal soldAmount = BigDecimal.ZERO;
public Order(String month, String productCode, String productName, int soldNum, BigDecimal soldAmount) {
this.month = month;
this.productCode = productCode;
this.productName = productName;
this.soldNum = soldNum;
this.soldAmount = soldAmount;
}
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public String getProductCode() {
return productCode;
}
public void setProductCode(String productCode) {
this.productCode = productCode;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getSoldNum() {
return soldNum;
}
public void setSoldNum(int soldNum) {
this.soldNum = soldNum;
}
public BigDecimal getSoldAmount() {
return soldAmount;
}
public void setSoldAmount(BigDecimal soldAmount) {
this.soldAmount = soldAmount;
}
}
结果