From 3e25ea5714d4be422313cc4125ff9044128b4964 Mon Sep 17 00:00:00 2001 From: MrJiaGe <3060669380@qq.com> Date: Mon, 29 Sep 2025 14:44:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Emsgbox=E5=BC=B9=E7=AA=97?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=20=E7=AE=80=E5=8C=96main.cpp=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CosmeticManager.cpp | 11 +++-- CosmeticManager.h | 2 + main.cpp | 114 +++++++++++++++++++++++++++++++++----------- 3 files changed, 96 insertions(+), 31 deletions(-) diff --git a/CosmeticManager.cpp b/CosmeticManager.cpp index 99fd57c..43b37ac 100644 --- a/CosmeticManager.cpp +++ b/CosmeticManager.cpp @@ -2,6 +2,7 @@ #include #include #include "Cosmetic.h" +#include "windows.h" CosmeticManager::CosmeticManager() : cosmetics(new Cosmetic*[10]), count(0), capacity(10) {} @@ -27,7 +28,7 @@ void CosmeticManager::resizeArray() { bool CosmeticManager::addCosmetic(const Cosmetic& cosmetic) { // 先检查品牌名是否已存在 - if (findCosmetic(cosmetic.getName()) != nullptr) { + if (findCosmetic(cosmetic.getName()) != NULL) { std::cout<<"错误:品牌名已存在"< -#include #include +#include void displayMenu() { std::cout << "\n===== 化妆品管理系统 =====" << std::endl; @@ -17,20 +17,31 @@ void displayMenu() { std::cout << "请选择操作: " << std::endl; } +// 获取并验证日期输入 +bool getAndValidateDate(CosmeticManager& manager, std::string& expirationDate) { + std::cout << "输入过期日期 (YYYY-MM-DD): "; + std::cin >> expirationDate; + if (!manager.isValidDate(expirationDate)) { + std::cout << "日期格式有误!格式YYYY-MM-DD 例2000-01-01" << std::endl; + manager.launchWindow("日期格式有误","格式YYYY-MM-DD 例2000-01-01"); + return false; + } + return true; +} + int main() { CosmeticManager manager; manager.loadFromFile(); // 启动时加载数据 - int choice; - std::string name; - double price; - bool type; - std::string expirationDate;// 过期日期 - while (true) { + int choice; + std::string name; + double price; + bool type; + std::string expirationDate;// 过期日期 + std::string msgboxTitle, msgboxContent;// 消息框标题和内容 displayMenu(); std::cin >> choice; - switch (choice) { case 1: { std::cout << "输入品牌名称: "; @@ -40,20 +51,33 @@ int main() { std::cout << "输入类型 (1-国产, 0-进口): "; std::cin >> type; if (type!=1 && type!=0) { - std::cout << "类型输入错误!请输入1-国产, 0-进口" << std::endl; + msgboxTitle="添加失败"; + msgboxContent="类型输入错误!请输入1-国产, 0-进口"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); break; } - std::cout << "输入过期日期 (YYYY-MM-DD): "; - std::cin >> expirationDate; - if (!manager.isValidDate(expirationDate)) { - std::cout << "日期格式有误!格式YYYY-MM-DD 例2000-01-01" << std::endl; + if (!getAndValidateDate(manager, expirationDate)) { + break; + } + // 检查品牌名是否已存在 + if (manager.findCosmetic(name) != nullptr) { + msgboxTitle="添加失败"; + msgboxContent="品牌名已存在"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); break; } - manager.addCosmetic(Cosmetic(name, price, type, expirationDate)); if (manager.addCosmetic(Cosmetic(name, price, type, expirationDate))) { - std::cout << "添加成功!" << std::endl; + msgboxTitle="添加成功"; + msgboxContent="品牌添加成功"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); } else { - std::cout << "添加失败!原因请检查日志。" << std::endl; + msgboxTitle="添加失败"; + msgboxContent="品牌已存在或其他错误"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); } break; } @@ -61,9 +85,15 @@ int main() { std::cout << "输入要删除的品牌名称: "; std::cin >> name; if (manager.deleteCosmetic(name)) { - std::cout << "删除成功!" << std::endl; + msgboxTitle="删除成功"; + msgboxContent="品牌删除成功"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); } else { - std::cout << "未找到该品牌!" << std::endl; + msgboxTitle="删除失败"; + msgboxContent="未找到该品牌"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); } break; } @@ -77,9 +107,25 @@ int main() { << "类型: " << (cosmetic->getType() ? "国产" : "进口") << "\n" << "过期日期: " << cosmetic->getExpirationDate() << "\n" << std::endl; + manager.launchWindow("查询成功",cosmetic->getName() + "\n" + + "价格: " + std::to_string(cosmetic->getPrice()) + "\n" + + "类型: " + (cosmetic->getType() ? "国产" : "进口") + "\n" + + "过期日期: " + cosmetic->getExpirationDate()); + msgboxTitle="查询成功"; + msgboxContent=cosmetic->getName() + "\n" + + "价格: " + std::to_string(cosmetic->getPrice()) + "\n" + + "类型: " + (cosmetic->getType() ? "国产" : "进口") + "\n" + + "过期日期: " + cosmetic->getExpirationDate(); + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); } else { - std::cout << "未找到该品牌!" << std::endl; + msgboxTitle="查询失败"; + msgboxContent="未找到该品牌"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); } + // 清除输入缓冲区 + std::cin.ignore(std::numeric_limits::max(), '\n'); break; } case 4: { @@ -90,19 +136,25 @@ int main() { std::cout << "输入新类型 (1-国产, 0-进口): "; std::cin >> type; if (type!=1 && type!=0) { - std::cout << "类型输入错误!请输入1-国产, 0-进口" << std::endl; + msgboxTitle="类型输入错误"; + msgboxContent="请输入1-国产, 0-进口"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); break; } - std::cout << "输入新过期日期 (YYYY-MM-DD): "; - std::cin >> expirationDate; - if (!manager.isValidDate(expirationDate)) { - std::cout << "日期格式有误!格式YYYY-MM-DD 例2000-01-01" << std::endl; + if (!getAndValidateDate(manager, expirationDate)) { break; } if (manager.modifyCosmetic(name, price, type, expirationDate)) { - std::cout << "修改成功!" << std::endl; + msgboxTitle="修改成功"; + msgboxContent="品牌修改成功"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); } else { - std::cout << "未找到该品牌!" << std::endl; + msgboxTitle="修改失败"; + msgboxContent="未找到该品牌"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); } break; } @@ -118,10 +170,16 @@ int main() { } case 0: manager.saveToFile(); - std::cout << "数据已保存,再见!" << std::endl; + msgboxTitle="数据已保存"; + msgboxContent="数据已保存,再见!"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); return 0; default: - std::cout << "无效选项,请重新选择!" << std::endl; + msgboxTitle="无效选项"; + msgboxContent="请重新选择!"; + std::cout << msgboxContent << std::endl; + manager.launchWindow(msgboxTitle,msgboxContent); } } // ???