Advertisement
VDimov01

LibraryCollection

Feb 19th, 2022
176
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.         }
  11.  
  12.         let bookObj = {
  13.             bookName,
  14.             bookAuthor,
  15.             payed: false,
  16.         };
  17.  
  18.         this.books.push(bookObj);
  19.         return `The ${bookName}, with an author ${bookAuthor}, collect.`;
  20.     }
  21.  
  22.     payBook(bookName){
  23.         let bookIndex = this.books.findIndex(x => x.bookName == bookName);
  24.  
  25.         if(bookIndex == -1){
  26.             throw new Error(`${bookName} is not in the collection.`);
  27.         }
  28.        
  29.         if(this.books[bookIndex].payed == true){
  30.             throw new Error(`${bookName} has already been paid.`);
  31.         }
  32.  
  33.         this.books[bookIndex].payed = true;
  34.         return `${bookName} has been successfully paid.`;
  35.     }
  36.  
  37.     removeBook(bookName){
  38.         let bookIndex = this.books.findIndex(x => x.bookName == bookName);
  39.         if(bookIndex == -1){
  40.             throw new Error("The book, you're looking for, is not found.");
  41.         }
  42.         if(this.books[bookIndex].payed == false){
  43.             throw new Error(`${bookName} need to be paid before removing from the collection.`);
  44.         }
  45.  
  46.         this.books.splice(bookIndex,1);
  47.         return `${bookName} remove from the collection.`;
  48.     }
  49.  
  50.     getStatistics(bookAuthor){
  51.         let output = [];
  52.  
  53.         if(typeof bookAuthor == 'undefined'){
  54.             let emptySlots = this.capacity - this.books.length;
  55.             output.push(`The book collection has ${emptySlots} empty spots left.`);
  56.             this.books = this.books.sort((a,b) => {
  57.                 return a.bookName.localeCompare(b.bookName);
  58.             }).forEach(x => {
  59.                 if(x.payed == true){
  60.                     output.push(`${x.bookName} == ${x.bookAuthor} - Has Paid.`);
  61.                 }else{
  62.                     output.push(`${x.bookName} == ${x.bookAuthor} - Not Paid.`);
  63.                 }
  64.             })
  65.  
  66.             return output.join('\n');
  67.         }else{
  68.             let bookAuthorIndex = this.books.findIndex(x => x.bookAuthor == bookAuthor);
  69.  
  70.             if(bookAuthorIndex == -1){
  71.                 throw new Error(`${bookAuthor} is not in the collection.`)
  72.             }
  73.            
  74.             if(this.books[bookAuthorIndex].payed){
  75.                     return `${this.books[bookAuthorIndex].bookName} == ${this.books[bookAuthorIndex].bookAuthor} - Has Paid.`;
  76.                 }else{
  77.                     return `${this.books[bookAuthorIndex].bookName} == ${this.books[bookAuthorIndex].bookAuthor} - Not Paid.`;
  78.                 }
  79.         }
  80.  
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement