实现python简单购物系统

        代码实现了一个简单的购物系统。

        用户可以注册账号,登录,添加余额,查看购物车,查看所有商品,购买商品,移除购物车中的商品以及结账等操作。

具体的,程序实现了以下功能:

  1. 用户注册,登录,退出系统。
  2. 添加余额。
  3. 将商品加入购物车,查看购物车内的商品。
  4. 查看所有商品,购买商品,从购物车中删除商品。
  5. 结账:输入收货地址和电话,从余额中扣除购物车中所有商品的总价格,若余额不足则提示余额不足。结账完成后,显示购买的商品的手机号和地址,提示结账成功,清空购物车。

        整个程序定义了两个类:User类和ShoppingMarket类。User类记录用户的姓名、余额、购物车等信息;ShoppingMarket类是购物系统的主体,实现了所有的操作。

话不多说,上代码:

class User:
    def __init__(self, name, balance):
        self.name = name
        self.balance = balance
        self.cart = []

    def add_to_cart(self, item, price):
        self.cart.append((item, price))

    #移除,输入想要删除的商品id和数量
    def remove_from_cart(self, id, count):
        for i in range(count):
            self.cart.remove(self.cart[id])

    def check_balance(self):
        print(f"当前余额为: {self.balance}")


class ShoppingMarket:
    def __init__(self):
        self.users = {}
        #10个商品
        self.products = {1:("苹果", 10),  2:("香蕉", 20),  3:("橘子", 30),  4:("梨", 40),  5:("葡萄", 50),  6:("西瓜", 60),  7:("芒果", 70),  8:("榴莲", 80),  9:("火龙果", 90),  10:("草莓", 100)}

    def register(self, name, password):
        self.users[name] = User(name, 0)

    def login(self, name, password):
        if name in self.users:
            self.current_user = self.users[name]
            return True
        return False

    def add_balance(self, amount):
        self.current_user.balance += amount

    def show_cart(self):
        print("购物车:")
        for item, price in self.current_user.cart:
            print(f"{item}: {price}")

    def show_products(self):
        print("所有商品:")
        for id, (item, price) in self.products.items():
            print(f"{id}. {item}: {price}")

    def buy(self, id, count, price):
        for i in range(count):
            self.current_user.add_to_cart(self.products[id][0], price)

    # 移除,输入想要删除的商品id和数量
    def remove_from_cart(self, id, count):
        for i in range(count):
            self.current_user.cart.remove(self.current_user.cart[id])

    # 结账,写地址和电话后扣除余额,余额不足则提示;结账完成后,显示购买的商品的手机号和地址,提示结账成功,清空购物车
    def checkout(self, address, phone):
        total_price = 0
        for item, price in self.current_user.cart:
            total_price += price
        if total_price > self.current_user.balance:
            print("余额不足")
        else:
            self.current_user.balance -= total_price
            print("结账成功")
            print("购买的商品为:", item)
            #余额:收货地址为:手机号:
            print("余额:", self.current_user.balance, "收货地址为:", address, "手机号:", phone)
            self.current_user.cart = []

    def run(self):

        while True:
            print("1. 注册")
            print("2. 登录")
            print("3. 退出")
            choice = input("请输入选项: ")
            if choice == "1":
                name = input("请输入用户名: ")
                password = input("请输入密码: ")
                self.register(name, password)
            elif choice == "2":
                name = input("请输入用户名: ")
                password = input("请输入密码: ")
                if self.login(name, password):
                    while True:
                        print("1. 查看余额")
                        print("2. 充值")
                        print("3. 查看购物车")
                        print("4. 查看所有商品")
                        print("5. 添加商品到购物车")
                        print("6. 从购物车删除商品")
                        print("7. 结账")
                        print("8. 退出")
                        choice = input("请输入选项: ")
                        if choice == "1":
                            self.current_user.check_balance()
                        elif choice == "2":
                            amount = int(input("请输入充值金额: "))
                            self.add_balance(amount)
                        elif choice == "3":
                            self.show_cart()
                        elif choice == "4":
                            self.show_products()
                        elif choice == "5":
                            id = int(input("请输入商品id: "))
                            count = int(input("请输入商品数量: "))
                            price = self.products[id][1]
                            self.buy(id, count, price)
                        elif choice == "6":
                            id = int(input("请输入移除的商品id: "))
                            count = int(input("请输入移除的商品数量: "))
                            self.remove_from_cart(id, count)
                        elif choice == "7":
                            address = input("请输入地址: ")
                            phone = input("请输入电话: ")
                            self.checkout(address, phone)
                        elif choice == "8":
                            break
            elif choice == "3":
                break


if __name__ == "__main__":
    market = ShoppingMarket()
    market.run()

  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

社区专家-Monster-XH

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值