新增msgbox弹窗功能
简化main.cpp代码
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Cosmetic.h"
|
#include "Cosmetic.h"
|
||||||
|
#include "windows.h"
|
||||||
CosmeticManager::CosmeticManager()
|
CosmeticManager::CosmeticManager()
|
||||||
: cosmetics(new Cosmetic*[10]), count(0), capacity(10) {}
|
: cosmetics(new Cosmetic*[10]), count(0), capacity(10) {}
|
||||||
|
|
||||||
@@ -27,7 +28,7 @@ void CosmeticManager::resizeArray() {
|
|||||||
|
|
||||||
bool CosmeticManager::addCosmetic(const Cosmetic& cosmetic) {
|
bool CosmeticManager::addCosmetic(const Cosmetic& cosmetic) {
|
||||||
// 先检查品牌名是否已存在
|
// 先检查品牌名是否已存在
|
||||||
if (findCosmetic(cosmetic.getName()) != nullptr) {
|
if (findCosmetic(cosmetic.getName()) != NULL) {
|
||||||
std::cout<<"错误:品牌名已存在"<<std::endl;
|
std::cout<<"错误:品牌名已存在"<<std::endl;
|
||||||
return false; // 品牌名已存在,添加失败
|
return false; // 品牌名已存在,添加失败
|
||||||
}
|
}
|
||||||
@@ -123,6 +124,12 @@ void CosmeticManager::displayExpiringCosmetics(int days=60) const {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 窗口显示
|
||||||
|
void CosmeticManager::launchWindow(std::string title, std::string info) const {
|
||||||
|
MessageBoxA(NULL, info.c_str(), title.c_str(), MB_OK);
|
||||||
|
}
|
||||||
|
|
||||||
// 基于YYYY-MM-DD格式的日期合法性检查
|
// 基于YYYY-MM-DD格式的日期合法性检查
|
||||||
bool CosmeticManager::isValidDate(const std::string& date) {
|
bool CosmeticManager::isValidDate(const std::string& date) {
|
||||||
if (date.length() != 10 || date[4] != '-' || date[7] != '-') {
|
if (date.length() != 10 || date[4] != '-' || date[7] != '-') {
|
||||||
@@ -152,5 +159,3 @@ bool CosmeticManager::isValidDate(const std::string& date) {
|
|||||||
|
|
||||||
return day <= maxDay;
|
return day <= maxDay;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ public:
|
|||||||
void displayAll() const;
|
void displayAll() const;
|
||||||
void displayExpiringCosmetics(int i) const; // 查找临期化妆品方法
|
void displayExpiringCosmetics(int i) const; // 查找临期化妆品方法
|
||||||
bool isValidDate(const std::string &date);
|
bool isValidDate(const std::string &date);
|
||||||
|
//窗口显示
|
||||||
|
void launchWindow(std::string title,std::string info) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Cosmetic** cosmetics; // 动态指针数组
|
Cosmetic** cosmetics; // 动态指针数组
|
||||||
|
|||||||
106
main.cpp
106
main.cpp
@@ -1,7 +1,7 @@
|
|||||||
#include "CosmeticManager.h"
|
#include "CosmeticManager.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ctime>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
void displayMenu() {
|
void displayMenu() {
|
||||||
std::cout << "\n===== 化妆品管理系统 =====" << std::endl;
|
std::cout << "\n===== 化妆品管理系统 =====" << std::endl;
|
||||||
@@ -17,20 +17,31 @@ void displayMenu() {
|
|||||||
std::cout << "请选择操作: " << std::endl;
|
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() {
|
int main() {
|
||||||
CosmeticManager manager;
|
CosmeticManager manager;
|
||||||
manager.loadFromFile(); // 启动时加载数据
|
manager.loadFromFile(); // 启动时加载数据
|
||||||
|
|
||||||
|
while (true) {
|
||||||
int choice;
|
int choice;
|
||||||
std::string name;
|
std::string name;
|
||||||
double price;
|
double price;
|
||||||
bool type;
|
bool type;
|
||||||
std::string expirationDate;// 过期日期
|
std::string expirationDate;// 过期日期
|
||||||
|
std::string msgboxTitle, msgboxContent;// 消息框标题和内容
|
||||||
while (true) {
|
|
||||||
displayMenu();
|
displayMenu();
|
||||||
std::cin >> choice;
|
std::cin >> choice;
|
||||||
|
|
||||||
switch (choice) {
|
switch (choice) {
|
||||||
case 1: {
|
case 1: {
|
||||||
std::cout << "输入品牌名称: ";
|
std::cout << "输入品牌名称: ";
|
||||||
@@ -40,20 +51,33 @@ int main() {
|
|||||||
std::cout << "输入类型 (1-国产, 0-进口): ";
|
std::cout << "输入类型 (1-国产, 0-进口): ";
|
||||||
std::cin >> type;
|
std::cin >> type;
|
||||||
if (type!=1 && type!=0) {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
std::cout << "输入过期日期 (YYYY-MM-DD): ";
|
if (!getAndValidateDate(manager, expirationDate)) {
|
||||||
std::cin >> expirationDate;
|
break;
|
||||||
if (!manager.isValidDate(expirationDate)) {
|
}
|
||||||
std::cout << "日期格式有误!格式YYYY-MM-DD 例2000-01-01" << std::endl;
|
// 检查品牌名是否已存在
|
||||||
|
if (manager.findCosmetic(name) != nullptr) {
|
||||||
|
msgboxTitle="添加失败";
|
||||||
|
msgboxContent="品牌名已存在";
|
||||||
|
std::cout << msgboxContent << std::endl;
|
||||||
|
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
manager.addCosmetic(Cosmetic(name, price, type, expirationDate));
|
|
||||||
if (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 {
|
} else {
|
||||||
std::cout << "添加失败!原因请检查日志。" << std::endl;
|
msgboxTitle="添加失败";
|
||||||
|
msgboxContent="品牌已存在或其他错误";
|
||||||
|
std::cout << msgboxContent << std::endl;
|
||||||
|
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -61,9 +85,15 @@ int main() {
|
|||||||
std::cout << "输入要删除的品牌名称: ";
|
std::cout << "输入要删除的品牌名称: ";
|
||||||
std::cin >> name;
|
std::cin >> name;
|
||||||
if (manager.deleteCosmetic(name)) {
|
if (manager.deleteCosmetic(name)) {
|
||||||
std::cout << "删除成功!" << std::endl;
|
msgboxTitle="删除成功";
|
||||||
|
msgboxContent="品牌删除成功";
|
||||||
|
std::cout << msgboxContent << std::endl;
|
||||||
|
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||||
} else {
|
} else {
|
||||||
std::cout << "未找到该品牌!" << std::endl;
|
msgboxTitle="删除失败";
|
||||||
|
msgboxContent="未找到该品牌";
|
||||||
|
std::cout << msgboxContent << std::endl;
|
||||||
|
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -77,9 +107,25 @@ int main() {
|
|||||||
<< "类型: " << (cosmetic->getType() ? "国产" : "进口") << "\n"
|
<< "类型: " << (cosmetic->getType() ? "国产" : "进口") << "\n"
|
||||||
<< "过期日期: " << cosmetic->getExpirationDate() << "\n"
|
<< "过期日期: " << cosmetic->getExpirationDate() << "\n"
|
||||||
<< std::endl;
|
<< 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 {
|
} else {
|
||||||
std::cout << "未找到该品牌!" << std::endl;
|
msgboxTitle="查询失败";
|
||||||
|
msgboxContent="未找到该品牌";
|
||||||
|
std::cout << msgboxContent << std::endl;
|
||||||
|
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||||
}
|
}
|
||||||
|
// 清除输入缓冲区
|
||||||
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 4: {
|
case 4: {
|
||||||
@@ -90,19 +136,25 @@ int main() {
|
|||||||
std::cout << "输入新类型 (1-国产, 0-进口): ";
|
std::cout << "输入新类型 (1-国产, 0-进口): ";
|
||||||
std::cin >> type;
|
std::cin >> type;
|
||||||
if (type!=1 && type!=0) {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
std::cout << "输入新过期日期 (YYYY-MM-DD): ";
|
if (!getAndValidateDate(manager, expirationDate)) {
|
||||||
std::cin >> expirationDate;
|
|
||||||
if (!manager.isValidDate(expirationDate)) {
|
|
||||||
std::cout << "日期格式有误!格式YYYY-MM-DD 例2000-01-01" << std::endl;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (manager.modifyCosmetic(name, price, type, expirationDate)) {
|
if (manager.modifyCosmetic(name, price, type, expirationDate)) {
|
||||||
std::cout << "修改成功!" << std::endl;
|
msgboxTitle="修改成功";
|
||||||
|
msgboxContent="品牌修改成功";
|
||||||
|
std::cout << msgboxContent << std::endl;
|
||||||
|
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||||
} else {
|
} else {
|
||||||
std::cout << "未找到该品牌!" << std::endl;
|
msgboxTitle="修改失败";
|
||||||
|
msgboxContent="未找到该品牌";
|
||||||
|
std::cout << msgboxContent << std::endl;
|
||||||
|
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -118,10 +170,16 @@ int main() {
|
|||||||
}
|
}
|
||||||
case 0:
|
case 0:
|
||||||
manager.saveToFile();
|
manager.saveToFile();
|
||||||
std::cout << "数据已保存,再见!" << std::endl;
|
msgboxTitle="数据已保存";
|
||||||
|
msgboxContent="数据已保存,再见!";
|
||||||
|
std::cout << msgboxContent << std::endl;
|
||||||
|
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||||
return 0;
|
return 0;
|
||||||
default:
|
default:
|
||||||
std::cout << "无效选项,请重新选择!" << std::endl;
|
msgboxTitle="无效选项";
|
||||||
|
msgboxContent="请重新选择!";
|
||||||
|
std::cout << msgboxContent << std::endl;
|
||||||
|
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ???
|
// ???
|
||||||
|
|||||||
Reference in New Issue
Block a user