修复添加化妆品无法更改过期时间的bug
This commit is contained in:
@@ -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天
|
||||
|
||||
@@ -58,11 +58,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;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public:
|
||||
void 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;
|
||||
|
||||
12
main.cpp
12
main.cpp
@@ -85,7 +85,17 @@ int main() {
|
||||
std::cin >> price;
|
||||
std::cout << "输入新类型 (1-国产, 0-进口): ";
|
||||
std::cin >> type;
|
||||
if (manager.modifyCosmetic(name, price, type)) {
|
||||
if (type!=1 && type!=0) {
|
||||
std::cout << "类型输入错误!请输入1-国产, 0-进口" << std::endl;
|
||||
break;
|
||||
}
|
||||
std::cout << "输入新过期日期 (YYYY-MM-DD): ";
|
||||
std::cin >> expirationDate;
|
||||
if (!manager.isValidDate(expirationDate)) {
|
||||
std::cout << "日期格式有误!格式YYYY-MM-DD 例2000-01-01" << std::endl;
|
||||
break;
|
||||
}
|
||||
if (manager.modifyCosmetic(name, price, type, expirationDate)) {
|
||||
std::cout << "修改成功!" << std::endl;
|
||||
} else {
|
||||
std::cout << "未找到该品牌!" << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user