使用python语言编写一个超市控制台系统
时间: 2024-01-16 07:03:14 浏览: 89
由于没有具体的需求和功能描述,以下是一个简单的超市控制台系统,可根据实际需求进行修改和完善。
```python
class Supermarket:
def __init__(self):
self.products = {'apple': 5, 'banana': 3, 'orange': 4} # 商品清单及价格
self.cart = {} # 购物车
def show_menu(self):
print('Welcome to the supermarket!')
print('Products:')
for p, price in self.products.items():
print(f'{p}: {price} yuan')
def add_to_cart(self, product, quantity):
if product in self.products:
if quantity > 0:
if product in self.cart:
self.cart[product] += quantity
else:
self.cart[product] = quantity
print(f'{quantity} {product}(s) added to cart.')
else:
print('Invalid quantity.')
else:
print('Invalid product.')
def show_cart(self):
if self.cart:
print('Shopping cart:')
for p, quantity in self.cart.items():
print(f'{p}: {quantity}')
else:
print('Shopping cart is empty.')
def checkout(self):
total_price = 0
if self.cart:
print('Checkout:')
for p, quantity in self.cart.items():
price = self.products[p] * quantity
print(f'{p}: {quantity} x {self.products[p]} = {price} yuan')
total_price += price
print(f'Total price: {total_price} yuan')
self.cart.clear()
else:
print('Shopping cart is empty.')
def main():
supermarket = Supermarket()
while True:
cmd = input('Please enter command (menu/add/show/checkout/exit): ')
if cmd == 'menu':
supermarket.show_menu()
elif cmd == 'add':
product = input('Please enter product name: ')
quantity = int(input('Please enter quantity: '))
supermarket.add_to_cart(product, quantity)
elif cmd == 'show':
supermarket.show_cart()
elif cmd == 'checkout':
supermarket.checkout()
elif cmd == 'exit':
break
else:
print('Invalid command.')
if __name__ == '__main__':
main()
```
使用示例:
```
Please enter command (menu/add/show/checkout/exit): menu
Welcome to the supermarket!
Products:
apple: 5 yuan
banana: 3 yuan
orange: 4 yuan
Please enter command (menu/add/show/checkout/exit): add
Please enter product name: apple
Please enter quantity: 2
2 apple(s) added to cart.
Please enter command (menu/add/show/checkout/exit): add
Please enter product name: banana
Please enter quantity: 3
3 banana(s) added to cart.
Please enter command (menu/add/show/checkout/exit): show
Shopping cart:
apple: 2
banana: 3
Please enter command (menu/add/show/checkout/exit): checkout
Checkout:
apple: 2 x 5 = 10 yuan
banana: 3 x 3 = 9 yuan
Total price: 19 yuan
Please enter command (menu/add/show/checkout/exit): show
Shopping cart is empty.
Please enter command (menu/add/show/checkout/exit): exit
```