JavaScript_ Build a Library

정세비·2021년 5월 26일
0

test

목록 보기
12/13
post-thumbnail
  • class, object, 상속, getter, setter 등을 이용하여 라이브러리 구축해보기
  • 3가지 미디어가 존재(book, movie, cd)하며 아래와 같은 속성 및 메서드가 있음

  • toggleCheckOutStatus()는 isCheckedOut 의 값을 가져오는데 이때 값이 true면 false로, false면 true로 변환되도록 한다.
  • getAverageRating 메서드는 ratings 배열의 값의 평균을 return하며, ratings 배열의 합계는 reduce method를 통해서 구한 후, ratings 배열의 length로 나누어서 retrun해야한다.
  • 하나의 인자가 있는 addRating을 만들고 .push()를 통하여 ratings에 값을 추가할 수 있도록 만들기

  • extends를 통해 book class와 Movie class 만들기(properties, getters, methods 활용)
  • historyOfEverything 변수명으로 아래 properties를 통해 Book instance 만들기
    • Author: 'Bill Bryson'
    • Title: 'A Short History of Nearly Everything'
    • pages: 544
  • historyOfEverything 인스턴스에서 toggleCheckOutStatus() 호출하기
  • historyOfEverything 인스턴스에 isCheckedOut에 저장된 값 log 하기
  • 4, 5, 5 값을 입력하며 historyOfEverything 에서 .addRating() 세 번 호출하기
  • historyOfEverything에 .getAverageRating() log하기
  • speed 변수명으로 아래 properties를 통해 Movie instance 만들기
    • Director: 'Jan de Bont'
    • Title: 'Speed'
    • Runtime: 116
  • speed 인스턴스에서 toggleCheckOutStatus() 호출하기
  • speed 인스턴스에 isCheckedOut에 저장된 값 log 하기
  • 1, 1, 5 값을 입력하여 speed에서 .addRating() 세 번 호출하기
  • speed에 .getAverageRating() log하기
  • 위와 동일하게 CD 클래스를 만들어 보기
    • singer : 'Gemma Brown'
    • title : 'sunny day'
    • Track: 8
    • 변수명 : timeless
    • rating: 2,4,5


      [Output]
      true
      4.666666666666667
      true
      2.3333333333333335
      true
      3.6666666666666665
class Media {
  constructor(title) {
    this._title = title;
    this._isCheckedOut = false;
    this._ratings = [];
  }

  get title() {
    return this._title;
  }

  get isCheckedOut() {
    return this._isCheckedOut;
  }

  get ratings() {
    return this._ratings;
  }

  set isCheckedOut(value) {
    this._ischeckedOut = values; 
  }

  toggleCheckOutStatus() {
    this._isCheckedOut = !this._isCheckedOut;
  }

  getAverageRating() {
    let ratingSum = this._ratings.reduce((accumulator, rating) => accumulator + rating);
    return ratingSum / this.ratings.length;
  }

  addRating(value) {
    this._ratings.push(value);
  }
}

//creating Book class
class Book extends Media {
  constructor(author, title, pages) {
    super(title); //  Pass it any arguments that the parent constructor uses
    this._author = author;
    this._pages = pages;
  }
  get author() {
    return this._author;
  }
  get pages() {
    return this._pages;
  }
}

// creating Movie class
class Movie extends Media {
  constructor(director, title, runTime) {
    super(title);
    this._director = director;
    this._runTime = runTime;
  }
  get director() {
    return this._director;
  }
  get pages() {
    return this._pages;
  }
}

// historyOfEverything
const historyOfEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544);

historyOfEverything.toggleCheckOutStatus();
console.log(historyOfEverything.isCheckedOut);

historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
console.log(historyOfEverything.getAverageRating());


// speed
const speed = new Movie('Jan de Bont', 'Speed', 116);

speed.toggleCheckOutStatus();
console.log(speed.isCheckedOut);

speed.addRating(1);
speed.addRating(1);
speed.addRating(5);

console.log(speed.getAverageRating());

// creating CD
class CD extends Media {
  constructor(singer, title, track) {
    super(title);
    this._singer = singer;
    this._track = track;
  }
  get singer() {
    return this._singer;
  }
  get track() {
    return this._track;
  }
}

const timeless = new CD('Gemma Brown', 'Sunny day', 8);

timeless.toggleCheckOutStatus();
console.log(timeless.isCheckedOut);

timeless.addRating(2);
timeless.addRating(4);
timeless.addRating(5);

console.log(timeless.getAverageRating());
profile
파주

0개의 댓글