当前位置: 面试刷题>> 订单问题 (经典算法题500道)


**题目描述补充**: 在电商系统中,经常需要处理订单的相关操作,如订单创建、查询、更新和删除等。本题目聚焦于订单查询中的一个具体场景:**根据用户ID和订单状态查询订单列表**。系统需要能够接收用户ID和订单状态作为查询条件,返回符合这些条件的订单列表。订单状态可以是待支付、已支付、已发货、已取消等。 **示例输入**: - 用户ID: 1001 - 订单状态: "已支付" **示例输出**: ```json [ { "orderId": "OD123456", "userId": 1001, "status": "已支付", "totalAmount": 199.99, "orderDate": "2023-04-01T12:00:00Z" }, { "orderId": "OD789012", "userId": 1001, "status": "已支付", "totalAmount": 249.99, "orderDate": "2023-04-02T15:30:00Z" } ] ``` **PHP 示例代码**: ```php "OD123456", "userId" => 1001, "status" => "已支付", "totalAmount" => 199.99, "orderDate" => "2023-04-01T12:00:00Z"], ["orderId" => "OD789012", "userId" => 1001, "status" => "已支付", "totalAmount" => 249.99, "orderDate" => "2023-04-02T15:30:00Z"], ["orderId" => "OD345678", "userId" => 1002, "status" => "待支付", "totalAmount" => 149.99, "orderDate" => "2023-04-03T08:00:00Z"], ]; $filteredOrders = array_filter($orders, function($order) use ($userId, $status) { return $order['userId'] == $userId && $order['status'] == $status; }); return $filteredOrders; } // 示例使用 $userId = 1001; $status = "已支付"; $orders = getOrdersByUserAndStatus($userId, $status); echo json_encode($orders, JSON_PRETTY_PRINT); ?> ``` **Python 示例代码**: ```python def get_orders_by_user_and_status(user_id, status): orders = [ {"orderId": "OD123456", "userId": 1001, "status": "已支付", "totalAmount": 199.99, "orderDate": "2023-04-01T12:00:00Z"}, {"orderId": "OD789012", "userId": 1001, "status": "已支付", "totalAmount": 249.99, "orderDate": "2023-04-02T15:30:00Z"}, {"orderId": "OD345678", "userId": 1002, "status": "待支付", "totalAmount": 149.99, "orderDate": "2023-04-03T08:00:00Z"}, ] filtered_orders = [order for order in orders if order['userId'] == user_id and order['status'] == status] return filtered_orders # 示例使用 user_id = 1001 status = "已支付" orders = get_orders_by_user_and_status(user_id, status) print(json.dumps(orders, indent=4)) ``` **JavaScript 示例代码**: ```javascript function getOrdersByUserAndStatus(userId, status) { const orders = [ { orderId: "OD123456", userId: 1001, status: "已支付", totalAmount: 199.99, orderDate: "2023-04-01T12:00:00Z" }, { orderId: "OD789012", userId: 1001, status: "已支付", totalAmount: 249.99, orderDate: "2023-04-02T15:30:00Z" }, { orderId: "OD345678", userId: 1002, status: "待支付", totalAmount: 149.99, orderDate: "2023-04-03T08:00:00Z" }, ]; const filteredOrders = orders.filter(order => order.userId === userId && order.status === status); return filteredOrders; } // 示例使用 const userId = 1001; const status = "已支付"; const orders = getOrdersByUserAndStatus(userId, status); console.log(JSON.stringify(orders, null, 4)); ``` **码小课提醒**: 在码小课网站中,你可以找到更多关于算法和数据结构、Web开发、后端开发等相关的内容分享,帮助大家更好地学习和提升编程技能。
推荐面试题