你可以用c++帮我写一个简单的快件管理系统吗,要求进入系统后首先选择取件人模式或管理员模式,内置管理员的账号密码,取件人可以凭借手机号或者快递单号取快递,管理员可以进行快件的录入,删除,读取,修改,查询已取快件,查询未取快件,一个快件应包含的信息有:随机生成的快递单号,寄件人,寄件人手机号,寄件人住址,收件人,收件人手机号,收件人住址
时间: 2024-03-12 13:48:43 浏览: 70
好的,我可以帮您写一个简单的快件管理系统。以下是一个简单的C++代码示例,其中包含了管理员和取件人两种模式,以及快递的录入、删除、读取、修改、查询已取快件和查询未取快件等功能。
```c++
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
// 快递的结构体
struct Package {
string package_id; // 快递单号
string sender_name; // 寄件人
string sender_phone; // 寄件人手机号
string sender_address; // 寄件人住址
string receiver_name; // 收件人
string receiver_phone; // 收件人手机号
string receiver_address;// 收件人住址
bool taken; // 是否已取件
};
// 管理员账号和密码
const string ADMIN_USERNAME = "admin";
const string ADMIN_PASSWORD = "123456";
// 快递列表
vector<Package> package_list;
// 生成随机的快递单号
string generate_package_id() {
srand(time(NULL)); // 设置随机数种子
string package_id = "KD" + to_string(rand() % 10000);
return package_id;
}
// 快递录入
void add_package() {
Package package;
package.package_id = generate_package_id();
cout << "请输入寄件人姓名:";
cin >> package.sender_name;
cout << "请输入寄件人手机号:";
cin >> package.sender_phone;
cout << "请输入寄件人住址:";
cin >> package.sender_address;
cout << "请输入收件人姓名:";
cin >> package.receiver_name;
cout << "请输入收件人手机号:";
cin >> package.receiver_phone;
cout << "请输入收件人住址:";
cin >> package.receiver_address;
package.taken = false;
package_list.push_back(package);
cout << "快递录入成功,快递单号为:" << package.package_id << endl;
}
// 快递删除
void delete_package() {
string package_id;
cout << "请输入要删除的快递单号:";
cin >> package_id;
bool found = false;
for (int i = 0; i < package_list.size(); i++) {
if (package_list[i].package_id == package_id) {
package_list.erase(package_list.begin() + i);
cout << "快递删除成功!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到快递单号为 " << package_id << " 的快递!" << endl;
}
}
// 快递读取
void read_package() {
string package_id;
cout << "请输入要读取的快递单号:";
cin >> package_id;
bool found = false;
for (Package package : package_list) {
if (package.package_id == package_id) {
cout << "快递单号:" << package.package_id << endl;
cout << "寄件人姓名:" << package.sender_name << endl;
cout << "寄件人手机号:" << package.sender_phone << endl;
cout << "寄件人住址:" << package.sender_address << endl;
cout << "收件人姓名:" << package.receiver_name << endl;
cout << "收件人手机号:" << package.receiver_phone << endl;
cout << "收件人住址:" << package.receiver_address << endl;
cout << "是否已取件:" << (package.taken ? "是" : "否") << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到快递单号为 " << package_id << " 的快递!" << endl;
}
}
// 快递修改
void modify_package() {
string package_id;
cout << "请输入要修改的快递单号:";
cin >> package_id;
bool found = false;
for (Package &package : package_list) {
if (package.package_id == package_id) {
cout << "请输入新的寄件人姓名:";
cin >> package.sender_name;
cout << "请输入新的寄件人手机号:";
cin >> package.sender_phone;
cout << "请输入新的寄件人住址:";
cin >> package.sender_address;
cout << "请输入新的收件人姓名:";
cin >> package.receiver_name;
cout << "请输入新的收件人手机号:";
cin >> package.receiver_phone;
cout << "请输入新的收件人住址:";
cin >> package.receiver_address;
cout << "快递信息修改成功!" << endl;
found = true;
break;
}
}
if (!found) {
cout << "未找到快递单号为 " << package_id << " 的快递!" << endl;
}
}
// 查询已取快件
void query_taken_packages() {
int count = 0;
for (Package package : package_list) {
if (package.taken) {
cout << "快递单号:" << package.package_id << endl;
cout << "寄件人姓名:" << package.sender_name << endl;
cout << "寄件人手机号:" << package.sender_phone << endl;
cout << "寄件人住址:" << package.sender_address << endl;
cout << "收件人姓名:" << package.receiver_name << endl;
cout << "收件人手机号:" << package.receiver_phone << endl;
cout << "收件人住址:" << package.receiver_address << endl;
cout << "是否已取件:" << (package.taken ? "是" : "否") << endl;
count++;
}
}
if (count == 0) {
cout << "没有已取快件!" << endl;
}
}
// 查询未取快件
void query_untaken_packages() {
int count = 0;
for (Package package : package_list) {
if (!package.taken) {
cout << "快递单号:" << package.package_id << endl;
cout << "寄件人姓名:" << package.sender_name << endl;
cout << "寄件人手机号:" << package.sender_phone << endl;
cout << "寄件人住址:" << package.sender_address << endl;
cout << "收件人姓名:" << package.receiver_name << endl;
cout << "收件人手机号:" << package.receiver_phone << endl;
cout << "收件人住址:" << package.receiver_address << endl;
cout << "是否已取件:" << (package.taken ? "是" : "否") << endl;
count++;
}
}
if (count == 0) {
cout << "没有未取快件!" << endl;
}
}
int main() {
string username, password;
cout << "欢迎使用快件管理系统!" << endl;
cout << "请选择您的身份:" << endl;
cout << "1. 取件人" << endl;
cout << "2. 管理员" << endl;
cout << "请选择:";
int mode;
cin >> mode;
if (mode == 1) {
bool found = false;
string phone;
cout << "请输入您的手机号:";
cin >> phone;
for (Package &package : package_list) {
if (package.receiver_phone == phone && !package.taken) {
cout << "您的快递已准备好,请前往取件!" << endl;
package.taken = true;
found = true;
break;
}
}
if (!found) {
cout << "未找到您的快递,请检查手机号是否正确或快递是否已取件!" << endl;
}
} else if (mode == 2) {
cout << "请输入管理员账号和密码:" << endl;
cout << "账号:";
cin >> username;
cout << "密码:";
cin >> password;
if (username == ADMIN_USERNAME && password == ADMIN_PASSWORD) {
cout << "管理员登录成功!" << endl;
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 录入快递" << endl;
cout << "2. 删除快递" << endl;
cout << "3. 读取快递" << endl;
cout << "4. 修改快递" << endl;
cout << "5. 查询已取快件" << endl;
cout << "6. 查询未取快件" << endl;
cout << "7. 退出" << endl;
cout << "请选择:";
int choice;
cin >> choice;
switch (choice) {
case 1:
add_package();
break;
case 2:
delete_package();
break;
case 3:
read_package();
break;
case 4:
modify_package();
break;
case 5:
query_taken_packages();
break;
case 6:
query_untaken_packages();
break;
case 7:
cout << "谢谢使用快件管理系统!" << endl;
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
}
}
} else {
cout << "管理员账号或密码错误!" << endl;
}
} else {
cout << "输入有误,请重新运行程序!" << endl;
}
return 0;
}
```
注意:以上代码只是一个简单的示例,可能存在一些不足之处,您可以根据自己的需求进行修改和完善。
阅读全文