用java写一个订单排产系统
时间: 2023-06-24 15:04:47 浏览: 140
订单排产系统是一个复杂的系统,需要考虑到多种因素,如订单数量、生产能力、生产时间、物料库存等等。下面是一个简单的订单排产系统的Java实现:
1. 定义订单类
首先,我们需要定义一个订单类,包含订单编号、订单数量、要求交货时间等属性。
```java
public class Order {
private int orderId;
private int orderQuantity;
private Date deliveryDate;
// 构造函数
public Order(int orderId, int orderQuantity, Date deliveryDate) {
this.orderId = orderId;
this.orderQuantity = orderQuantity;
this.deliveryDate = deliveryDate;
}
// Getter和Setter方法
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getOrderQuantity() {
return orderQuantity;
}
public void setOrderQuantity(int orderQuantity) {
this.orderQuantity = orderQuantity;
}
public Date getDeliveryDate() {
return deliveryDate;
}
public void setDeliveryDate(Date deliveryDate) {
this.deliveryDate = deliveryDate;
}
}
```
2. 定义生产计划类
接下来,我们需要定义一个生产计划类,包含生产计划编号、生产开始时间、生产结束时间、生产数量等属性。
```java
public class ProductionPlan {
private int productionPlanId;
private Date startTime;
private Date endTime;
private int productionQuantity;
// 构造函数
public ProductionPlan(int productionPlanId, Date startTime, Date endTime, int productionQuantity) {
this.productionPlanId = productionPlanId;
this.startTime = startTime;
this.endTime = endTime;
this.productionQuantity = productionQuantity;
}
// Getter和Setter方法
public int getProductionPlanId() {
return productionPlanId;
}
public void setProductionPlanId(int productionPlanId) {
this.productionPlanId = productionPlanId;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public int getProductionQuantity() {
return productionQuantity;
}
public void setProductionQuantity(int productionQuantity) {
this.productionQuantity = productionQuantity;
}
}
```
3. 实现订单排产算法
在实现订单排产算法之前,我们需要先定义一些常量,如生产时间、物料库存、生产能力等。
```java
public class OrderScheduler {
private static final int PRODUCTION_TIME = 8; // 生产时间,单位为小时
private static final int MATERIAL_STOCK = 1000; // 物料库存,单位为件
private static final int PRODUCTION_CAPACITY = 100; // 生产能力,单位为件/小时
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
// 订单排产方法
public static List<ProductionPlan> scheduleOrders(List<Order> orders) {
List<ProductionPlan> productionPlans = new ArrayList<>();
int currentStock = MATERIAL_STOCK;
Date currentDate = new Date();
for (Order order : orders) {
int requiredQuantity = order.getOrderQuantity();
int remainingQuantity = requiredQuantity;
Date deliveryDate = order.getDeliveryDate();
while (remainingQuantity > 0) {
int productionQuantity = Math.min(remainingQuantity, PRODUCTION_CAPACITY * PRODUCTION_TIME);
if (productionQuantity > currentStock) {
// 物料库存不足,需要先生产物料
int materialQuantity = productionQuantity - currentStock;
int materialProductionTime = (int) Math.ceil(materialQuantity * 1.0 / PRODUCTION_CAPACITY);
Date materialEndTime = addHoursToDate(currentDate, materialProductionTime);
ProductionPlan materialPlan = new ProductionPlan(0, currentDate, materialEndTime, materialQuantity);
productionPlans.add(materialPlan);
// 更新当前物料库存和日期
currentStock = 0;
currentDate = materialEndTime;
}
// 生产订单
int orderProductionTime = (int) Math.ceil(productionQuantity * 1.0 / PRODUCTION_CAPACITY);
Date orderStartTime = currentDate;
Date orderEndTime = addHoursToDate(currentDate, orderProductionTime);
ProductionPlan orderPlan = new ProductionPlan(order.getOrderId(), orderStartTime, orderEndTime, productionQuantity);
productionPlans.add(orderPlan);
// 更新当前物料库存和日期
currentStock -= productionQuantity;
currentDate = orderEndTime;
remainingQuantity -= productionQuantity;
}
// 如果交货时间比当前时间早,则需要提前生产
if (deliveryDate.before(currentDate)) {
int earlyProductionTime = (int) Math.ceil((currentDate.getTime() - deliveryDate.getTime()) * 1.0 / (PRODUCTION_TIME * 60 * 60 * 1000));
Date earlyStartTime = addHoursToDate(currentDate, -earlyProductionTime);
ProductionPlan earlyPlan = new ProductionPlan(0, earlyStartTime, currentDate, requiredQuantity);
productionPlans.add(earlyPlan);
}
}
return productionPlans;
}
// 将日期加上指定的小时数
private static Date addHoursToDate(Date date, int hours) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, hours);
return calendar.getTime();
}
}
```
4. 测试订单排产算法
最后,我们需要编写一个测试类来测试订单排产算法。
```java
public class OrderSchedulerTest {
public static void main(String[] args) {
List<Order> orders = new ArrayList<>();
orders.add(new Order(1, 1000, parseDate("2022-01-01")));
orders.add(new Order(2, 500, parseDate("2022-01-02")));
orders.add(new Order(3, 2000, parseDate("2022-01-03")));
List<ProductionPlan> productionPlans = OrderScheduler.scheduleOrders(orders);
for (ProductionPlan plan : productionPlans) {
System.out.println(String.format("生产计划:%d,开始时间:%s,结束时间:%s,生产数量:%d",
plan.getProductionPlanId(), DATE_FORMAT.format(plan.getStartTime()), DATE_FORMAT.format(plan.getEndTime()), plan.getProductionQuantity()));
}
}
private static Date parseDate(String dateString) {
try {
return DATE_FORMAT.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
```
这个测试类会输出每个生产计划的编号、开始时间、结束时间和生产数量。根据这些生产计划,我们可以安排生产计划并确保订单能够按时交货。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)