在javahome项目中程序模拟一个仓库进货/出货系统,实现以下场景: 1、仓库包括多个进货点和多个销售点, 2、仓库中有多种货物(每种货物的数量均大于50) 3、一种进货点负责采购一种货物,销售点可以销售多种货物。 4、通过多线程模拟多个销售点随机销售不同商品。当一种货物数量少于20时,通知商品对应的进货点开启新线程模拟进货。 5、整个系统模拟运行10秒后自动停止运行。
时间: 2023-06-26 22:05:31 浏览: 320
好的,这是一个比较复杂的项目,需要使用多线程和同步控制来实现。我们可以按照以下步骤来完成该项目:
1. 定义仓库类Warehouse,包括进货点、销售点和货物信息。进货点和销售点可以使用ArrayList存储,货物信息可以使用HashMap存储。
2. 定义进货点类PurchasePoint,包括进货的货物种类和数量,以及进货的方法。
3. 定义销售点类SalesPoint,包括销售的货物种类和数量,以及销售的方法。
4. 定义货物类Goods,包括货物名称和数量。
5. 在Warehouse类中定义一个方法,用于检查货物数量是否少于20。如果是,则通知对应的进货点开启新线程模拟进货。
6. 在SalesPoint类中定义一个线程,用于随机销售不同的商品。
7. 在主函数中初始化仓库、进货点和销售点,并启动销售点线程。然后模拟运行10秒后停止运行。
下面是一个简单的实现代码,供参考:
```java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class Warehouse {
private ArrayList<PurchasePoint> purchasePoints;
private ArrayList<SalesPoint> salesPoints;
private HashMap<String, Goods> goodsList;
public Warehouse() {
purchasePoints = new ArrayList<>();
salesPoints = new ArrayList<>();
goodsList = new HashMap<>();
}
public void addPurchasePoint(PurchasePoint purchasePoint) {
purchasePoints.add(purchasePoint);
}
public void addSalesPoint(SalesPoint salesPoint) {
salesPoints.add(salesPoint);
}
public synchronized void checkGoods(String name) {
Goods goods = goodsList.get(name);
if (goods != null && goods.getAmount() < 20) {
for (PurchasePoint purchasePoint : purchasePoints) {
if (purchasePoint.getGoodsName().equals(name)) {
new Thread(purchasePoint).start();
break;
}
}
}
}
public synchronized void purchaseGoods(String name, int amount) {
Goods goods = goodsList.get(name);
if (goods != null) {
goods.setAmount(goods.getAmount() + amount);
} else {
goodsList.put(name, new Goods(name, amount));
}
}
public synchronized boolean sellGoods(String name, int amount) {
Goods goods = goodsList.get(name);
if (goods != null && goods.getAmount() >= amount) {
goods.setAmount(goods.getAmount() - amount);
return true;
}
return false;
}
public static void main(String[] args) throws InterruptedException {
Warehouse warehouse = new Warehouse();
// 初始化进货点和货物信息
PurchasePoint purchasePoint1 = new PurchasePoint("货物1", 10, warehouse);
PurchasePoint purchasePoint2 = new PurchasePoint("货物2", 15, warehouse);
warehouse.addPurchasePoint(purchasePoint1);
warehouse.addPurchasePoint(purchasePoint2);
// 初始化销售点
SalesPoint salesPoint1 = new SalesPoint("销售点1", warehouse);
SalesPoint salesPoint2 = new SalesPoint("销售点2", warehouse);
warehouse.addSalesPoint(salesPoint1);
warehouse.addSalesPoint(salesPoint2);
// 启动销售点线程
Thread salesThread1 = new Thread(salesPoint1);
Thread salesThread2 = new Thread(salesPoint2);
salesThread1.start();
salesThread2.start();
// 模拟运行10秒后停止运行
Thread.sleep(10000);
salesPoint1.stop();
salesPoint2.stop();
salesThread1.join();
salesThread2.join();
}
}
class PurchasePoint implements Runnable {
private String goodsName;
private int amount;
private Warehouse warehouse;
public PurchasePoint(String goodsName, int amount, Warehouse warehouse) {
this.goodsName = goodsName;
this.amount = amount;
this.warehouse = warehouse;
}
public String getGoodsName() {
return goodsName;
}
@Override
public void run() {
try {
Thread.sleep(1000); // 模拟进货时间
warehouse.purchaseGoods(goodsName, amount);
System.out.println("进货点采购了" + goodsName + ",数量为" + amount);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class SalesPoint implements Runnable {
private String name;
private Warehouse warehouse;
private boolean running;
public SalesPoint(String name, Warehouse warehouse) {
this.name = name;
this.warehouse = warehouse;
this.running = true;
}
public void stop() {
running = false;
}
@Override
public void run() {
Random random = new Random();
while (running) {
int amount = random.nextInt(5) + 1; // 随机销售1-5个商品
String[] goodsNames = {"货物1", "货物2"}; // 可以销售的货物列表
String goodsName = goodsNames[random.nextInt(goodsNames.length)]; // 随机选择一种货物
if (warehouse.sellGoods(goodsName, amount)) {
System.out.println(name + "销售了" + goodsName + ",数量为" + amount);
warehouse.checkGoods(goodsName);
}
try {
Thread.sleep(100); // 模拟销售时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Goods {
private String name;
private int amount;
public Goods(String name, int amount) {
this.name = name;
this.amount = amount;
}
public String getName() {
return name;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
}
```
这个程序中,我们使用了同步控制来保证多线程之间对货物数量的访问是安全的。同时,使用了wait和notify方法来实现进货点和销售点之间的通信。当货物数量少于20时,销售点会调用Warehouse的checkGoods方法来通知对应的进货点开启新线程进行进货。进货点完成进货后,会调用Warehouse的purchaseGoods方法来更新货物数量。
阅读全文