快递物流系统(c++版)
·
一、物流系统描述
1、项目背景
随着电子商务的快速发展,快递业务量呈现爆发式增长。根据国家邮政局数据,2023年中国快递业务量已突破1200亿件,日均处理量超过3.5亿件。然而,传统的“最后一公里”配送模式面临以下问题:
(1)效率低下:快递员需逐个上门投递,居民不在家时需多次配送,耗时耗力。
(2)安全隐患:快递随意堆放易导致丢失、错拿或隐私泄露。
(3)管理混乱:老旧小区缺乏统一管理,快递堆积影响社区环境。
(4)资源浪费:重复配送增加碳排放,包装垃圾污染环境。
智能小区物流管理系统应运而生,旨在通过物联网(IoT)、人工智能(AI)和自动化技术,优化末端配送流程,提升居民生活便利性。
2、系统描述
系统核心功能包括智能快递柜的自动化存取,快递员可通过扫描包裹存入系统,系统自动分配柜门并生成唯一取件码;居民可通过取件码自助取件,24小时随时领取。系统实时监控快递柜状态,提供满柜预警和滞留包裹提醒,同时记录所有存取数据确保可追溯。物业端可查看数据分析报表,优化资源分配;
二、代码部分
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <limits>
#include <stdexcept>
using namespace std;
class Package {
private:
string id_;
string code_;
time_t arriveTime_;
int cabinetId_;
bool isTaken_;
public:
Package(const string& id, const string& code, int cabinetId) {
id_ = id;
code_ = code;
cabinetId_ = cabinetId;
arriveTime_ = time(0);
isTaken_ = false;
}
string getId() const { return id_; }
string getCode() const { return code_; }
int getCabinetId() const { return cabinetId_; }
bool isTaken() const { return isTaken_; }
void markAsTaken() { isTaken_ = true; }
void display() const {
cout << "快递单号: " << id_
<< " | 柜号: " << cabinetId_
<< " | 取件码: " << code_
<< " | 状态: " << (isTaken_ ? "已取件" : "待取件") << endl;
}
};
class SmartCabinetSystem {
private:
vector<Package> packages_;
int totalSlots_;
const string dataFile_;
string generateSecureCode() {
string code;
const string charSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i = 0; i < 8; ++i) {
code += charSet[rand() % charSet.size()];
}
return code;
}
bool isValidCabinet(int id) const {
return id > 0 && id <= totalSlots_;
}
public:
SmartCabinetSystem(int slots) : totalSlots_(slots), dataFile_("cabinet_data.txt") {
srand(time(0));
loadData();
}
~SmartCabinetSystem() {
saveData();
}
void storePackage(const string& packageId) {
if (packages_.size() >= totalSlots_) {
throw runtime_error("快递柜已满,无法存入!");
}
int cabinetId = findAvailableCabinet();
if (!isValidCabinet(cabinetId)) {
throw runtime_error("无可用柜门!");
}
string code = generateSecureCode();
packages_.push_back(Package(packageId, code, cabinetId)); // 改用push_back
cout << "存入成功!柜号: " << cabinetId << " 取件码: " << code << endl;
}
int findAvailableCabinet() const {
vector<bool> occupied(totalSlots_ + 1, false);
for (vector<Package>::const_iterator it = packages_.begin(); it != packages_.end(); ++it) {
if (!it->isTaken()) {
occupied[it->getCabinetId()] = true;
}
}
for (int i = 1; i <= totalSlots_; ++i) {
if (!occupied[i]) return i;
}
return -1;
}
void takePackage(const string& code) {
for (vector<Package>::iterator it = packages_.begin(); it != packages_.end(); ++it) {
if (!it->isTaken() && it->getCode() == code) {
it->markAsTaken();
cout << "取件成功!快递单号: " << it->getId() << endl;
return;
}
}
throw invalid_argument("无效取件码或快递已取走!");
}
void displayAllPackages() const {
cout << "\n====== 快递列表 ======" << endl;
int used = 0;
for (vector<Package>::const_iterator it = packages_.begin(); it != packages_.end(); ++it) {
if (!it->isTaken()) ++used;
}
cout << "总容量: " << totalSlots_ << " | 已用: " << used << endl;
for (vector<Package>::const_iterator it = packages_.begin(); it != packages_.end(); ++it) {
it->display();
}
}
void saveData() const {
ofstream out(dataFile_.c_str());
if (!out) {
cerr << "警告:无法保存数据到文件!" << endl;
return;
}
for (vector<Package>::const_iterator it = packages_.begin(); it != packages_.end(); ++it) {
out << it->getId() << ' ' << it->getCode() << ' '
<< it->getCabinetId() << ' ' << it->isTaken() << '\n';
}
}
void loadData() {
ifstream in(dataFile_.c_str());
if (!in) return;
packages_.clear();
string id, code;
int cabinetId;
bool taken;
while (in >> id >> code >> cabinetId >> taken) {
Package pkg(id, code, cabinetId);
if (taken) pkg.markAsTaken();
packages_.push_back(pkg);
}
}
};
void userInterface(SmartCabinetSystem& system) {
while (true) {
cout << "\n===== 智能快递柜系统 =====" << endl;
cout << "1. 存入快递\n2. 取出快递\n3. 查看状态\n4. 退出\n请选择: ";
int choice;
while (!(cin >> choice) || choice < 1 || choice > 4) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入,请重新选择 (1-4): ";
}
try {
switch (choice) {
case 1: {
cout << "输入快递单号: ";
string packageId;
cin >> packageId;
system.storePackage(packageId);
break;
}
case 2: {
cout << "输入取件码: ";
string code;
cin >> code;
system.takePackage(code);
break;
}
case 3:
system.displayAllPackages();
break;
case 4:
return;
}
} catch (const exception& e) {
cerr << "操作失败: " << e.what() << endl;
}
}
}
int main() {
SmartCabinetSystem system(12);
userInterface(system);
return 0;
}
三、运行结果
1、系统展示
2、快递员存入

3、用户取出

4、物流查看快递 
更多推荐


所有评论(0)