AreWe

Някао рандъм домашно

Jun 14th, 2020
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Library {
  2.  
  3.     constructor(libraryID = '') {
  4.         this.libraryID = libraryID;
  5.         this.books = [];
  6.         this.authors = {};
  7.         this.publishers = {};
  8.     }
  9.  
  10.     addBook(title = '', firstName = '', lastName = '', publisher = '', year = 0) {
  11.         const book = {
  12.             title,
  13.             author: {
  14.                 firstName,
  15.                 lastName
  16.             },
  17.             publisher,
  18.             year
  19.         };
  20.  
  21.         this.books.push(book);
  22.  
  23.         const authorID = `${firstName}${lastName}`;
  24.         if(!this.authors[authorID]) {
  25.             this.authors[authorID] = {
  26.                 firstName,
  27.                 lastName,
  28.                 publishedCount: 1,
  29.                 books: [ book ]
  30.             }
  31.         } else {
  32.             this.authors[authorID].publishedCount++;
  33.             this.authors[authorID].books.push(book);
  34.         }
  35.  
  36.         if(!this.publishers[publisher]) {
  37.             this.publishers[publisher] = {
  38.                 publishedCount: 1,
  39.                 authors: new Set([authorID])
  40.             };
  41.         } else {
  42.             this.publishers[publisher].publishedCount++;
  43.             this.publishers[publisher].authors.add(authorID);
  44.         }
  45.     }
  46.  
  47.     getBooksCount() {
  48.         return this.books.length;
  49.     }
  50.  
  51.     getAuthorsCount() {
  52.         return Object.keys(this.authors).length;
  53.     }
  54.  
  55.     getPublishersCount() {
  56.         return Object.keys(this.publishers).length;
  57.     }
  58.  
  59.     // издателството с най-много книги
  60.     getTopPublisher() {
  61.         return Object
  62.             .entries(this.publishers)
  63.             .sort((a, b) => b[1].publishedCount - a[1].publishedCount)
  64.             [0];
  65.     }
  66.  
  67.     // изтелствата, които имат издадена само 1 книга
  68.     getLowestPublishers() {
  69.         return Object
  70.             .entries(this.publishers)
  71.             .filter(p => p[1].publishedCount === 1);
  72.     }
  73.  
  74.     // издателството, което е издало книги на най-много различни автори
  75.     getMostPopularPublisher() {
  76.         return Object
  77.             .entries(this.publishers)
  78.             .sort((a, b) => b[1].authors.size - a[1].authors.size)
  79.             [0];
  80.     }
  81.  
  82.     // книги от автор подредени по фамилия? че то един автор да няма 100 фамилии? :D
  83.     getBooksByAuthor(firstName = '', lastName = '') {
  84.  
  85.     }
  86.  
  87.     // всички автори, които имат повече от 1 издадена книга в една и съща година
  88.     // аре просто да позлваме database за тая работа ? :D
  89. }
  90.  
  91. const library = new Library('01652646-fb48-46bc-93ef-3bd532418b92');
  92. library.addBook('Косачи', 'Нийл', 'Шустърман', 'Orange-Books', 2018);
  93. library.addBook('Бурята', 'Нийл', 'Шустърман', 'Orange-Books', 2018);
  94. library.addBook('Косачи', 'Нийл', 'Шустърман', 'Orange-Books', 2018);
  95. library.addBook('Ангелски свят', 'Сюзън', 'Ий', 'Емас', 2016);
  96. library.addBook('Войната на маковете', 'Р. Ф.', 'Куанг', 'Orange-Books', 2019);
  97. console.log(library.getBooksCount());
  98. console.log('------------------------------');
  99. console.log(library.getAuthorsCount());
  100. console.log('------------------------------');
  101. console.log(library.getPublishersCount());
  102. console.log('------------------------------');
  103. console.log(library.getTopPublisher());
  104. console.log('------------------------------');
  105. console.log(library.getLowestPublishers());
  106. console.log('------------------------------');
  107. console.log(library.getMostPopularPublisher());
  108. console.log('------------------------------');
Advertisement
Add Comment
Please, Sign In to add comment