Python 代码实现智慧超市管理系统

下面是一个简单的智慧超市管理系统的代码实现,分为商品管理模块、购物车管理模块和订单管理模块。

class Product:
    def __init__(self, name, price, quantity):
        self.name = name
        self.price = price
        self.quantity = quantity

class ShoppingCart:
    def __init__(self):
        self.items = []

    def add_item(self, product, quantity):
        for item in self.items:
            if item.product.name == product.name:
                item.quantity += quantity
                return
        self.items.append({'product': product, 'quantity': quantity})

    def remove_item(self, product_name, quantity):
        for item in self.items:
            if item['product'].name == product_name:
                if item['quantity'] <= quantity:
                    self.items.remove(item)
                else:
                    item['quantity'] -= quantity
                return

    def checkout(self):
        total_price = 0
        for item in self.items:
            total_price += item['product'].price * item['quantity']
        return total_price

class Order:
    def __init__(self, items, total_price):
        self.items = items
        self.total_price = total_price

class Supermarket:
    def __init__(self):
        self.products = []

    def add_product(self, product):
        self.products.append(product)

    def display_products(self):
        for product in self.products:
            print(f"{product.name}: ${product.price} ({product.quantity} available)")

# 创建超市对象
supermarket = Supermarket()

# 添加商品
product1 = Product("Apple", 1.5, 100)
product2 = Product("Banana", 0.8, 200)
supermarket.add_product(product1)
supermarket.add_product(product2)

# 创建购物车
cart = ShoppingCart()

# 添加商品到购物车
cart.add_item(product1, 5)
cart.add_item(product2, 3)

# 结账
total_price = cart.checkout()
print("Total price:", total_price)

以上代码实现了一个简单的智慧超市管理系统,包括商品管理、购物车管理和订单管理功能。每个模块都被封装成了一个类,使得代码结构清晰,易于维护和扩展。

C++ 代码实现智慧超市管理系统

以下是一个简单的 C++ 实现智慧超市管理系统的示例代码,分为商品管理模块、购物车管理模块和订单管理模块

#include <iostream>
#include <vector>
#include <string>

// 商品类
class Product {
public:
    std::string name;
    double price;
    int quantity;

    Product(std::string n, double p, int q) : name(n), price(p), quantity(q) {}
};

// 购物车类
class ShoppingCart {
private:
    std::vector<std::pair<Product, int>> items;

public:
    void add_item(Product product, int quantity) {
        for (auto& item : items) {
            if (item.first.name == product.name) {
                item.second += quantity;
                return;
            }
        }
        items.push_back(std::make_pair(product, quantity));
    }

    void remove_item(std::string product_name, int quantity) {
        for (auto it = items.begin(); it != items.end(); ++it) {
            if (it->first.name == product_name) {
                if (it->second <= quantity) {
                    items.erase(it);
                } else {
                    it->second -= quantity;
                }
                return;
            }
        }
    }

    double calculate_total_price() {
        double total_price = 0;
        for (const auto& item : items) {
            total_price += item.first.price * item.second;
        }
        return total_price;
    }
};

// 订单类
class Order {
public:
    std::vector<std::pair<Product, int>> items;
    double total_price;

    Order(std::vector<std::pair<Product, int>> i, double tp) : items(i), total_price(tp) {}
};

// 超市类
class Supermarket {
private:
    std::vector<Product> products;

public:
    void add_product(Product product) {
        products.push_back(product);
    }

    void display_products() {
        std::cout << "Products available in the supermarket:" << std::endl;
        for (const auto& product : products) {
            std::cout << product.name << ": $" << product.price << " (" << product.quantity << " available)" << std::endl;
        }
    }
};

int main() {
    // 创建超市对象
    Supermarket supermarket;

    // 添加商品
    Product product1("Apple", 1.5, 100);
    Product product2("Banana", 0.8, 200);
    supermarket.add_product(product1);
    supermarket.add_product(product2);

    // 创建购物车对象
    ShoppingCart cart;

    // 添加商品到购物车
    cart.add_item(product1, 5);
    cart.add_item(product2, 3);

    // 结账
    double total_price = cart.calculate_total_price();
    std::cout << "Total price: $" << total_price << std::endl;

    return 0;
}

这段代码实现了一个简单的智慧超市管理系统,包括商品管理、购物车管理和订单管理功能。每个模块都被封装成了一个类,使得代码结构清晰,易于理解和维护。