用ideal写一个不使用数据库而是使用对象和集合功能存储数据的面向对象的快递服务控制台程序,可实现用户寄件,取件,查询订单,修改订单,取消订单等功能
时间: 2023-12-11 12:02:32 浏览: 84
物流配送(快递)管理系统框架:html + jsp + ssh + mysql开发工具:idea
好的,我会给您提供一个简单的示例代码,实现上述功能:
```java
import java.util.ArrayList;
import java.util.Scanner;
//定义快递订单类
class Order {
private String orderId;
private String senderName;
private String senderAddress;
private String receiverName;
private String receiverAddress;
private boolean isDelivered;
public Order(String orderId, String senderName, String senderAddress, String receiverName, String receiverAddress) {
this.orderId = orderId;
this.senderName = senderName;
this.senderAddress = senderAddress;
this.receiverName = receiverName;
this.receiverAddress = receiverAddress;
this.isDelivered = false;
}
public String getOrderId() {
return orderId;
}
public String getSenderName() {
return senderName;
}
public String getSenderAddress() {
return senderAddress;
}
public String getReceiverName() {
return receiverName;
}
public String getReceiverAddress() {
return receiverAddress;
}
public boolean isDelivered() {
return isDelivered;
}
public void setDelivered(boolean delivered) {
isDelivered = delivered;
}
}
//定义快递服务类
class DeliveryService {
private ArrayList<Order> orders;
public DeliveryService() {
orders = new ArrayList<Order>();
}
//添加订单
public void addOrder(Order order) {
orders.add(order);
System.out.println("订单添加成功!");
}
//根据订单号查询订单
public Order getOrderById(String orderId) {
for (Order order : orders) {
if (order.getOrderId().equals(orderId)) {
return order;
}
}
return null;
}
//修改订单状态
public void updateOrderStatus(String orderId, boolean isDelivered) {
Order order = getOrderById(orderId);
if (order != null) {
order.setDelivered(isDelivered);
System.out.println("订单状态更新成功!");
} else {
System.out.println("订单不存在!");
}
}
//取消订单
public void cancelOrder(String orderId) {
Order order = getOrderById(orderId);
if (order != null) {
orders.remove(order);
System.out.println("订单已取消!");
} else {
System.out.println("订单不存在!");
}
}
}
public class Main {
public static void main(String[] args) {
DeliveryService deliveryService = new DeliveryService();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请选择功能:");
System.out.println("1. 寄件");
System.out.println("2. 取件");
System.out.println("3. 查询订单");
System.out.println("4. 修改订单");
System.out.println("5. 取消订单");
System.out.println("0. 退出");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入寄件人姓名:");
String senderName = scanner.next();
System.out.println("请输入寄件人地址:");
String senderAddress = scanner.next();
System.out.println("请输入收件人姓名:");
String receiverName = scanner.next();
System.out.println("请输入收件人地址:");
String receiverAddress = scanner.next();
Order order = new Order(String.valueOf(System.currentTimeMillis()), senderName, senderAddress, receiverName, receiverAddress);
deliveryService.addOrder(order);
break;
case 2:
System.out.println("请输入订单号:");
String orderId1 = scanner.next();
Order order1 = deliveryService.getOrderById(orderId1);
if (order1 != null) {
order1.setDelivered(true);
System.out.println("取件成功!");
} else {
System.out.println("订单不存在!");
}
break;
case 3:
System.out.println("请输入订单号:");
String orderId2 = scanner.next();
Order order2 = deliveryService.getOrderById(orderId2);
if (order2 != null) {
System.out.println("订单信息:");
System.out.println("订单号:" + order2.getOrderId());
System.out.println("寄件人:" + order2.getSenderName());
System.out.println("寄件地址:" + order2.getSenderAddress());
System.out.println("收件人:" + order2.getReceiverName());
System.out.println("收件地址:" + order2.getReceiverAddress());
System.out.println("订单状态:" + (order2.isDelivered() ? "已取件" : "未取件"));
} else {
System.out.println("订单不存在!");
}
break;
case 4:
System.out.println("请输入订单号:");
String orderId3 = scanner.next();
System.out.println("请输入订单状态(true表示已取件,false表示未取件):");
boolean isDelivered = scanner.nextBoolean();
deliveryService.updateOrderStatus(orderId3, isDelivered);
break;
case 5:
System.out.println("请输入订单号:");
String orderId4 = scanner.next();
deliveryService.cancelOrder(orderId4);
break;
case 0:
System.exit(0);
}
}
}
}
```
在这个示例代码中,我们使用了对象和集合来存储订单数据。我们定义了一个Order类表示一个快递订单,其中包含了订单号、寄件人姓名、寄件人地址、收件人姓名、收件人地址和订单状态。我们还定义了一个DeliveryService类表示快递服务,其中包含了添加订单、查询订单、修改订单状态和取消订单等功能。最后,在主函数中,我们使用一个while循环来接收用户输入并根据用户选择调用不同的功能。
阅读全文