Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BookCollection{
- constructor(shelfGenre, room, shelfCapacity){
- if(room === 'livingRoom' || room === 'bedRoom' || room === 'closet'){
- this.shelfGenre = shelfGenre;
- this.room = room;
- this.shelfCapacity = shelfCapacity;
- this.shelf = [];
- } else {
- throw new Error(`Cannot have book shelf in ${room}`)
- }
- }
- addBook(bookName, bookAuthor, genre){
- let createBook = {name: bookName, author: bookAuthor, genre: genre};
- if(this.shelf.length < this.shelfCapacity) {
- if (genre !== undefined) {
- createBook.genre = genre;
- } else {
- createBook.genre = this.shelfGenre;
- }
- if (this.shelf.length < this.shelfCapacity) {
- this.shelf.push(createBook);
- } else {
- this.shelf.shift();
- this.shelf.push(createBook);
- }
- }
- this.shelf.sort((a, b) => a.author.localeCompare(b.author));
- }
- throwAwayBook(bookName){
- for (let i = 0; i < this.shelf.length; i++) {
- if(this.shelf[i].name === bookName){
- this.shelf.splice(i, 1);
- }
- }
- }
- showBooks(genre){
- let result = '';
- result += `Results for search "${genre}":\n`;
- for (let i = 0; i < this.shelf.length; i++) {
- if(this.shelf[i].genre === genre){
- result += ` \uD83D\uDCD6 ${this.shelf[i].author} - "${this.shelf[i].name}"\n`
- }
- }
- return result;
- }
- get shelfCondition(){
- let count = this.shelfCapacity - this.shelf.length;
- if(count > 0){
- return count;
- }
- return 0;
- }
- toString(){
- if(this.shelf.length === 0){
- return "It's an empty shelf";
- } else {
- let result = '';
- result += `"${this.shelfGenre}" shelf in ${this.room} contains: \n`;
- for (let obj of this.shelf) {
- result += ` \uD83D\uDCD6 "${obj.name}" - ${obj.author}\n`
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement