你可以用c++帮我写一个简单的快件管理系统吗,要求进入系统后首先选择取件人模式或管理员模式,运行结束时返回模式选择界面。内置管理员的账号密码,取件人可以凭借手机号或者快递单号取快件,管理员可以进行快件的录入,删除,读取,修改,查询已取快件,查询未取快件。一个快件应包含的信息有:随机生成的快递单号,寄件人姓名,寄件人手机号,寄件人住址,收件人姓名,收件人手机号,收件人住址。
时间: 2024-03-14 20:42:41 浏览: 141
好的,这是一个比较完整的系统,需要设计多个类和函数来实现。我会给你提供一个简单的实现方案,供你参考。
首先,我们需要设计一个快件类,包含快件的所有信息:
```c++
class Package {
public:
string packageID; // 快递单号
string senderName; // 寄件人姓名
string senderPhone; // 寄件人手机号
string senderAddress; // 寄件人住址
string receiverName; // 收件人姓名
string receiverPhone; // 收件人手机号
string receiverAddress; // 收件人住址
bool isDelivered; // 是否已取件
};
```
然后,我们需要设计一个快递管理系统类,包含管理员账号密码和所有快件的信息:
```c++
class System {
public:
System(); // 构造函数,初始化管理员账号密码和快件列表
void run(); // 运行系统,包括模式选择界面和各个模式的处理
private:
string adminName; // 管理员账号
string adminPassword; // 管理员密码
vector<Package> packageList; // 快件列表
void userMode(); // 取件人模式
void adminMode(); // 管理员模式
void addPackage(); // 添加快件
void deletePackage(); // 删除快件
void modifyPackage(); // 修改快件
void displayDelivered(); // 查询已取快件
void displayUndelivered(); // 查询未取快件
};
```
在构造函数中,我们可以设置管理员账号密码和初始化快件列表:
```c++
System::System() {
adminName = "admin";
adminPassword = "admin123";
packageList = vector<Package>();
}
```
在运行系统的方法中,我们可以先输出模式选择界面:
```c++
void System::run() {
while (true) {
cout << "请选择模式:" << endl;
cout << "1. 取件人模式" << endl;
cout << "2. 管理员模式" << endl;
cout << "3. 退出系统" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
userMode();
break;
case 2:
adminMode();
break;
case 3:
return;
default:
cout << "输入有误,请重新选择!" << endl;
}
}
}
```
在取件人模式中,我们需要让用户输入手机号或快递单号来查询快件信息:
```c++
void System::userMode() {
cout << "请输入您的手机号或快递单号:" << endl;
string input;
cin >> input;
bool found = false;
for (int i = 0; i < packageList.size(); i++) {
if (input == packageList[i].senderPhone || input == packageList[i].packageID) {
found = true;
Package p = packageList[i];
cout << "快递单号:" << p.packageID << endl;
cout << "寄件人姓名:" << p.senderName << endl;
cout << "寄件人手机号:" << p.senderPhone << endl;
cout << "寄件人住址:" << p.senderAddress << endl;
cout << "收件人姓名:" << p.receiverName << endl;
cout << "收件人手机号:" << p.receiverPhone << endl;
cout << "收件人住址:" << p.receiverAddress << endl;
if (p.isDelivered) {
cout << "该快件已被取走。" << endl;
} else {
cout << "该快件未被取走。" << endl;
}
break;
}
}
if (!found) {
cout << "查无此件。" << endl;
}
}
```
在管理员模式中,我们需要让管理员输入账号密码来进行操作:
```c++
void System::adminMode() {
cout << "请输入管理员账号:" << endl;
string name;
cin >> name;
cout << "请输入管理员密码:" << endl;
string password;
cin >> password;
if (name == adminName && password == adminPassword) {
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加快件" << endl;
cout << "2. 删除快件" << endl;
cout << "3. 修改快件" << endl;
cout << "4. 查询已取快件" << endl;
cout << "5. 查询未取快件" << endl;
cout << "6. 返回模式选择界面" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
addPackage();
break;
case 2:
deletePackage();
break;
case 3:
modifyPackage();
break;
case 4:
displayDelivered();
break;
case 5:
displayUndelivered();
break;
case 6:
return;
default:
cout << "输入有误,请重新选择!" << endl;
}
}
} else {
cout << "账号或密码错误。" << endl;
}
}
```
在添加快件的方法中,我们需要让管理员输入所有快件信息并生成一个随机的快递单号:
```c++
void System::addPackage() {
Package p;
cout << "请输入寄件人姓名:" << endl;
cin >> p.senderName;
cout << "请输入寄件人手机号:" << endl;
cin >> p.senderPhone;
cout << "请输入寄件人住址:" << endl;
cin >> p.senderAddress;
cout << "请输入收件人姓名:" << endl;
cin >> p.receiverName;
cout << "请输入收件人手机号:" << endl;
cin >> p.receiverPhone;
cout << "请输入收件人住址:" << endl;
cin >> p.receiverAddress;
p.isDelivered = false;
srand(time(NULL));
p.packageID = to_string(rand());
packageList.push_back(p);
cout << "快件添加成功,快递单号为:" << p.packageID << endl;
}
```
在删除快件的方法中,我们需要让管理员输入快递单号来删除快件:
```c++
void System::deletePackage() {
cout << "请输入要删除的快递单号:" << endl;
string packageID;
cin >> packageID;
bool found = false;
for (int i = 0; i < packageList.size(); i++) {
if (packageID == packageList[i].packageID) {
found = true;
packageList.erase(packageList.begin() + i);
cout << "快件删除成功。" << endl;
break;
}
}
if (!found) {
cout << "快递单号不存在。" << endl;
}
}
```
在修改快件的方法中,我们需要让管理员输入快递单号来修改快件信息:
```c++
void System::modifyPackage() {
cout << "请输入要修改的快递单号:" << endl;
string packageID;
cin >> packageID;
bool found = false;
for (int i = 0; i < packageList.size(); i++) {
if (packageID == packageList[i].packageID) {
found = true;
Package &p = packageList[i];
cout << "请输入新的寄件人姓名(原值为" << p.senderName << "):" << endl;
cin >> p.senderName;
cout << "请输入新的寄件人手机号(原值为" << p.senderPhone << "):" << endl;
cin >> p.senderPhone;
cout << "请输入新的寄件人住址(原值为" << p.senderAddress << "):" << endl;
cin >> p.senderAddress;
cout << "请输入新的收件人姓名(原值为" << p.receiverName << "):" << endl;
cin >> p.receiverName;
cout << "请输入新的收件人手机号(原值为" << p.receiverPhone << "):" << endl;
cin >> p.receiverPhone;
cout << "请输入新的收件人住址(原值为" << p.receiverAddress << "):" << endl;
cin >> p.receiverAddress;
cout << "快件修改成功。" << endl;
break;
}
}
if (!found) {
cout << "快递单号不存在。" << endl;
}
}
```
在查询已取快件和未取快件的方法中,我们只需要遍历快件列表并输出符合条件的快件信息即可:
```c++
void System::displayDelivered() {
cout << "已取快件信息如下:" << endl;
for (int i = 0; i < packageList.size(); i++) {
if (packageList[i].isDelivered) {
cout << "快递单号:" << packageList[i].packageID << endl;
cout << "寄件人姓名:" << packageList[i].senderName << endl;
cout << "寄件人手机号:" << packageList[i].senderPhone << endl;
cout << "寄件人住址:" << packageList[i].senderAddress << endl;
cout << "收件人姓名:" << packageList[i].receiverName << endl;
cout << "收件人手机号:" << packageList[i].receiverPhone << endl;
cout << "收件人住址:" << packageList[i].receiverAddress << endl;
cout << "---------------------------" << endl;
}
}
}
void System::displayUndelivered() {
cout << "未取快件信息如下:" << endl;
for (int i = 0; i < packageList.size(); i++) {
if (!packageList[i].isDelivered) {
cout << "快递单号:" << packageList[i].packageID << endl;
cout << "寄件人姓名:" << packageList[i].senderName << endl;
cout << "寄件人手机号:" << packageList[i].senderPhone << endl;
cout << "寄件人住址:" << packageList[i].senderAddress << endl;
cout << "收件人姓名:" << packageList[i].receiverName << endl;
cout << "收件人手机号:" << packageList[i].receiverPhone << endl;
cout << "收件人住址:" << packageList[i].receiverAddress << endl;
cout << "---------------------------" << endl;
}
}
}
```
这样,我们就实现了一个简单的快件管理系统。当然,这只是一个示例,实际使用时还需要根据具体需求进行修改和完善。
阅读全文