完成便捷水果店4.0的程序编写,实现以下的效果,请将代码与运行结果截图上传
时间: 2023-05-26 14:07:33 浏览: 106
很抱歉,作为AI语言模型,我没有办法本地编写程序与截图上传。但我可以帮你提供代码:
```python
class Fruit:
def __init__(self, name, price, stock):
self.name = name
self.price = price
self.stock = stock
class FruitShop:
def __init__(self):
self.fruits = []
def buy_fruit(self, name, number):
for fruit in self.fruits:
if fruit.name == name:
if fruit.stock >= number:
fruit.stock -= number
return fruit.price * number
else:
return -1
return -1
def add_fruit(self, name, price, stock):
for fruit in self.fruits:
if fruit.name == name:
fruit.stock += stock
return
self.fruits.append(Fruit(name, price, stock))
def get_stock(self, name):
for fruit in self.fruits:
if fruit.name == name:
return fruit.stock
return -1
shop = FruitShop()
shop.add_fruit('apple', 5, 10)
shop.add_fruit('banana', 3, 5)
shop.add_fruit('orange', 4, 8)
while True:
print('Welcome to Fruit Shop 4.0')
print('1. Buy fruit')
print('2. Add fruit')
print('3. Check stock')
print('4. Exit')
choice = input('Please select an option:')
if choice == '1':
name = input('Please enter the name of the fruit you want to buy:')
number = int(input('Please enter the number of fruits you want to buy:'))
cost = shop.buy_fruit(name, number)
if cost == -1:
print('Sorry, we do not have enough stock.')
else:
print('You need to pay', cost, 'dollars.')
elif choice == '2':
name = input('Please enter the name of the fruit you want to add:')
price = int(input('Please enter the price of the fruit:'))
stock = int(input('Please enter the stock of the fruit:'))
shop.add_fruit(name, price, stock)
print('Add successfully.')
elif choice == '3':
name = input('Please enter the name of the fruit you want to check:')
stock = shop.get_stock(name)
if stock == -1:
print('Sorry, we do not have this fruit.')
else:
print('The stock of', name, 'is', stock)
elif choice == '4':
print('Thank you for using Fruit Shop 4.0!')
break
else:
print('Invalid option. Please select again.')
```
这是一个简单的水果店程序,实现了买水果、添加水果和查询水果库存等功能。你可以将以上代码复制粘贴到Python IDE中执行,也可以参考其中的思路完成自己的代码,具体效果请自行体验。
阅读全文