Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve() {
- class LibraryCollection {
- constructor(capacity) {
- this.capacity = capacity;
- this.books = [];
- }
- addBook(bookName, bookAuthor) {
- console.log(books);
- if (this.books.length == this.capacity) {
- return "Not enough space in the collection.";
- }
- else {
- let newbook =
- {
- bookName: bookName,
- bookAuthor: bookAuthor,
- Payed: false
- }
- this.books.push(newbook);
- return `The ${bookName}, with an author ${bookAuthor}, collect.`
- }
- };
- payBook(bookName) {
- let bookFound = false;
- let bookID = 0;
- for (let i = 0; i < this.books.length; i++) {
- if (this.books[i].bookName = bookName) {
- bookFound = true;
- bookID = i;
- break;
- }
- if (bookFound) {
- if (this.books[bookID].Payed == false) {
- this.books[bookID].Payed = true;
- return `${this.books[bookID].bookName} has been successfully paid.`;
- }
- else {
- throw `${this.books[bookID].bookName} has been successfully paid.`;
- }
- }
- else {
- throw `${this.books[bookID].bookName} is not in the collection.`;
- }
- }
- }
- removeBook(bookName) {
- let bookFound = false;
- let bookID = 0;
- for (let i = 0; i < this.books.length; i++) {
- if (this.books[i].bookName = bookName) {
- bookFound = true;
- bookID = i;
- break;
- }
- if (bookFound) {
- if (this.books[bookID].Payed == false) {
- // error
- throw `${this.books[bookID].bookName} need to be paid before removing from the collection.`;
- }
- else {
- // remove
- this.books.splice(bookID, 1);
- }
- }
- else {
- throw "The book, you're looking for, is not found."
- }
- }
- }
- getStatistics(bookAuthor) {
- if (arguments.length == 0) {
- return this.books;
- }
- else {
- let authorFound = false;
- for (book of this.books) {
- if (book.bookAuthor == bookAuthor) {
- authorFound = true; break;
- }
- }
- if (authorFound) {
- let emptySlots = this.capacity - this.books.length;
- if (emptySlots > -1) {
- console.log(`The book collection has ${emptySlots} empty spots left.`)
- }
- this.books.sort(function (a = a.bookName, b = b.bookName) {
- return a - b;
- });
- let Allbooks = '';
- for (book of this.books) {
- if (book.Payed == true) {
- Allbooks += (`${book.bookName} == ${book.bookAuthor} - Has Paid.`)
- }
- else {
- Allbooks += (`${book.bookName} == ${book.bookAuthor} - Not Paid.`)
- }
- }
- return Allbooks;
- }
- else {
- throw `${bookAuthor} is not in the collection.`;
- }
- }
- }
- }
- //TEST - MUST REMOVE BEFORE PUTTING IN JUDGE
- //----------------------------------------------------
- const library = new LibraryCollection(2)
- console.log(library.addBook('In Search of Lost Time', 'Marcel Proust'));
- console.log(library.addBook('Don Quixote', 'Miguel de Cervantes'));
- console.log(library.addBook('Ulysses', 'James Joyce'));
- //----------------------------------------------------
- }
- //TEST - MUST REMOVE BEFORE PUTTING IN JUDGE
- solve();
- //----------------------------------------------------
Add Comment
Please, Sign In to add comment