水果系统(面向过程,面向对象)
Posted hany-postq473111315
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了水果系统(面向过程,面向对象)相关的知识,希望对你有一定的参考价值。
fruit = []
def menu():
print(
‘‘‘
********************水果超市******************** (面向对象,面向过程)
1. 查询全部水果
2. 查询指定名称的水果
3. 增加水果(增加到数据库)
4. 修改水果数量或者价格
5. 删除水果
6. 按照价格排序
7. 退出系统
***********************************************
‘‘‘
)
def showFruit():
‘‘‘功能1 查询全部水果‘‘‘
print(‘-‘ * 30)
print("水果的信息如下:")
print(‘-‘ * 30)
print(‘序号 水果名 价格 数量‘)
fru_id = 1
for fru_temp in fruit:
print("%s %s %s %s "%(fru_id,fru_temp[‘name‘],fru_temp[‘price‘],fru_temp[‘num‘]))
fru_id += 1
def searchFruitName():
‘‘‘功能2 查询指定名称的水果‘‘‘
showFruit()
fru_name = input("请输入想要查询的水果名称")
fru_id = 1
for fru in fruit:
if fru_name in fru[‘name‘]:
print("该水果信息如下:")
print("%d %s %s %s " % (fru_id, fru[‘name‘], fru[‘price‘], fru[‘num‘]))
return
fru_id += 1
print("没有查询到该水果名称")
def addFruit():
‘‘‘功能3 增加水果(增加到数据库)‘‘‘
newFruitName = input(‘请输入新水果的名称‘)
newFruitPrice = input(‘请输入新水果的价格‘)
newFruitNum = input(‘请输入新水果的数量‘)
newFruit = {}
newFruit[‘name‘] = newFruitName
newFruit[‘price‘] = newFruitPrice
newFruit[‘num‘] = newFruitNum
fruit.append(newFruit)
def changeFruit():
‘‘‘功能4 修改水果数量或者价格‘‘‘
showFruit()
fru_id = int(input("请输入需要修改的水果的序号: "))
changeFruitName = input(‘请输入修改后的水果的名称‘)
changeFruitPrice = input(‘请输入修改后的水果的价格‘)
changeFruitNum = input(‘请输入修改后的水果的数量‘)
fruit[fru_id - 1][‘name‘] = changeFruitName
fruit[fru_id - 1][‘price‘] = changeFruitPrice
fruit[fru_id - 1][‘num‘] = changeFruitNum
def delFruit():
‘‘‘功能5 删除水果‘‘‘
showFruit()
delFruitId = int(input(‘请输入要删除的序号:‘))
del fruit[delFruitId - 1]
def sortFruit():
‘‘‘功能6 按照价格排序‘‘‘
showFruit()
sortStandard = input("请选择(0升序;1降序):")
if sortStandard == "0":
sortStandard = True
elif sortStandard == "1":
sortStandard = False
else:
print("您的输入有误,请重新输入!")
fruit.sort(key = lambda x:x[‘price‘],reverse = sortStandard)
showFruit()
def exitSystem():
‘‘‘功能7 退出系统‘‘‘
print("您已经退出水果超市系统")
exit()
def main():
notExit = True
while notExit:
menu()
try:
option = int(input("请选择功能:"))
except Exception as e:
print("请重新输入")
if option in [i for i in range(1,8)]:
if option == 1:
showFruit()
elif option == 2:
searchFruitName()
elif option == 3:
addFruit()
elif option == 4:
changeFruit()
elif option == 5:
delFruit()
elif option == 6:
sortFruit()
elif option == 7:
notExit = False
exitSystem()
if __name__ == ‘__main__‘:
main()
class FruitMarket():
def __init__(self):
self.fruit = []
def showFruit(self):
‘‘‘功能1 查询全部水果‘‘‘
print(‘-‘ * 30)
print("水果的信息如下:")
print(‘-‘ * 30)
print(‘序号 水果名 价格 数量‘)
fru_id = 1
for fru_temp in self.fruit:
print("%s %s %s %s " % (fru_id, fru_temp[‘name‘], fru_temp[‘price‘], fru_temp[‘num‘]))
fru_id += 1
def searchFruitName(self):
‘‘‘功能2 查询指定名称的水果‘‘‘
self.showFruit()
fru_name = input("请输入想要查询的水果名称")
fru_id = 1
for fru in self.fruit:
if fru_name in fru[‘name‘]:
print("该水果信息如下:")
print("%d %s %s %s " % (fru_id, fru[‘name‘], fru[‘price‘], fru[‘num‘]))
return
fru_id += 1
print("没有查询到该水果名称")
def addFruit(self):
‘‘‘功能3 增加水果(增加到数据库)‘‘‘
newFruitName = input(‘请输入新水果的名称‘)
newFruitPrice = input(‘请输入新水果的价格‘)
newFruitNum = input(‘请输入新水果的数量‘)
newFruit = {}
newFruit[‘name‘] = newFruitName
newFruit[‘price‘] = newFruitPrice
newFruit[‘num‘] = newFruitNum
self.fruit.append(newFruit)
def changeFruit(self):
‘‘‘功能4 修改水果数量或者价格‘‘‘
self.showFruit()
fru_id = int(input("请输入需要修改的水果的序号: "))
changeFruitName = input(‘请输入修改后的水果的名称‘)
changeFruitPrice = input(‘请输入修改后的水果的价格‘)
changeFruitNum = input(‘请输入修改后的水果的数量‘)
self.fruit[fru_id - 1][‘name‘] = changeFruitName
self.fruit[fru_id - 1][‘price‘] = changeFruitPrice
self.fruit[fru_id - 1][‘num‘] = changeFruitNum
def delFruit(self):
‘‘‘功能5 删除水果‘‘‘
self.showFruit()
delFruitId = int(input(‘请输入要删除的序号:‘))
del self.fruit[delFruitId - 1]
def sortFruit(self):
‘‘‘功能6 按照价格排序‘‘‘
self.showFruit()
sortStandard = input("请选择(0升序;1降序):")
if sortStandard == "0":
sortStandard = True
elif sortStandard == "1":
sortStandard = False
else:
print("您的输入有误,请重新输入!")
self.fruit.sort(key=lambda x: x[‘price‘], reverse=sortStandard)
self.showFruit()
def exitSystem(self):
‘‘‘功能7 退出系统‘‘‘
print("您已经退出水果超市系统")
exit()
def menu( ):
print(
‘‘‘
********************水果超市******************** (面向对象,面向过程)
1. 查询全部水果
2. 查询指定名称的水果
3. 增加水果(增加到数据库)
4. 修改水果数量或者价格
5. 删除水果
6. 按照价格排序
7. 退出系统
***********************************************
‘‘‘
)
fruitmarket = FruitMarket()
def main():
notExit = True
while notExit:
menu()
try:
option = int(input("请选择功能:"))
except Exception as e:
print("请重新输入")
if option == 1:
fruitmarket.showFruit()
elif option == 2:
fruitmarket.searchFruitName()
elif option == 3:
fruitmarket.addFruit()
elif option == 4:
fruitmarket.changeFruit()
elif option == 5:
fruitmarket.delFruit()
elif option == 6:
fruitmarket.sortFruit()
elif option == 7:
notExit = False
fruitmarket.exitSystem()
if __name__ == ‘__main__‘:
main()
2020-05-24
以上是关于水果系统(面向过程,面向对象)的主要内容,如果未能解决你的问题,请参考以下文章