연습문제
- 현재 리스트의 모든 요소보다 클때만 요소를 삽입하는 함수를 구현하시오. (작을때만도 있는데 그건 안함)
List.prototype.big = function(element){
var listToCheck = this.toString(),
len = this.length(),
last;
if(typeof element == "String"){
last = listToCheck.sort()[len-1];
if(element > last){
list.append(element);
}
}else{
last = listToCheck.sort(function(a,b){
return a - b;
})[len-1];
if(element > last){
list.append(element);
}
}
}
- 사람의 이름과 성별을 저장하는 Person 클래스를 구현하시오.
- 최소한 10개의 Person객체를 포함하는 리스트를 만드시오.
- 리스트에서 같은 성별을 가진 사람을 모두 출력하는 함수를 구현하시오.
- 대여한 영화를 대여된 영화리스트로 추가하시오.
- 고객이 영화를 대여할 때마다 대여된 영화 리스트를 출력하시오.
- 비디오 대여 상점 프로그램에 반납 함수를 추가하시오.
- 고객이 영화를 반납하면 대여된 영화 리스트에서 영화를 삭제하고, 이용할수 있는 영화리스트에 추가하시오.
const fs = require('fs');
var new_movie = new Movie();
function Movie(){
this.movieList = [];
this.rentMovie = [
{name: "minwoo", movie: "apple"}
];
this.size = this.movieList.length;
this.toString = this.movieList.toString();
};
Movie.prototype.renting = function(customer, movieName) {
if (customer && movieName) {
if(this.movieList.includes(movieName)){
this.rentMovie.push({name: customer, movie: movieName});
this.movieList.shift(movieName);
console.log(customer + "가 "+ movieName + "를 대여했습니다.")
}else{
console.log(movieName + "는 이미 대여된 영화입니다.")
this.include(this.rentMovie, movieName);
};
}else{
console.log("이름을 입력하세요!");
}
};
Movie.prototype.return = function(movieName) {
if (this.include(this.rentMovie, movieName)) {
this.rentMovie.shift(this.include(this.rentMovie, movieName));
this.movieList.push(movieName);
console.log(movieName + "이 반납되었습니다.");
console.log(this.movieList);
}
};
Movie.prototype.include = function(list, movieName) {
var result = list.find(function(l) {
return l.movie == movieName;
});
return result;
};
function createArr(file){
var movies = fs.readFileSync(file, 'utf8').split("\n");
for(var i = 0; i < movies.length; i++){
new_movie.movieList.push(movies[i].trim());
console.log(new_movie.movieList[i]);
}
}
createArr("film.txt");
console.log(new_movie.size);
console.log(new_movie.movieList);
new_movie.return("apple");
new_movie.renting("minwoo","banana");
new_movie.renting("minwoo","banana");