Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LibraryCollection{
- constructor(capacity){
- this.capacity = capacity;
- this.books = [];
- }
- addBook(bookName, bookAuthor){
- if(this.books.length >= this.capacity){
- throw new Error("Not enough space in the collection.");
- }
- let bookObj = {
- bookName,
- bookAuthor,
- payed: false,
- };
- this.books.push(bookObj);
- return `The ${bookName}, with an author ${bookAuthor}, collect.`;
- }
- payBook(bookName){
- let bookIndex = this.books.findIndex(x => x.bookName == bookName);
- if(bookIndex == -1){
- throw new Error(`${bookName} is not in the collection.`);
- }
- if(this.books[bookIndex].payed == true){
- throw new Error(`${bookName} has already been paid.`);
- }
- this.books[bookIndex].payed = true;
- return `${bookName} has been successfully paid.`;
- }
- removeBook(bookName){
- let bookIndex = this.books.findIndex(x => x.bookName == bookName);
- if(bookIndex == -1){
- throw new Error("The book, you're looking for, is not found.");
- }
- if(this.books[bookIndex].payed == false){
- throw new Error(`${bookName} need to be paid before removing from the collection.`);
- }
- this.books.splice(bookIndex,1);
- return `${bookName} remove from the collection.`;
- }
- getStatistics(bookAuthor){
- let output = [];
- if(typeof bookAuthor == 'undefined'){
- let emptySlots = this.capacity - this.books.length;
- output.push(`The book collection has ${emptySlots} empty spots left.`);
- this.books = this.books.sort((a,b) => {
- return a.bookName.localeCompare(b.bookName);
- }).forEach(x => {
- if(x.payed == true){
- output.push(`${x.bookName} == ${x.bookAuthor} - Has Paid.`);
- }else{
- output.push(`${x.bookName} == ${x.bookAuthor} - Not Paid.`);
- }
- })
- return output.join('\n');
- }else{
- let bookAuthorIndex = this.books.findIndex(x => x.bookAuthor == bookAuthor);
- if(bookAuthorIndex == -1){
- throw new Error(`${bookAuthor} is not in the collection.`)
- }
- if(this.books[bookAuthorIndex].payed){
- return `${this.books[bookAuthorIndex].bookName} == ${this.books[bookAuthorIndex].bookAuthor} - Has Paid.`;
- }else{
- return `${this.books[bookAuthorIndex].bookName} == ${this.books[bookAuthorIndex].bookAuthor} - Not Paid.`;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement