Advertisement
Niicksana

02. Library Collection

Jun 22nd, 2022 (edited)
1,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class LibraryCollection {
  2.     constructor(capacity) {
  3.         this.capacity = capacity;
  4.         this.books = [];
  5.     }
  6.  
  7.     addBook(bookName, bookAuthor) {
  8.         if (this.books.length == this.capacity) {
  9.             throw new Error("Not enough space in the collection.");
  10.         } else {
  11.             this.books.push({
  12.                 bookName,
  13.                 bookAuthor,
  14.                 payed: false,
  15.             });
  16.  
  17.             return `The ${bookName}, with an author ${bookAuthor}, collect.`;
  18.         }
  19.     }
  20.  
  21.     payBook(bookName) {
  22.         let book = this.books.find((b) => b.bookName == bookName);
  23.  
  24.         if (!book) {
  25.             throw new Error(`${bookName} is not in the collection.`);
  26.         }
  27.  
  28.         // check if book is alredy paid
  29.         if (book.payed) {
  30.             throw new Error(`${bookName} has already been paid.`);
  31.         }
  32.  
  33.         book.payed = true;
  34.         return `${bookName} has been successfully paid.`;
  35.     }
  36.  
  37.     removeBook(bookName) {
  38.         let book = this.books.find((b) => b.bookName == bookName);
  39.         if (!book) {
  40.             throw new Error("The book, you're looking for, is not found.");
  41.         }
  42.    
  43.         if (book.payed == false) {
  44.             throw new Error(
  45.                 `${bookName} need to be paid before removing from the collection.`
  46.             );
  47.         } else {
  48.             for (const b of this.books) {
  49.                 delete book.bookName;
  50.                 return `${bookName} remove from the collection.`;
  51.             }
  52.         }
  53.     }
  54.  
  55.     getStatistics(bookAuthor) {
  56.         let isPayed;
  57.  
  58.         if (bookAuthor) {
  59.             if (!this.books.find(b => b.bookAuthor == bookAuthor)) {
  60.                 throw new Error(`${bookAuthor} is not in the collection.`);
  61.             }
  62.            
  63.             // you need to return array of all books by given author
  64.             const booksByAuthor = [];
  65.  
  66.             this.books.map(b => b.bookAuthor == bookAuthor);
  67.  
  68.             for (const book of this.books) {
  69.                 if (book.payed) {
  70.                     isPayed = "Has Paid";
  71.                 } else {
  72.                     isPayed = "Not Paid";
  73.                 }
  74.  
  75.                 booksByAuthor.push(
  76.                     `${book.bookName} == ${book.bookAuthor} - ${isPayed}.`
  77.                 );
  78.             }
  79.  
  80.             return booksByAuthor.join("\n");
  81.         } else {
  82.             let resultArr = [
  83.                 `The book collection has ${this.capacity - this.books.length
  84.                 } empty spots left.`,
  85.             ];
  86.             let sortedNames = this.books.sort((a, b) =>
  87.                 a.bookName.localeCompare(b.bookName)
  88.             );
  89.             for (const sortedBook of sortedNames) {
  90.                 if (sortedBook.payed) {
  91.                     isPayed = "Has Paid";
  92.                 } else {
  93.                     isPayed = "Not Paid";
  94.                 }
  95.                 resultArr.push(
  96.                     `${sortedBook.bookName} == ${sortedBook.bookAuthor} - ${isPayed}.`
  97.                 );
  98.             }
  99.             return resultArr.join("\n");
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement