Compare commits
11 Commits
5f13ccfe85
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 236cbb7740 | |||
| e21fae702e | |||
| ba0bdac7f0 | |||
| daee60c469 | |||
| 3e25ea5714 | |||
| c5cd2f28e2 | |||
| cb7d499189 | |||
| a2f2a82a9c | |||
| 1068d32886 | |||
| d67d8a2d83 | |||
| 4465f1f27d |
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -26,6 +26,12 @@ void Cosmetic::setPrice(double price) {
|
||||
void Cosmetic::setType(bool type) {
|
||||
this->type = type;
|
||||
}
|
||||
// 设置过期日期
|
||||
void Cosmetic::setExpirationDate(std::string expirationDate) {
|
||||
this->expirationDate = expirationDate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string Cosmetic::getExpirationDate() const {
|
||||
return expirationDate;
|
||||
|
||||
@@ -18,6 +18,8 @@ public:
|
||||
void setName(std::string name);
|
||||
void setPrice(double price);
|
||||
void setType(bool type);
|
||||
// 设置过期日期
|
||||
void setExpirationDate(std::string expirationDate);
|
||||
// 获取当前时间
|
||||
static std::string getTime();
|
||||
// 获取当前时间+days天
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "Cosmetic.h"
|
||||
#include "windows.h"
|
||||
CosmeticManager::CosmeticManager()
|
||||
: cosmetics(new Cosmetic*[10]), count(0), capacity(10) {}
|
||||
|
||||
@@ -25,12 +26,19 @@ void CosmeticManager::resizeArray() {
|
||||
capacity = newCapacity;
|
||||
}
|
||||
|
||||
void CosmeticManager::addCosmetic(const Cosmetic& cosmetic) {
|
||||
bool CosmeticManager::addCosmetic(const Cosmetic& cosmetic) {
|
||||
// 先检查品牌名是否已存在
|
||||
if (findCosmetic(cosmetic.getName()) != NULL) {
|
||||
std::cout<<"错误:品牌名已存在"<<std::endl;
|
||||
return false; // 品牌名已存在,添加失败
|
||||
}
|
||||
|
||||
if (count >= capacity) {
|
||||
resizeArray();
|
||||
}
|
||||
cosmetics[count++] = new Cosmetic(cosmetic);
|
||||
saveToFile();
|
||||
return true; // 添加成功
|
||||
}
|
||||
|
||||
bool CosmeticManager::deleteCosmetic(const std::string& name) {
|
||||
@@ -58,11 +66,12 @@ Cosmetic* CosmeticManager::findCosmetic(const std::string& name) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool CosmeticManager::modifyCosmetic(const std::string& name, double newPrice, bool newType) {
|
||||
bool CosmeticManager::modifyCosmetic(const std::string &name, double newPrice, bool newType, std::string expirationDate) {
|
||||
Cosmetic* cosmetic = findCosmetic(name);
|
||||
if (cosmetic) {
|
||||
cosmetic->setPrice(newPrice);
|
||||
cosmetic->setType(newType);
|
||||
cosmetic->setExpirationDate(expirationDate);
|
||||
saveToFile();
|
||||
return true;
|
||||
}
|
||||
@@ -98,23 +107,33 @@ void CosmeticManager::displayAll() const {
|
||||
for (int i = 0; i < count; ++i) {
|
||||
std::cout << "品牌: " << cosmetics[i]->getName()
|
||||
<< " | 价格: " << cosmetics[i]->getPrice()
|
||||
<< " | 类型: " << (cosmetics[i]->getType() ? "国产" : "进口")
|
||||
<< " | 类型: " << (cosmetics[i]->getType() ? "国产" : "进口")// 注意,这里还为开放其他类型,若需2种以上要新构建
|
||||
<< " | 过期日期: " << cosmetics[i]->getExpirationDate() // 新增
|
||||
<< std::endl;
|
||||
launchWindow("化妆品信息", "品牌: " + cosmetics[i]->getName()
|
||||
+ " | 价格: " + std::to_string(cosmetics[i]->getPrice())
|
||||
+ " | 类型: " + (cosmetics[i]->getType() ? "国产" : "进口")// 注意,这里还为开放其他类型,若需2种以上要新构建
|
||||
+ " | 过期日期: " + cosmetics[i]->getExpirationDate());
|
||||
}
|
||||
}
|
||||
// 查找临期化妆品,小于60天
|
||||
void CosmeticManager::displayExpiringCosmetics() const {
|
||||
void CosmeticManager::displayExpiringCosmetics(int days=60) const {
|
||||
std::string date = Cosmetic::getTime();
|
||||
for (int i = 0; i < count; ++i) {
|
||||
//这里可以调整判定临期的时间(天)
|
||||
if (cosmetics[i]->getExpirationDate() < Cosmetic::getTimeAdd(60)) {
|
||||
if (cosmetics[i]->getExpirationDate() < Cosmetic::getTimeAdd(days)) {
|
||||
std::cout << "品牌: " << cosmetics[i]->getName()
|
||||
<< " | 过期日期: " << cosmetics[i]->getExpirationDate()
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 窗口显示
|
||||
void CosmeticManager::launchWindow(std::string title, std::string info) const {
|
||||
MessageBoxA(NULL, info.c_str(), title.c_str(), MB_OK);
|
||||
}
|
||||
|
||||
// 基于YYYY-MM-DD格式的日期合法性检查
|
||||
bool CosmeticManager::isValidDate(const std::string& date) {
|
||||
if (date.length() != 10 || date[4] != '-' || date[7] != '-') {
|
||||
@@ -144,4 +163,3 @@ bool CosmeticManager::isValidDate(const std::string& date) {
|
||||
|
||||
return day <= maxDay;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,15 +10,17 @@ public:
|
||||
CosmeticManager();
|
||||
~CosmeticManager();
|
||||
|
||||
void addCosmetic(const Cosmetic& cosmetic);
|
||||
bool addCosmetic(const Cosmetic& cosmetic);
|
||||
bool deleteCosmetic(const std::string& name);
|
||||
Cosmetic* findCosmetic(const std::string& name);
|
||||
bool modifyCosmetic(const std::string& name, double newPrice, bool newType);
|
||||
bool modifyCosmetic(const std::string &name, double newPrice, bool newType, std::string string);
|
||||
void loadFromFile();
|
||||
void saveToFile();
|
||||
void displayAll() const;
|
||||
void displayExpiringCosmetics() const; // 查找临期化妆品方法
|
||||
void displayExpiringCosmetics(int i) const; // 查找临期化妆品方法
|
||||
bool isValidDate(const std::string &date);
|
||||
//窗口显示
|
||||
void launchWindow(std::string title,std::string info) const;
|
||||
|
||||
private:
|
||||
Cosmetic** cosmetics; // 动态指针数组
|
||||
|
||||
136
main.cpp
136
main.cpp
@@ -1,7 +1,7 @@
|
||||
#include "CosmeticManager.h"
|
||||
#include <iostream>
|
||||
#include <ctime>
|
||||
#include <string>
|
||||
#include <limits>
|
||||
|
||||
void displayMenu() {
|
||||
std::cout << "\n===== 化妆品管理系统 =====" << std::endl;
|
||||
@@ -14,48 +14,92 @@ void displayMenu() {
|
||||
std::cout << "6. 查找临期化妆品" << std::endl;
|
||||
std::cout << "0. 退出系统" << std::endl;
|
||||
std::cout << "=========================" << std::endl;
|
||||
std::cout << "请选择操作: ";
|
||||
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;
|
||||
}
|
||||
|
||||
// 获取并验证类型输入
|
||||
bool getAndValidateType(int& type, CosmeticManager& manager) {
|
||||
std::cout << "输入类型 (1-国产, 0-进口): ";
|
||||
std::cin >> type;
|
||||
if (type != 1 && type != 0) {
|
||||
std::cout << "类型输入错误!请输入1-国产, 0-进口" << std::endl;
|
||||
manager.launchWindow("类型输入错误","请输入1-国产, 0-进口");
|
||||
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;
|
||||
int type;
|
||||
std::string expirationDate;// 过期日期
|
||||
std::string msgboxTitle, msgboxContent;// 消息框标题和内容
|
||||
displayMenu();
|
||||
std::cin >> choice;
|
||||
|
||||
switch (choice) {
|
||||
case 1: {
|
||||
std::cout << "输入品牌名称: ";
|
||||
std::cin >> name;
|
||||
std::cout << "输入价格: ";
|
||||
std::cin >> price;
|
||||
std::cout << "输入类型 (1-国产, 0-进口): ";
|
||||
std::cin >> type;
|
||||
std::cout << "输入过期日期 (YYYY-MM-DD): ";
|
||||
std::cin >> expirationDate;
|
||||
if (!manager.isValidDate(expirationDate)) {
|
||||
std::cout << "日期格式有误!格式YYYY-MM-DD 例2000-01-01" << std::endl;
|
||||
if (!getAndValidateType(type, manager)) {
|
||||
break;
|
||||
}
|
||||
manager.addCosmetic(Cosmetic(name, price, type, expirationDate));
|
||||
std::cout << "添加成功!" << std::endl;
|
||||
if (!getAndValidateDate(manager, expirationDate)) {
|
||||
break;
|
||||
}
|
||||
// 检查品牌名是否已存在
|
||||
if (manager.findCosmetic(name) != nullptr) {
|
||||
msgboxTitle="添加失败";
|
||||
msgboxContent="品牌名已存在";
|
||||
std::cout << msgboxContent << std::endl;
|
||||
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||
break;
|
||||
}
|
||||
if (manager.addCosmetic(Cosmetic(name, price, type, expirationDate))) {
|
||||
msgboxTitle="添加成功";
|
||||
msgboxContent="品牌添加成功";
|
||||
std::cout << msgboxContent << std::endl;
|
||||
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||
} else {
|
||||
msgboxTitle="添加失败";
|
||||
msgboxContent="品牌已存在或其他错误";
|
||||
std::cout << msgboxContent << std::endl;
|
||||
manager.launchWindow(msgboxTitle,msgboxContent);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
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;
|
||||
}
|
||||
@@ -66,12 +110,24 @@ int main() {
|
||||
if (cosmetic) {
|
||||
std::cout << "品牌: " << cosmetic->getName() << "\n"
|
||||
<< "价格: " << cosmetic->getPrice() << "\n"
|
||||
<< "类型: " << (cosmetic->getType() ? "国产" : "进口") << "\n"
|
||||
<< "类型: " << (cosmetic->getType() ? "国产" : "进口") << "\n"// 注意,这里还为开放其他类型,若需2种以上要新构
|
||||
<< "过期日期: " << cosmetic->getExpirationDate() << "\n"
|
||||
<< std::endl;
|
||||
msgboxTitle="查询成功";
|
||||
msgboxContent=cosmetic->getName() + "\n"
|
||||
+ "价格: " + std::to_string(cosmetic->getPrice()) + "\n"
|
||||
+ "类型: " + (cosmetic->getType() ? "国产" : "进口") + "\n"// 注意,这里还为开放其他类型,若需2种以上要新构建
|
||||
+ "过期日期: " + 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<std::streamsize>::max(), '\n');
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
@@ -79,27 +135,47 @@ int main() {
|
||||
std::cin >> name;
|
||||
std::cout << "输入新价格: ";
|
||||
std::cin >> price;
|
||||
std::cout << "输入新类型 (1-国产, 0-进口): ";
|
||||
std::cin >> type;
|
||||
if (manager.modifyCosmetic(name, price, type)) {
|
||||
std::cout << "修改成功!" << std::endl;
|
||||
if (!getAndValidateType(type, manager)) {
|
||||
break;
|
||||
}
|
||||
if (!getAndValidateDate(manager, expirationDate)) {
|
||||
break;
|
||||
}
|
||||
if (manager.modifyCosmetic(name, price, type, expirationDate)) {
|
||||
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;
|
||||
}
|
||||
case 5:
|
||||
manager.displayAll();
|
||||
break;
|
||||
case 6:
|
||||
manager.displayExpiringCosmetics();
|
||||
case 6: {
|
||||
int days;
|
||||
std::cout << "输入临期天数: ";
|
||||
std::cin >> days;
|
||||
manager.displayExpiringCosmetics(days);
|
||||
break;
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
// ???
|
||||
|
||||
Reference in New Issue
Block a user