Advertisement
tochka

Untitled

Jul 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. class BookCollection{
  2. constructor(shelfGenre, room, shelfCapacity){
  3. if(room === 'livingRoom' || room === 'bedRoom' || room === 'closet'){
  4. this.shelfGenre = shelfGenre;
  5. this.room = room;
  6. this.shelfCapacity = shelfCapacity;
  7. this.shelf = [];
  8. } else {
  9. throw new Error(`Cannot have book shelf in ${room}`)
  10. }
  11. }
  12.  
  13. addBook(bookName, bookAuthor, genre){
  14. let createBook = {name: bookName, author: bookAuthor, genre: genre};
  15. if(this.shelf.length < this.shelfCapacity) {
  16. if (genre !== undefined) {
  17. createBook.genre = genre;
  18. } else {
  19. createBook.genre = this.shelfGenre;
  20. }
  21. if (this.shelf.length < this.shelfCapacity) {
  22. this.shelf.push(createBook);
  23. } else {
  24. this.shelf.shift();
  25. this.shelf.push(createBook);
  26. }
  27. }
  28.  
  29. this.shelf.sort((a, b) => a.author.localeCompare(b.author));
  30. }
  31.  
  32. throwAwayBook(bookName){
  33. for (let i = 0; i < this.shelf.length; i++) {
  34. if(this.shelf[i].name === bookName){
  35. this.shelf.splice(i, 1);
  36. }
  37. }
  38. }
  39.  
  40. showBooks(genre){
  41. let result = '';
  42. result += `Results for search "${genre}":\n`;
  43. for (let i = 0; i < this.shelf.length; i++) {
  44. if(this.shelf[i].genre === genre){
  45. result += ` \uD83D\uDCD6 ${this.shelf[i].author} - "${this.shelf[i].name}"\n`
  46. }
  47. }
  48. return result;
  49. }
  50.  
  51. get shelfCondition(){
  52. let count = this.shelfCapacity - this.shelf.length;
  53. if(count > 0){
  54. return count;
  55. }
  56. return 0;
  57. }
  58.  
  59. toString(){
  60. if(this.shelf.length === 0){
  61. return "It's an empty shelf";
  62. } else {
  63. let result = '';
  64. result += `"${this.shelfGenre}" shelf in ${this.room} contains: \n`;
  65. for (let obj of this.shelf) {
  66. result += ` \uD83D\uDCD6 "${obj.name}" - ${obj.author}\n`
  67. }
  68. return result;
  69. }
  70. }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement