Stan0033

Untitled

Feb 19th, 2022 (edited)
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     class LibraryCollection {
  3.         constructor(capacity) {
  4.             this.capacity = capacity;
  5.             this.books = [];
  6.            
  7.  
  8.  
  9.         }
  10.         addBook(bookName, bookAuthor) {
  11.             console.log(books);
  12.             if (this.books.length == this.capacity) {
  13.                 return "Not enough space in the collection.";
  14.             }
  15.             else {
  16.                 let newbook =
  17.                 {
  18.                     bookName: bookName,
  19.                     bookAuthor: bookAuthor,
  20.                     Payed: false
  21.                 }
  22.                 this.books.push(newbook);
  23.                 return `The ${bookName}, with an author ${bookAuthor}, collect.`
  24.             }
  25.         };
  26.  
  27.  
  28.         payBook(bookName) {
  29.             let bookFound = false;
  30.             let bookID = 0;
  31.             for (let i = 0; i < this.books.length; i++) {
  32.                 if (this.books[i].bookName = bookName) {
  33.                     bookFound = true;
  34.                     bookID = i;
  35.                     break;
  36.                 }
  37.                 if (bookFound) {
  38.                     if (this.books[bookID].Payed == false) {
  39.                         this.books[bookID].Payed = true;
  40.                         return `${this.books[bookID].bookName} has been successfully paid.`;
  41.                     }
  42.                     else {
  43.                         throw `${this.books[bookID].bookName} has been successfully paid.`;
  44.                     }
  45.                 }
  46.                 else {
  47.                     throw `${this.books[bookID].bookName} is not in the collection.`;
  48.                 }
  49.             }
  50.         }
  51.         removeBook(bookName) {
  52.             let bookFound = false;
  53.             let bookID = 0;
  54.             for (let i = 0; i < this.books.length; i++) {
  55.                 if (this.books[i].bookName = bookName) {
  56.                     bookFound = true;
  57.                     bookID = i;
  58.                     break;
  59.                 }
  60.                 if (bookFound) {
  61.                     if (this.books[bookID].Payed == false) {
  62.                         // error
  63.                         throw `${this.books[bookID].bookName} need to be paid before removing from the collection.`;
  64.                     }
  65.                     else {
  66.                         // remove
  67.                         this.books.splice(bookID, 1);
  68.                     }
  69.                 }
  70.                 else {
  71.                     throw "The book, you're looking for, is not found."
  72.                 }
  73.             }
  74.         }
  75.         getStatistics(bookAuthor) {
  76.  
  77.  
  78.             if (arguments.length == 0) {
  79.                 return this.books;
  80.             }
  81.             else {
  82.                 let authorFound = false;
  83.                 for (book of this.books) {
  84.                     if (book.bookAuthor == bookAuthor) {
  85.                         authorFound = true; break;
  86.                     }
  87.                 }
  88.                 if (authorFound) {
  89.                     let emptySlots = this.capacity - this.books.length;
  90.                     if (emptySlots > -1) {
  91.                         console.log(`The book collection has ${emptySlots} empty spots left.`)
  92.                     }
  93.  
  94.                     this.books.sort(function (a = a.bookName, b = b.bookName) {
  95.                         return a - b;
  96.                     });
  97.                     let Allbooks = '';
  98.                     for (book of this.books) {
  99.                         if (book.Payed == true) {
  100.                             Allbooks += (`${book.bookName} == ${book.bookAuthor} - Has Paid.`)
  101.                         }
  102.                         else {
  103.                             Allbooks += (`${book.bookName} == ${book.bookAuthor} - Not Paid.`)
  104.                         }
  105.                     }
  106.                     return Allbooks;
  107.                 }
  108.                 else {
  109.                     throw `${bookAuthor} is not in the collection.`;
  110.                 }
  111.  
  112.             }
  113.         }
  114.     }
  115.      //TEST - MUST REMOVE BEFORE PUTTING IN JUDGE
  116.      //----------------------------------------------------
  117.      const library = new LibraryCollection(2)
  118. console.log(library.addBook('In Search of Lost Time', 'Marcel Proust'));
  119. console.log(library.addBook('Don Quixote', 'Miguel de Cervantes'));
  120. console.log(library.addBook('Ulysses', 'James Joyce'));
  121.  //----------------------------------------------------
  122. }
  123.  //TEST - MUST REMOVE BEFORE PUTTING IN JUDGE
  124. solve();
  125.  //----------------------------------------------------
  126.  
  127.  
Add Comment
Please, Sign In to add comment