Map<String, Integer> totalStock = new HashMap<>();
AtomicInteger total = new AtomicInteger();
private List<Car> carList = new ArrayList<>();
public void testSale() {
produceCar();
for (; ; ) {
if (total.get()>0) {
sleep(TimeUnit.MILLISECONDS,200);
saleCar();
} else {
System.out.println("所有库存都卖完了,快点进货吧!");
break;
}
}
}
private void saleCar() {
new Thread(() -> carList.forEach(car -> car.sale(new Custom(RandomUtil.randomString(10),
RandomUtil.randomDouble(0, 1000000.00)))), "小红").start();
new Thread(() -> carList.forEach(car -> car.sale(new Custom(RandomUtil.randomString(10),
RandomUtil.randomDouble(0, 1000000.00)))), "小张").start();
new Thread(() -> carList.forEach(car -> car.sale(new Custom(RandomUtil.randomString(10),
RandomUtil.randomDouble(0, 1000000.00)))), "小明").start();
new Thread(() -> carList.forEach(car -> car.sale(new Custom(RandomUtil.randomString(10),
RandomUtil.randomDouble(0, 1000000.00)))), "小花").start();
}
private void produceCar() {
System.out.println("开始进购汽车....");
carList.add(new Car("高尔夫", 10, 129888, 0.94f));
totalStock.put("高尔夫", 10);
total.addAndGet(10);
System.out.println("进购高尔夫 10");
carList.add(new Car("Polo", 10, 90998, 0.9f));
totalStock.put("Polo", 10);
total.addAndGet(10);
System.out.println("进购Polo 10");
carList.add(new Car("迈腾", 40, 186898, 0.9f));
totalStock.put("迈腾", 40);
total.addAndGet(40);
System.out.println("进购迈腾 40");
carList.add(new Car("途观", 30, 199088, 0.91f));
totalStock.put("途观", 30);
total.addAndGet(30);
System.out.println("进购途观 30");
System.out.println("共进购" + totalStock + "汽车,总库存" + total);
}
class Custom {
String name;
double capitalFund;
final List<String> cars = new ArrayList<>();
public Custom(String name, double capitalFund) {
this.name = name;
this.capitalFund = capitalFund;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getCapitalFund() {
return capitalFund;
}
public void setCapitalFund(double capitalFund) {
this.capitalFund = capitalFund;
}
public int buyCar(String carName, double carPrice) {
try {
double fund = capitalFund - carPrice;
if (fund >= 0d) {
cars.add("品牌:" + carName + ",价格:" + carPrice);
System.out.println(name + "买人了一辆" + carName + "剩余资产:" + NumberUtil.roundStr(fund, 2));
this.setCapitalFund(fund);
TimeUnit.MILLISECONDS.sleep(20);
return 1;
} else {
System.out.println(name + "想买人了一辆" + carName + "," +
"可是资产不足,总资产:" + NumberUtil.roundStr(capitalFund, 2) + ",需要资产:" + NumberUtil.roundStr(carPrice, 2));
return 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
class Car {
String brandName;
int num;
int price;
double discount;
public Car(String brandName, int num, int price, double discount) {
this.brandName = brandName;
this.num = num;
this.price = price;
this.discount = discount;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
Lock lock = new ReentrantLock();
public void sale(Custom custom) {
lock.lock();
try {
if (num > 0) {
double price = (this.price * this.discount) + (this.price / 1.17 * 0.1);
int r = custom.buyCar(this.getBrandName(), price);
if (r > 0) {
num--;
total.decrementAndGet();
totalStock.put(this.brandName, num);
System.out.println(Thread.currentThread().getName() + "卖给了" + custom.getName() + "一辆" + this.brandName + "车," + this.brandName + "剩余:" + num);
System.out.println("本店库存:" + totalStock + ",total:" + total);
TimeUnit.MILLISECONDS.sleep(10);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
private void sleep(TimeUnit timeUnit, int time) {
try {
timeUnit.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}