0.完整代码
class Thing:
def __init__(self, sname, name, detaction, state = 0):
self.sname = sname
self.name = name
self.detaction = detaction
self.state = state
def __str__(self):
status = '未借出'
if self.state == 1:
status = '已借出'
return '缩写:(%s) 全称:%s 描述:%s\n状态:%s ' % (self.sname, self.name, self.detaction, status)
class ThingManager:
things = []
def __init__(self):
thing1 = Thing('jsq','计算器','卡西欧')
self.things.append(thing1)
def menu(self):
print('欢迎使用物资管理系统\n')
while True:
print('1.查询所有物资\n2.添加物资\n3.借物资\n4.归还物资\n5.退出系统\n')
choice = int(input('请输入选择对应的功能:'))
if choice == 1:
self.show_all_thing()
elif choice == 2:
self.add_thing()
elif choice == 3:
self.lend_thing()
elif choice == 4:
self.return_thing()
else:
print('感谢使用!')
break
def show_all_thing(self):
print('物资信息如下:')
for thing in self.things:
print(thing)
print('')
def add_thing(self):
new_sname = input('请输入物资缩写:')
new_name = input('请输入全称:')
new_detaction = input('请输入物资描述:')
new_thing = Thing(new_sname, new_name, new_detaction)
self.things.append(new_thing)
print('物资录入成功!\n')
def check_thing(self,sname):
for thing in self.things:
if thing.sname == sname:
return thing
else:
return None
def lend_thing(self):
sname = input('请输入物资的缩写:')
res = self.check_thing(sname)
if res != None:
if res.state == 1:
print('已经被借走')
else:
print('成功')
res.state = 1
else:
print('物资暂时没有收录在系统里')
def return_thing(self):
sname = input('请输入归还物资的缩写:')
res = self.check_thing(sname)
if res == None:
print('没有该物资')
else:
if res.state == 0:
print('物资没有被借走')
else:
print('归还成功!')
res.state = 0
manager = ThingManager()
manager.menu()
1.解释
class Thing:
def __init__(self, sname, name, detaction, state = 0):
self.sname = sname
self.name = name
self.detaction = detaction
self.state = state
def __str__(self):
status = '未借出'
if self.state == 1:
status = '已借出'
return '缩写:(%s) 全称:%s 描述:%s\n状态:%s ' % (self.sname, self.name, self.detaction, status)
首先创建一个类(class),然后对类进行描述(def)
内容包括sname(缩写)name(全称)detaction(描述 “英文是随便打的。。。”)
state(状态) 1借出 0未借出
_init_ 代表对实例属性的初始化
_str_代表直接打印其中返回(return)值,就是print(Thing)后会直接显示return中的值
class ThingManager:
things = []
def __init__(self):
thing1 = Thing('jsq','计算器','卡西欧')
self.things.append(thing1)
定义菜单
增加物资描述(直接导入物资的话)
每个物资都应该有thing1 = Thing('s','n','d')和self.things.append(thing1)
def menu(self):
print('欢迎使用物资管理系统\n')
while True:
print('1.查询所有物资\n2.添加物资\n3.借物资\n4.归还物资\n5.退出系统\n')
choice = int(input('请输入选择对应的功能:'))
if choice == 1:
self.show_all_thing()
elif choice == 2:
self.add_thing()
elif choice == 3:
self.lend_thing()
elif choice == 4:
self.return_thing()
else:
print('感谢使用!')
break
菜单主界面定义
def show_all_thing(self):
print('物资信息如下:')
for thing in self.things: #遍历表格,打印物资
print(thing)
print('')
def add_thing(self): #新建物资
new_sname = input('请输入物资缩写:')
new_name = input('请输入全称:')
new_detaction = input('请输入物资描述:')
new_thing = Thing(new_sname, new_name, new_detaction)
self.things.append(new_thing)
print('物资录入成功!\n')
def check_thing(self,sname): #查看物资状态
for thing in self.things:
if thing.sname == sname:
return thing
else:
return None
def lend_thing(self):
sname = input('请输入物资的缩写:')
res = self.check_thing(sname)
if res != None:
if res.state == 1:
print('已经被借走')
else:
print('成功')
res.state = 1
else:
print('物资暂时没有收录在系统里')
def return_thing(self):
sname = input('请输入归还物资的缩写:')
res = self.check_thing(sname)
if res == None:
print('没有该物资')
else:
if res.state == 0:
print('物资没有被借走')
else:
print('归还成功!')
res.state = 0
这样一个简陋的物资管理程序就做好了,当然还可以加入信息录入人,借出人信息,再读取一个表格来储存信息。