Files
CosmeticManagerCppHomeWork/main.cpp

126 lines
4.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "CosmeticManager.h"
#include <iostream>
#include <ctime>
#include <string>
void displayMenu() {
std::cout << "\n===== 化妆品管理系统 =====" << std::endl;
std::cout << "当前时间: " << Cosmetic::getTime() << std::endl;
std::cout << "1. 添加化妆品" << std::endl;
std::cout << "2. 删除化妆品" << std::endl;
std::cout << "3. 查询化妆品" << std::endl;
std::cout << "4. 修改化妆品" << std::endl;
std::cout << "5. 显示所有化妆品" << std::endl;
std::cout << "6. 查找临期化妆品" << std::endl;
std::cout << "0. 退出系统" << std::endl;
std::cout << "=========================" << std::endl;
std::cout << "请选择操作: " << std::endl;
}
int main() {
CosmeticManager manager;
manager.loadFromFile(); // 启动时加载数据
int choice;
std::string name;
double price;
bool type;
std::string expirationDate;// 过期日期
while (true) {
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;
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;
}
manager.addCosmetic(Cosmetic(name, price, type, expirationDate));
std::cout << "添加成功!" << std::endl;
break;
}
case 2: {
std::cout << "输入要删除的品牌名称: ";
std::cin >> name;
if (manager.deleteCosmetic(name)) {
std::cout << "删除成功!" << std::endl;
} else {
std::cout << "未找到该品牌!" << std::endl;
}
break;
}
case 3: {
std::cout << "输入要查询的品牌名称: ";
std::cin >> name;
Cosmetic* cosmetic = manager.findCosmetic(name);
if (cosmetic) {
std::cout << "品牌: " << cosmetic->getName() << "\n"
<< "价格: " << cosmetic->getPrice() << "\n"
<< "类型: " << (cosmetic->getType() ? "国产" : "进口") << "\n"
<< "过期日期: " << cosmetic->getExpirationDate() << "\n"
<< std::endl;
} else {
std::cout << "未找到该品牌!" << std::endl;
}
break;
}
case 4: {
std::cout << "输入要修改的品牌名称: ";
std::cin >> name;
std::cout << "输入新价格: ";
std::cin >> price;
std::cout << "输入新类型 (1-国产, 0-进口): ";
std::cin >> 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;
}
break;
}
case 5:
manager.displayAll();
break;
case 6: {
int days;
std::cout << "输入临期天数: ";
std::cin >> days;
manager.displayExpiringCosmetics(days);
break;
}
case 0:
manager.saveToFile();
std::cout << "数据已保存,再见!" << std::endl;
return 0;
default:
std::cout << "无效选项,请重新选择!" << std::endl;
}
}
// ???
return 0;
}