网约车平台项目C++实现
时间: 2024-05-04 11:21:10 浏览: 32
网约车平台项目可以分为多个模块,包括用户端、司机端、管理后台、订单管理等。下面是一个简单的C++实现示例,供您参考:
```
#include <iostream>
#include <string>
#include <vector>
// 用户信息
struct User {
int id;
std::string name;
std::string phone;
std::vector<int> orders; // 用户下的订单列表
};
// 司机信息
struct Driver {
int id;
std::string name;
std::string phone;
std::vector<int> orders; // 司机接的订单列表
};
// 订单信息
struct Order {
int id;
int userId;
int driverId;
std::string start;
std::string end;
double distance;
double fee;
};
// 网约车平台类
class CarPlatform {
public:
// 用户相关操作
User getUserById(int id);
User getUserByPhone(std::string phone);
void addUser(User user);
// 司机相关操作
Driver getDriverById(int id);
Driver getDriverByPhone(std::string phone);
void addDriver(Driver driver);
// 订单相关操作
Order getOrderById(int id);
void addOrder(Order order);
void assignOrder(Order order, int driverId);
void finishOrder(Order order);
private:
std::vector<User> users_;
std::vector<Driver> drivers_;
std::vector<Order> orders_;
};
// 实现网约车平台类中的方法
User CarPlatform::getUserById(int id) {
for (const auto& user : users_) {
if (user.id == id) {
return user;
}
}
return User{-1, "", "", {}};
}
User CarPlatform::getUserByPhone(std::string phone) {
for (const auto& user : users_) {
if (user.phone == phone) {
return user;
}
}
return User{-1, "", "", {}};
}
void CarPlatform::addUser(User user) {
users_.push_back(std::move(user));
}
Driver CarPlatform::getDriverById(int id) {
for (const auto& driver : drivers_) {
if (driver.id == id) {
return driver;
}
}
return Driver{-1, "", "", {}};
}
Driver CarPlatform::getDriverByPhone(std::string phone) {
for (const auto& driver : drivers_) {
if (driver.phone == phone) {
return driver;
}
}
return Driver{-1, "", "", {}};
}
void CarPlatform::addDriver(Driver driver) {
drivers_.push_back(std::move(driver));
}
Order CarPlatform::getOrderById(int id) {
for (const auto& order : orders_) {
if (order.id == id) {
return order;
}
}
return Order{-1, -1, -1, "", "", 0.0, 0.0};
}
void CarPlatform::addOrder(Order order) {
orders_.push_back(std::move(order));
}
void CarPlatform::assignOrder(Order order, int driverId) {
for (auto& driver : drivers_) {
if (driver.id == driverId) {
driver.orders.push_back(order.id);
}
}
for (auto& user : users_) {
if (user.id == order.userId) {
user.orders.push_back(order.id);
}
}
for (auto& o : orders_) {
if (o.id == order.id) {
o.driverId = driverId;
break;
}
}
}
void CarPlatform::finishOrder(Order order) {
for (auto& driver : drivers_) {
if (driver.id == order.driverId) {
auto it = std::find(driver.orders.begin(), driver.orders.end(), order.id);
if (it != driver.orders.end()) {
driver.orders.erase(it);
break;
}
}
}
for (auto& user : users_) {
if (user.id == order.userId) {
auto it = std::find(user.orders.begin(), user.orders.end(), order.id);
if (it != user.orders.end()) {
user.orders.erase(it);
break;
}
}
}
for (auto& o : orders_) {
if (o.id == order.id) {
o.fee = order.distance * 2.0; // 简单计费规则,每公里收费2元
break;
}
}
}
// 测试
int main() {
CarPlatform platform;
// 添加用户
User user1{1, "Alice", "12345678901", {}};
platform.addUser(user1);
User user2{2, "Bob", "12345678902", {}};
platform.addUser(user2);
// 添加司机
Driver driver1{1, "Charlie", "12345678903", {}};
platform.addDriver(driver1);
Driver driver2{2, "David", "12345678904", {}};
platform.addDriver(driver2);
// 添加订单
Order order1{1, 1, -1, "A", "B", 10.0, 0.0};
platform.addOrder(order1);
Order order2{2, 2, -1, "B", "C", 20.0, 0.0};
platform.addOrder(order2);
// 分配订单给司机
platform.assignOrder(order1, 1);
platform.assignOrder(order2, 2);
// 完成订单
platform.finishOrder(order1);
platform.finishOrder(order2);
return 0;
}
```
注意:这只是一个简单的示例,实际项目中还需要考虑数据存储、并发控制、异常处理等问题。
相关推荐
![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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)