数据结构python商品货架管理系统

基于栈,包含进出货,查货,打印过期货物,删除货物,打印货物清单

详细代码如下,望采纳

from datetime import datetime
from collections import deque

class Product:
    def __init__(self, name, expiration_date):
        self.name = name
        self.expiration_date = expiration_date
    
    def is_expired(self):
        today = datetime.now().date()
        return self.expiration_date < today
    
    def __str__(self):
        return f"{self.name} ({self.expiration_date.strftime('%Y-%m-%d')})"

class Shelf:
    def __init__(self):
        self.products = deque()
    
    def restock(self, product):
        self.products.append(product)
    
    def remove(self, product):
        self.products.remove(product)
    
    def check(self, product_name):
        for product in self.products:
            if product.name == product_name:
                return product
        return None
    
    def check_expired(self):
        expired_products = []
        for product in self.products:
            if product.is_expired():
                expired_products.append(product)
        return expired_products
    
    def __str__(self):
        product_names = [product.name for product in self.products]
        return ", ".join(product_names)

shelf = Shelf()

# 进出货
apple = Product("apple", datetime(2022, 1, 1).date())
banana = Product("banana", datetime(2022, 2, 1).date())
orange = Product("orange", datetime(2022, 3, 1).date())
shelf.restock(apple)
shelf.restock(banana)
shelf.restock(orange)

# 查货
product = shelf.check("apple")
if product:
    print(f"Found {product} on the shelf.")
else:
    print("Product not found on the shelf.")

# 打印过期货物
expired_products = shelf.check_expired()
print(f"Expired products: {expired_products}")

# 删除货物
shelf.remove(banana)
print(f"Current products on the shelf: {shelf}")