mr_anastasov

library

May 19th, 2023
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* eslint-disable linebreak-style */
  2. import { libraryShelf } from '../common/library-shelf.js';
  3.  
  4. export class Library {
  5.  
  6.     _shelves;
  7.     constructor() {
  8.       this._shelves = {};
  9.     }
  10.  
  11.  
  12.     addBook(book, shelf) {
  13.       if (!Object.values(libraryShelf).includes(shelf)) {
  14.         throw Error('The shelf value is invalid');
  15.       }
  16.  
  17.       if (!(shelf in this._shelves)) {
  18.         this._shelves[shelf] = [];
  19.       }
  20.  
  21.       this._shelves[shelf].push(book);
  22.     }
  23.  
  24.     printBooks() {
  25.       const title = '---Task Board ---\nTasks:\n';
  26.  
  27.       if (Object.keys(this._shelves).length === 0) {
  28.  
  29.         console.log(title + 'No books added');
  30.  
  31.       } else {
  32.  
  33.         let shelfAsString = '';
  34.         // eslint-disable-next-line guard-for-in
  35.         for (const key in this._shelves) {
  36.           const element = this._shelves[key];
  37.           shelfAsString += ` == ${libraryShelf[key]} == \n`;
  38.  
  39.           element.forEach(el => {
  40.             shelfAsString += `${el.title}, by ${el.authors}, ${el.pages} \n`;
  41.           });
  42.  
  43.           // if (Object.hasOwn(this._shelves, key)) {
  44.           //   element.map(el => shelfAsString += console.log(el.toString()));
  45.           //   // shelfAsString += this._shelves[key].map(el => el.toString());
  46.           // }
  47.         }
  48.         console.log(title + shelfAsString);
  49.       }
  50.     }
  51.  
  52. };
  53.  
Advertisement
Add Comment
Please, Sign In to add comment