<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>迷你DVD管理系统</title>
</head>
<body>
<script>
function DVD(name,type,state=0,count=0){
this.name = name
this.type = type
this.state = state //状态0表示未借出,1表示借出
this.count = count //借出次数
}
// let d1 = new DVD('警察故事','警匪片')
// console.log(d1);
let dvdShop={
dvds:[
new DVD('泰坦尼克号','爱情片',0,5),
new DVD('超级战舰','战争片',1,8),
new DVD('飓风营救','动作片',0,12),
new DVD('弱点','励志片',1,7),
new DVD('利刃出鞘','悬疑片',0,5),
new DVD('星际穿越','科幻片',0,6)
],
getDVDByName:function(name){
//let dvd = null;
return this.dvds.find(d=>d.name===name)
return dvd
},
//归还dvd
evenDVD:function(){
console.log('**************归还DVD******************');
let name = prompt('请输入DVD的名称:')
let dvd = this.getDVDByName(name)
if (dvd===undefined) {
alert('对不起,您要的dvd没有')
}else{
if (dvd.state===1) {
alert('归还成功')
dvd.state = 0
dvd.count++
}else{
alert('对不起,该DVD从未借出,无需归还!')
}
}
},
//租售
borrowDVD:function(){
console.log('**************租售DVD******************');
let name = prompt('请输入DVD的名称:')
let dvd = this.getDVDByName(name)
if (dvd===undefined) {
alert('对不起,您要的dvd没有')
}else{
if (dvd.state===0) {
alert('借出成功')
dvd.state = 1
}else{
alert('对不起,该dvd已经借出')
}
}
},
show:function(){
console.log('**************查看DVD******************');
console.log('序号 名称 类型 状态 人气');
this.dvds.forEach((d,index)=>{
console.log (`${index+1} ${d.name} ${d.type} ${d.state===0?'未借':'已借'} ${d.count}\n`);
})
},
//添加
addDVD:function(){
console.log('**************添加DVD******************');
let name = prompt('请输入DVD的名称:')
let dvd = this.getDVDByName(name)
if (dvd===undefined) {
let type = prompt('请输入DVD的类型:')
let newdvd = new DVD(name,type)
this.dvds.push(newdvd)
alert('添加成功!')
//console.log(dvd);
}else{
alert('该DVD已经存在')
}
},
delDVD:function(){
console.log('**************删除DVD******************');
let name = prompt('请输入DVD的名称:')
let dvd = this.getDVDByName(name)
//根据dvd的名称返回该dvd在数组中的下标
//let index = this.dvds.findIndex(d=>d.name===name)
if (dvd===undefined) {
alert('对不起,您要删除的dvd不存在')
}else{
if (dvd.state===1) {
alert('该dvd已经借出,暂时无法删除!')
}else{
let index = this.dvds.findIndex(d=>d.name===name)
this.dvds.splice(index,1)
alert('删除成功!')
}
}
},
menu:function(){
let no = parseInt(prompt('1.查看DVD 2.租售DVD 3.归还DVD 4.添加DVD 5.删除DVD 0.退出系统'))
switch(no){
case 1:
this.show()
break;
case 2:
this.borrowDVD()
break;
case 3:
this.evenDVD()
break;
case 4:
this.addDVD()
break;
case 5:
this.delDVD()
break;
default:
return
alert('退出成功,欢迎下次继续使用!')
}
arguments.callee.call(this)
}
}
dvdShop.menu()
</script>
</body>
</html>