class Dvd:
def __init__(self,serialNumber=None,state='可借',name=None,loanDate="",loanNumber=0):
self.serialNumber = serialNumber
self.state = state
self.name = name
self.loanDate = loanDate
self.loanNumber = loanNumber
class DvdManage:
def __init__(self):
self.dvds = []
self.dvds.append(Dvd('序号','状态','名称','借出日期','借出次数'))
self.dvds.append(Dvd(len(self.dvds),'可借','《罗马假日》','',0))
self.dvds.append(Dvd(len(self.dvds),'可借','《老人与海》','',0))
self.dvds.append(Dvd(len(self.dvds),'可借','《伊索寓言》','',0))
self.dvds.append(Dvd(len(self.dvds),'可借','《浪漫满屋》','',0))
def myadd(self):
dvdName = input('请输入要添加的DVD名称:').strip()
if len(self.dvds) <= 6:
self.dvds.append(Dvd(len(self.dvds),'可借',dvdName,'',0))
print('新增{0}成功'.format(dvdName))
else:
print('新增{0}失败,货架已满!'.format(dvdName))
def mycheck(self):
for dvd in self.dvds:
print(dvd.serialNumber,dvd.state,dvd.name,dvd.loanDate,dvd.loanNumber)
def mydel(self):
delname = input('请输入要删除的DVD名称:').strip()
for dvd in self.dvds:
if delname == dvd.name:
if dvd.state == '可借':
self.dvds.remove(dvd)
print('删除{0}成功'.format(delname))
else:
print('删除失败,为借出状态,不能删除!')
break
else:
print('删除失败,没有找到匹配信息!')#注意else的位置
# for dvd in self.dvds:
# print(dvd.serialNumber, dvd.state, dvd.name, dvd.loanDate, dvd.loanNumber)
def myloan(self):
loanName = input('请输入要借的DVD名称:').strip()
for dvd in self.dvds:
if loanName == dvd.name:
if dvd.state == '可借':
while True:
myloanDate = input('请输入借出日期:')
loandate = ''
for num in filter(str.isdigit,myloanDate):
loandate += num
loandate = int(loandate)
if loandate > 0 and loandate <= 31:
dvd.loanDate = myloanDate
dvd.state = '已借出'
dvd.loanNumber += 1
print('借出{0}成功!'.format(loanName))
break
else:
print('所输日期无效,请重新输入!')
else:
print('{0}已被借出!'.format(loanName))
break
else:
print('借出失败,没有找到匹配信息!')
def giveBack(self):
giveBackName = input('请输入要归还的DVD名称:')
for dvd in self.dvds:
if dvd.name == giveBackName:
if dvd.state == '已借出':
giveBackDate = input('请输入归还日期:')
backDate = ''
for num in filter(str.isdigit, giveBackDate):
backDate += num
backDate = int(backDate)
if backDate > 0 and backDate <= 31:
dvd.state = '可借'
loandate = ''
for num in filter(str.isdigit, dvd.loanDate):
loandate += num
loandate = int(loandate)
if backDate > loandate:
sumDate = backDate - loandate
print('归还{0}成功!'.format(giveBackName))
print('''
借出日期为{0}
归还日期为{1}
应付租金(元){2}'''.format(dvd.loanDate,giveBackDate,sumDate))
break
else:
print('还DVD日期不能小于借出日期!')
break
else:
print('所输日期无效,请重新输入!')
break
else:
print('此DVD未借出,不能执行还DVD操作!')
break
else:
print('您输入有误,没有此DVD!')
class Kongzhi:
manage = DvdManage()
def start(self):
while True:
print('欢迎使用迷你DVD管理器')
print('-'*40)
print('''
1.新增DVD
2.查看DVD
3.删除DVD
4.借出DVD
5.归还DVD
6.退出DVD
''')
print('-'*40)
# choice = self.ch()
if self.ch() == 6: #相当于调用了ch()
break
print('*'*30)
tag = 0
while tag == 0:
try:
isexit = int(input('输入0返回:'))
if isexit == 0:
tag = 1
else:
tag = 2
except ValueError:
print('输入错误!')
if tag == 1:
continue
elif tag == 2:
break
def ch(self):
choice = input('请选择:')
if choice.isdigit():
choice = int(choice)
if choice == 1:
print('---->新增DVD')
self.manage.myadd()
elif choice == 2:
print('---->查看DVD')
self.manage.mycheck()
elif choice == 3:
print('---->删除DVD')
self.manage.mydel()
elif choice == 4:
print('---->借出DVD')
self.manage.myloan()
elif choice == 5:
print('---->归还DVD')
self.manage.giveBack()
elif choice == 6:
print('谢谢使用!')
else:
print('请输入有效数字!')
else:
print('请输入数字!')
return choice
# DvdManage().giveBack()
# a=DvdManage()
# a.myloan()
# a.giveBack()
Kongzhi().start()
#del str1[索引] , str1.remove(对象)
'''
str1 = ['name',3,5,6,7]
str1.remove(3)#移除指定值
str1.pop()#指定索引删除 或者 默认删除序列最后一个
print(str1)
myindex = str1.index('name')
del str1[myindex]
print(myindex,str1)
'''
#str的拼接用+号,或者是把字符追加到列表中再用str.join(list1)把列表改为字符串
#filter()筛选作用,有返回值,需要遍历输出
'''
str1 = '12日'
str2 = '3日'
str3 = ''
for num in filter(str.isdigit,str1):
str3 += num
print(num)
print(str1,'-'*6,str3)
'''
#格式化输出对齐方式'{:*^7}{:^10}{:^12}{:^12}{:>9}'.format()
项目DVD
最新推荐文章于 2020-11-22 16:11:50 发布