neZnam121

Untitled

Feb 19th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. class LibraryCollection{
  2.     constructor(capacity, books) {
  3.         this.capacity = capacity ? capacity : 0;
  4.         this.books = books ? books : [];
  5.       }
  6.  
  7.       addBook(bookName, bookAuthor) {
  8.           if (this.capacity <= this.books.length) {
  9.               throw 'Not enough space in the collection.';
  10.           }
  11.  
  12.           this.books.push({
  13.               bookName,
  14.               bookAuthor,
  15.               payed: false
  16.           });
  17.           return `The ${bookName}, with an author ${bookAuthor}, collect.`;
  18.       }
  19.  
  20.     payBook( bookName ) {
  21.         if(!this.books.some(x => x.bookName == bookName)){
  22.             throw new Error(`${bookName} is not in the collection.`);
  23.         }
  24.         else if(this.books.some(x => x.payed == true)) {
  25.             throw new Error(`${bookName} has already been paid.`);
  26.         }
  27.         else{
  28.             let book = this.books.some(x => x.bookName == bookName);
  29.             book.payed = true;
  30.             return `${bookName} has been successfully paid.`;
  31.         }
  32.     }
  33.     removeBook(bookName) {
  34.         if(!this.books.some(x => x.bookName == bookName)){
  35.             throw new Error(`The book, you're looking for, is not found.`);
  36.        }
  37.        if (this.books.some(x => x.payed == false)) {
  38.            throw new Error(`${bookName} need to be paid before removing from the collection.`);
  39.        }else{
  40.            this.books=this.books.filter(x => x.bookName != bookName);
  41.            return `${bookName} remove from the collection.`;
  42.        }
  43.    }
  44.    getStatistics(bookAuthor) {
  45.        let result=[];
  46.  
  47.        if (!bookAuthor || bookAuthor === '') {
  48.            result.push(`The book collection has ${this.capacity - this.books.length} empty slots left.`);
  49.  
  50.            this.books.sort(function(a, b) {
  51.                var bookA = a.bookName.toUpperCase();
  52.                var bookB = b.bookName.toUpperCase();
  53.                return (bookA < bookB) ? -1 : (bookA > bookB) ? 1 : 0;
  54.            });
  55.  
  56.            this.books.forEach(book => {
  57.                result.push(`${book.bookName} == ${book.bookAuthor} - ${book.payed ? 'Has Paid' : 'Not Paid'}`);
  58.            });
  59.        } else {
  60.            let bookFound = false;
  61.            this.books.forEach(book => {
  62.                if (book.bookAuthor === bookAuthor) {
  63.                    bookFound = true;
  64.                    result.push(`${book.bookName} == ${book.bookAuthor} - ${book.payed ? 'Has Paid' : 'Not Paid'}`);
  65.                }                
  66.            });
  67.  
  68.            if (!bookFound) {
  69.                throw `${bookAuthor} is not in the collection.`;
  70.            }
  71.  
  72.            return result.join('\n');
  73.        }
  74.    }
  75. }
  76.  
Add Comment
Please, Sign In to add comment