shangcheng/src/product.cpp

29 lines
734 B
C++

#include "product.hpp"
#include <sstream>
#include <iomanip>
Product::Product(uint64_t id, std::string name, double price, int stock)
: id_(id), name_(std::move(name)), price_(price), stock_(stock) {}
bool Product::reduceStock(int quantity) {
if (quantity <= 0 || quantity > stock_) {
return false;
}
stock_ -= quantity;
return true;
}
void Product::addStock(int quantity) {
if (quantity > 0) {
stock_ += quantity;
}
}
std::string Product::toString() const {
std::ostringstream oss;
oss << "[#" << id_ << "] " << name_
<< " \xEF\xBF\xA5" << std::fixed << std::setprecision(2) << price_
<< " (\xe5\xba\x93\xe5\xad\x98:" << stock_ << ")";
return oss.str();
}