shangcheng/include/product.hpp

65 lines
1.6 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.

#ifndef MALL_PRODUCT_HPP
#define MALL_PRODUCT_HPP
#include <string>
#include <cstdint>
/**
* @brief 商品类,表示商城中的一件商品。
*/
class Product {
public:
/**
* @brief 默认构造函数,构造一个空商品。
*/
Product() = default;
/**
* @brief 构造商品。
* @param id 商品唯一编号
* @param name 商品名称
* @param price 商品单价(元)
* @param stock 库存数量
*/
Product(uint64_t id, std::string name, double price, int stock);
/** @brief 获取商品编号 */
uint64_t getId() const noexcept { return id_; }
/** @brief 获取商品名称 */
const std::string& getName() const noexcept { return name_; }
/** @brief 获取商品单价 */
double getPrice() const noexcept { return price_; }
/** @brief 获取库存数量 */
int getStock() const noexcept { return stock_; }
/**
* @brief 减少库存。
* @param quantity 减少数量
* @return true 扣减成功false 库存不足
*/
bool reduceStock(int quantity);
/**
* @brief 增加库存。
* @param quantity 增加数量
*/
void addStock(int quantity);
/**
* @brief 返回商品简要信息字符串。
* @return 例如 "[#1001] 苹果 ¥5.50 (库存:20)"
*/
std::string toString() const;
private:
uint64_t id_ = 0; ///< 商品编号
std::string name_; ///< 商品名称
double price_ = 0.0; ///< 单价
int stock_ = 0; ///< 库存数量
};
#endif // MALL_PRODUCT_HPP