Python做一个商场购物代码运行后有
登录
注册
退出
注册成功后可以登录,登录成功后会有充值,充值成功后去选择购物
查询余额
充值
查看商品,最少十个可以添加的物品
添加商品到购物车
购物车
进入购物车后购物车里面可以查看
已经选择的商品,修改商品的数量,删除商品,结算,结算后显示余额,询问手机号和地址,
Python做一个商场购物代码运行后有
登录
注册
退出
注册成功后可以登录,登录成功后会有充值,充值成功后去选择购物
查询余额
充值
查看商品,最少十个可以添加的物品
添加商品到购物车
购物车
进入购物车后购物车里面可以查看
已经选择的商品,修改商品的数量,删除商品,结算,结算后显示余额,询问手机号和地址,
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()