数据结构与算法描述(第三章案例介绍)

来源:互联网 发布:美萍汽车维修软件 编辑:程序博客网 时间:2024/04/30 02:38
//显示现有的影碟清单
function displayList(list) {
    for(var i = 0;i < list.length();++i) {
        if(list.getElement() instanceof Customer) {
            console.log(list.getElement().name + "," +list.getElement().movie);
            list.next();
        } else {
            console.log(list.getElement());
            list.next();
        }
    }
    list.front();
}

//允许客户检出电影的函数
function checkOut(name,movie,filmList,customerList) {
    if(movieList.contains(movie)) {
        var c = new Customer(name,movie);
        customerList.append(c);
        var remove = filmList.remove(movie);
        lendedlist.append(remove);
        showLended();
    } else {
        console.log(movie + "is not available");
    }
}

function showLended() {
    console.log("已经出租的影片有:");
    console.log(JSON.stringify(lendedlist.showList()));
}

function check_in(name,returning) {
    lendedlist.remove(returning);
    movieList.append(returning);
    console.log(name + "归还影片:" + returning);
}

function Customer(name,movie) { //Customer对象
    this.name = name;
    this.movie = movie;
}
//测试
var movies = ["movie_first","movie_second","movie_third","movie_forth","movie_fifth"];
var movieList = new List(); //保存电影
var customers = new List(); //用来保存在系统中检出电影的客户
var lendedlist = new List(); //已出租列表
//将电影数组保存到一个列表中
for (var i = 0;i < movies.length;++i) {
    movieList.append(movies[i]);
}
console.log("目前还可以租借的电影有:");
displayList(movieList);
console.log("将为xq检出电影movie_second");
checkOut("xq","movie_second",movieList,customers);
console.log("将为xq检出电影movie_first");
checkOut("xq","movie_first",movieList,customers);
console.log("已经租借的客户信息:");
displayList(customers);
console.log("现在还可以租借的电影有:");
displayList(movieList);
checkOut("xq","movie_third",movieList,customers);
console.log("已经租借的客户信息:");
displayList(customers);
console.log("现在还可以租借的电影有:");
displayList(movieList);
check_in("xq","movie_second");
console.log("现在还可以租借的电影有:");
displayList(movieList);
0 0
原创粉丝点击