Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LibraryCollection{
- constructor(capacity, books) {
- this.capacity = capacity ? capacity : 0;
- this.books = books ? books : [];
- }
- addBook(bookName, bookAuthor) {
- if (this.capacity <= this.books.length) {
- throw 'Not enough space in the collection.';
- }
- this.books.push({
- bookName,
- bookAuthor,
- payed: false
- });
- return `The ${bookName}, with an author ${bookAuthor}, collect.`;
- }
- payBook( bookName ) {
- if(!this.books.some(x => x.bookName == bookName)){
- throw new Error(`${bookName} is not in the collection.`);
- }
- else if(this.books.some(x => x.payed == true)) {
- throw new Error(`${bookName} has already been paid.`);
- }
- else{
- let book = this.books.some(x => x.bookName == bookName);
- book.payed = true;
- return `${bookName} has been successfully paid.`;
- }
- }
- removeBook(bookName) {
- if(!this.books.some(x => x.bookName == bookName)){
- throw new Error(`The book, you're looking for, is not found.`);
- }
- if (this.books.some(x => x.payed == false)) {
- throw new Error(`${bookName} need to be paid before removing from the collection.`);
- }else{
- this.books=this.books.filter(x => x.bookName != bookName);
- return `${bookName} remove from the collection.`;
- }
- }
- getStatistics(bookAuthor) {
- let result=[];
- if (!bookAuthor || bookAuthor === '') {
- result.push(`The book collection has ${this.capacity - this.books.length} empty slots left.`);
- this.books.sort(function(a, b) {
- var bookA = a.bookName.toUpperCase();
- var bookB = b.bookName.toUpperCase();
- return (bookA < bookB) ? -1 : (bookA > bookB) ? 1 : 0;
- });
- this.books.forEach(book => {
- result.push(`${book.bookName} == ${book.bookAuthor} - ${book.payed ? 'Has Paid' : 'Not Paid'}`);
- });
- } else {
- let bookFound = false;
- this.books.forEach(book => {
- if (book.bookAuthor === bookAuthor) {
- bookFound = true;
- result.push(`${book.bookName} == ${book.bookAuthor} - ${book.payed ? 'Has Paid' : 'Not Paid'}`);
- }
- });
- if (!bookFound) {
- throw `${bookAuthor} is not in the collection.`;
- }
- return result.join('\n');
- }
- }
- }
Add Comment
Please, Sign In to add comment