![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2c00a726648a7bc1804cb9c9e1358578.png)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class Device {
public:
string deviceNumber;
string deviceName;
string user;
string department;
int quantity;
string purchaseDate;
double price;
Device() {}
Device(string number, string name, string u, string dept, int qty, string date, double p)
: deviceNumber(number), deviceName(name), user(u), department(dept), quantity(qty), purchaseDate(date), price(p) {}
void display() {
cout << "设备号: " << deviceNumber << endl;
cout << "设备名称: " << deviceName << endl;
cout << "领用人: " << user << endl;
cout << "所属部门: " << department << endl;
cout << "数量: " << quantity << endl;
cout << "购买时间: " << purchaseDate << endl;
cout << "价格: " << price << endl;
}
double getPrice() {
return price;
}
string getDepartment() {
return department;
}
};
class DeviceManagementSystem
{
vector<Device> devices;
public:
void addDevice(Device device) {
devices.push_back(device);
}
void displayDevices() {
for (Device device : devices) {
device.display();
cout << endl;
}
}
void modifyDevice(string number, string newName, string newUser, string newDept, int newQty, string newDate, double newPrice) {
for (Device& device : devices) {
if (device.deviceNumber == number) {
device.deviceName = newName;
device.user = newUser;
device.department = newDept;
device.quantity = newQty;
device.purchaseDate = newDate;
device.price = newPrice;
break;
}
}
}
void deleteDevice(string number) {
for (auto iter = devices.begin(); iter != devices.end(); ++iter) {
if (iter->deviceNumber == number) {
devices.erase(iter);
break;
}
}
}
double calculateTotalValue() {
double totalValue = 0.0;
for (Device device : devices) {
totalValue += device.getPrice() * device.quantity;
}
return totalValue;
}
void groupByDeviceName() {
}
void groupByDepartment() {
}
void saveToFile(string filename) {
ofstream outfile(filename);
if (outfile.is_open()) {
for (Device device : devices) {
outfile << device.deviceNumber << "," << device.deviceName << "," << device.user << ","
<< device.department << "," << device.quantity << "," << device.purchaseDate << ","
<< device.price << endl;
}
outfile.close();
}
else {
cout << "无法打开文件" << filename << "进行保存" << endl;
}
}
void loadFromFile(string filename) {
ifstream file(filename);
if (file.is_open()) {
devices.clear();
string line;
while (getline(file, line)) {
Device device;
size_t pos = 0;
string token;
while ((pos = line.find(',')) != string::npos) {
token = line.substr(0, pos);
line.erase(0, pos + 1);
if (device.deviceNumber.empty()) {
device.deviceNumber = token;
}
else if (device.deviceName.empty()) {
device.deviceName = token;
}
else if (device.user.empty()) {
device.user = token;
}
else if (device.department.empty()) {
device.department = token;
}
else if (device.quantity == 0) {
device.quantity = stoi(token);
}
else if (device.purchaseDate.empty()) {
device.purchaseDate = token;
}
else if (device.price == 0.0) {
device.price = stod(token);
}
}
devices.push_back(device);
}
file.close();
cout << "设备信息已从文件加载。" << endl;
}
else {
cout << "无法打开文件。" << endl;
}
}
};
int main() {
DeviceManagementSystem dms;
dms.loadFromFile("devices.txt");
int choice;
do {
cout << "人机对话界面:" << endl;
cout << "请选择操作:" << endl;
cout << "1. 添加设备信息" << endl;
cout << "2. 显示设备信息" << endl;
cout << "3. 修改设备信息" << endl;
cout << "4. 删除设备信息" << endl;
cout << "5. 计算设备总金额" << endl;
cout << "6. 按照设备种类进行分类" << endl;
cout << "7. 按照设备所属部门进行分类" << endl;
cout << "0. 退出系统" << endl;
cout << "请输入选项:";
cin >> choice;
switch (choice) {
case 1: {
string number, name, user, dept, date;
int quantity;
double price;
cout << "请输入设备号:";
cin >> number;
cout << "请输入设备名称:";
cin >> name;
cout << "请输入领用人:";
cin >> user;
cout << "请输入所属部门:";
cin >> dept;
cout << "请输入数量:";
cin >> quantity;
cout << "请输入购买时间:";
cin >> date;
cout << "请输入价格:";
cin >> price;
dms.addDevice(Device(number, name, user, dept, quantity, date, price));
cout << "设备信息已添加" << endl;
break;
}
case 2:
dms.displayDevices();
break;
case 3: {
string number, newName, newUser, newDept, newDate;
int newQty;
double newPrice;
cout << "请输入要修改的设备号:";
cin >> number;
cout << "请输入新的设备名称:";
cin >> newName;
cout << "请输入新的领用人:";
cin >> newUser;
cout << "请输入新的所属部门:";
cin >> newDept;
cout << "请输入新的数量:";
cin >> newQty;
cout << "请输入新的购买时间:";
cin >> newDate;
cout << "请输入新的价格:";
cin >> newPrice;
dms.modifyDevice(number, newName, newUser, newDept, newQty, newDate, newPrice);
cout << "设备信息已修改" << endl;
break;
}
case 4: {
string number;
cout << "请输入要删除的设备号:";
cin >> number;
dms.deleteDevice(number);
cout << "设备信息已删除" << endl;
break;
}
case 5:
cout << "设备总金额为:" << dms.calculateTotalValue() << endl;
break;
case 6:
dms.groupByDeviceName();
break;
case 7:
dms.groupByDepartment();
break;
case 0:
dms.saveToFile("devices.txt");
cout << "谢谢使用,再见!" << endl;
break;
default:
cout << "无效选项,请重新输入" << endl;
break;
}
cout << endl;
} while (choice != 0);
return 0;
}