momo3141

library

Feb 15th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { libraryShelf } from '../common/library-shelf.js';
  2.  
  3. export class Library {
  4.     #shelves = {
  5.       1: [],
  6.       2: [],
  7.       3: [],
  8.       4: [],
  9.       99: [],
  10.     };
  11.     set _shelves(obj) {
  12.       for (const keys in obj) {
  13.         const shelf = keys;
  14.         const book = obj[keys][0];
  15.         this.addBook(book, shelf);
  16.       }
  17.      
  18.  
  19.     }
  20.     get _shelves() {
  21.       return this.#shelves;
  22.     }
  23.  
  24.     addBook(book, shelf) {
  25.       if (!libraryShelf[shelf]) {
  26.         throw new Error('Not a valid shelf.');
  27.       }
  28.       this.#shelves[shelf].push(book);
  29.     }
  30.     get _shelves() {
  31.       return this.#shelves;
  32.     }
  33.     printBooks() {
  34.       let outPut = '';
  35.       // eslint-disable-next-line guard-for-in, prefer-const
  36.       for ( let keys in this.#shelves) {
  37.         if (this.#shelves[keys].length !== 0) {
  38.           outPut += ('==' + libraryShelf[keys] + '==' +'\n');
  39.           this.#shelves[keys].map(el =>{
  40.             outPut += el.title + ', by ' + el.authors + ', ' + el.pages + ' pages' + '\n';
  41.           });
  42.         }
  43.       }
  44.       if (outPut === '') {
  45.         return 'No books added';
  46.       } else {
  47.         return outPut;
  48.       }
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment