Vassil_Iliev

Santa's helper

Jun 13th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Wish {
  2.  
  3.     constructor(itemName, price, childName) {
  4.         this.itemName = itemName;
  5.         this.price = price;
  6.         this.childName = childName;
  7.     }
  8. }
  9.  
  10. class SantaSystem {
  11.  
  12.     constructor() {
  13.         this.map = new Map();
  14.     }
  15.  
  16.     AddWish(itemName, price, childName) {
  17.         const wish = new Wish(itemName, price, childName);
  18.  
  19.         if (this.map.has(childName)) {
  20.             this.map.get(childName).push(wish)
  21.         } else {
  22.             this.map.set(childName, [wish]);
  23.         }
  24.  
  25.         return 'Wish added'
  26.     }
  27.  
  28.     DeleteWishes(childName) {
  29.         if (this.map.has(childName)) {
  30.             const count = this.map.get(childName).length;
  31.             this.map.delete(childName);
  32.             return `${count} Wishes deleted`
  33.         }
  34.  
  35.         return 'No Wishes found'
  36.     }
  37.  
  38.     FindWishesByPriceRange(from, to) {
  39.         const result = [...this.map.values()]
  40.             .flat(1)
  41.             .filter(x => x.price <= to && x.price >= from)
  42.             .sort((a, b) => {
  43.                 if (a.itemName > b.itemName) {
  44.                     return 1;
  45.                 } else if (a.itemName < b.itemName) {
  46.                     return -1;
  47.                 } else {
  48.                     return 0;
  49.                 }
  50.             })
  51.  
  52.         if (result.length === 0) return 'No Wishes found';
  53.  
  54.         return result
  55.             .reduce((string, el) => {
  56.                 string += `{${el.itemName};${el.childName};${el.price.toFixed(2)}}` + '\n'
  57.                 return string;
  58.             }, '')
  59.             .trimEnd()
  60.     }
  61.  
  62.     FindWishesByChild(childName) {
  63.  
  64.         if (!this.map.has(childName)) {
  65.             return 'No Wishes found'
  66.         }
  67.  
  68.         const childWishes = this.map.get(childName);
  69.  
  70.         const result = childWishes
  71.             .sort((a, b) => {
  72.                 if (a.itemName > b.itemName) {
  73.                     return 1;
  74.                 } else if (a.itemName < b.itemName) {
  75.                     return -1;
  76.                 } else {
  77.                     return 0;
  78.                 }
  79.             })
  80.  
  81.         return result
  82.             .reduce((string, el) => {
  83.                 string += `{${el.itemName};${el.childName};${el.price.toFixed(2)}}` + '\n'
  84.                 return string;
  85.             }, '')
  86.             .trimEnd()
  87.     }
  88. }
  89.  
  90. const commandCount = +gets();
  91. const test = new SantaSystem();
  92.  
  93. for (let i = 0; i < commandCount; i++) {
  94.     const string = gets().split(' ')
  95.     const command = string[0];
  96.     const args = string.slice(1).join(' ').split(';');
  97.  
  98.     print(test[command](args[0], +args[1], args[2]));
  99. }
Advertisement
Add Comment
Please, Sign In to add comment