149 lines
4.3 KiB
C++
149 lines
4.3 KiB
C++
#include "CosmeticManager.h"
|
||
#include <fstream>
|
||
#include <iostream>
|
||
#include "Cosmetic.h"
|
||
CosmeticManager::CosmeticManager()
|
||
: cosmetics(new Cosmetic*[10]), count(0), capacity(10) {}
|
||
|
||
CosmeticManager::~CosmeticManager() {
|
||
for (int i = 0; i < count; ++i) {
|
||
delete cosmetics[i];
|
||
}
|
||
delete[] cosmetics;
|
||
}
|
||
|
||
void CosmeticManager::resizeArray() {
|
||
int newCapacity = capacity * 2;
|
||
Cosmetic** newArray = new Cosmetic*[newCapacity];
|
||
|
||
for (int i = 0; i < count; ++i) {
|
||
newArray[i] = cosmetics[i];
|
||
}
|
||
|
||
delete[] cosmetics;
|
||
cosmetics = newArray;
|
||
capacity = newCapacity;
|
||
}
|
||
|
||
void CosmeticManager::addCosmetic(const Cosmetic& cosmetic) {
|
||
if (count >= capacity) {
|
||
resizeArray();
|
||
}
|
||
cosmetics[count++] = new Cosmetic(cosmetic);
|
||
saveToFile();
|
||
}
|
||
|
||
bool CosmeticManager::deleteCosmetic(const std::string& name) {
|
||
for (int i = 0; i < count; ++i) {
|
||
if (cosmetics[i]->getName() == name) {
|
||
delete cosmetics[i];
|
||
// 移动后续元素
|
||
for (int j = i; j < count - 1; ++j) {
|
||
cosmetics[j] = cosmetics[j + 1];
|
||
}
|
||
count--;
|
||
saveToFile();
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
Cosmetic* CosmeticManager::findCosmetic(const std::string& name) {
|
||
for (int i = 0; i < count; ++i) {
|
||
if (cosmetics[i]->getName() == name) {
|
||
return cosmetics[i];
|
||
}
|
||
}
|
||
return nullptr;
|
||
}
|
||
|
||
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;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
void CosmeticManager::loadFromFile() {
|
||
std::ifstream file("Cosmetic.txt");
|
||
if (!file.is_open()) return;
|
||
|
||
std::string name, expirationDate;
|
||
double price;
|
||
bool type;
|
||
|
||
while (file >> name >> price >> type >> expirationDate) {
|
||
addCosmetic(Cosmetic(name, price, type, expirationDate));
|
||
}
|
||
file.close();
|
||
}
|
||
|
||
void CosmeticManager::saveToFile() {
|
||
std::ofstream file("Cosmetic.txt");
|
||
for (int i = 0; i < count; ++i) {
|
||
file << cosmetics[i]->getName() << " "
|
||
<< cosmetics[i]->getPrice() << " "
|
||
<< cosmetics[i]->getType() << " "
|
||
<< cosmetics[i]->getExpirationDate() << "\n"; // 新增过期日期
|
||
}
|
||
file.close();
|
||
}
|
||
|
||
void CosmeticManager::displayAll() const {
|
||
for (int i = 0; i < count; ++i) {
|
||
std::cout << "品牌: " << cosmetics[i]->getName()
|
||
<< " | 价格: " << cosmetics[i]->getPrice()
|
||
<< " | 类型: " << (cosmetics[i]->getType() ? "国产" : "进口")
|
||
<< " | 过期日期: " << cosmetics[i]->getExpirationDate() // 新增
|
||
<< std::endl;
|
||
}
|
||
}
|
||
// 查找临期化妆品,小于60天
|
||
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(days)) {
|
||
std::cout << "品牌: " << cosmetics[i]->getName()
|
||
<< " | 过期日期: " << cosmetics[i]->getExpirationDate()
|
||
<< std::endl;
|
||
}
|
||
}
|
||
}
|
||
// 基于YYYY-MM-DD格式的日期合法性检查
|
||
bool CosmeticManager::isValidDate(const std::string& date) {
|
||
if (date.length() != 10 || date[4] != '-' || date[7] != '-') {
|
||
return false;
|
||
}
|
||
for (int i = 0; i < 10; ++i) {
|
||
if (i == 4 || i == 7) continue;
|
||
if (!isdigit(date[i])) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
int year, month, day;
|
||
if (sscanf(date.c_str(), "%d-%d-%d", &year, &month, &day) != 3) {
|
||
return false;
|
||
}
|
||
|
||
// 基本范围检查
|
||
if (year < 1 || month < 1 || month > 12 || day < 1) {
|
||
return false;
|
||
}
|
||
|
||
// 每月天数检查(包含闰年处理)
|
||
static const int daysInMonth[] = {31, 28 + (month == 2 && (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))),
|
||
31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
|
||
int maxDay = daysInMonth[month - 1];
|
||
|
||
return day <= maxDay;
|
||
}
|
||
|