电子商品商店

"""
Shop
Attrs:shops、cart
Methods:
    1.list() 列出商品
    2.list_cart() 列出购物车商品
    3.add() 添加新商品
    4.run() 执行程序
<<<<<<< .mine
"""


class Shop(object):
    def __init__(self,shops,cart):
        self.shops = shops
        self.cart = cart

    def list(self):

from goods import Good

from cart import CartItem

class Shop(object):
    """商店"""
    def __init__(self):
        # 存储所有商品
        self.shops = []
        # 存储购物车商品
        self.cart = []
        # 加载商品
        self.load()

    def load(self):
        """加载商品"""
        self.add(Goods('Apple Watch', 3299, 100))
        self.add(Goods('AirPods', 1288, 100))
        self.add(Goods('Home Pod', 1299, 100))
        self.add(Goods('iPhone X', 6288, 100))

    def add(self, good):
        """
        设置新商品的id,添加到列表中
        :param good: 新商品
        :return: None
        """
        good.id = len(self.shops) + 1
        self.shops.append(good)

    def print_line(self):

        print('-'*50)

    def print_double_line(self):
        print('='*50)

    def list(self):
        """列出所有商品"""
        print('请选择商品:')
        self.print_double_line()
        # 遍历商品列表
        for g in self.shops:

            print('%s       %s        %s' % (g.id, g.name, g.price))
            self.print_line()

    def list_cart(self):
        """展示购物车商品,计算总价"""
        self.print_line()
        total = 0.0
        for item in self.cart:
            print('%s         =¥%s' % (item, item.amount()))
            total += item.amount()
        self.print_line()
        print('总金额:¥%.2f' % total)

    def add_to_cart(self):
        """添加商品到购物车"""
        print('\n')
        g_id = input('请输入商品Id(回车去结账,0清空购物车):')

        if len(g_id) == 0:
            # 结账
            total = 0.0
            for item in self.cart:

                total += item.amount()
            self.print_line()
            print('请支付:¥%.2f' % total)

            # 清空购物车
            self.cart.clear()
            print('支付成功!')

        elif g_id == '0':
            self.cart.clear()
            print('购物车已清空!')
        else:
            # 计算商品索引
            idx = int(g_id) - 1
            # 取出商品
            goods = self.shops[idx]
            self.print_line()
            print(goods)

            count = int(input('请输入购买数量:'))
            # 判断数量是否大于库存量
            while count > goods.stock:
                count = int(input('没有这么多商品,请重新输入:'))

            # 如果商品已经在购物车中,修改商品数量
            # 变量表示在购物车中是否有这个商品
            is_exsts = False
            for item in self.cart:
                if item.goods == goods:
                    # 说明在购物车中有该商品
                    is_exsts = True
                    item.count += count
                    # 减少库存
                    goods.stock -= count

            # 如果执行到这,is_exsts的值还是False,说明购物车中没有该商品
            if is_exsts == False:
                # 把商品添加到购物车
                goods.stock -= count
                self.cart.append(CartItem(goods, count))

            # 展示购物车商品,计算总价
            self.list_cart()

    def run(self):
        """运行应用程序"""
        print('电子产品商店')
        print('v1.0')
        print('\n')

        self.list()

        while True:
            self.add_to_cart()


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值