Vassil_Iliev

task1

May 30th, 2022
72
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 Manager {
  11.  
  12.     constructor() {
  13.         this.dataBase = new Map();
  14.     }
  15.  
  16.     AddWish(itemName, price, childName) {
  17.         if (this.dataBase.has(childName)) {
  18.             this.dataBase.get(childName).push(new Wish(itemName, price, childName));
  19.         } else {
  20.             this.dataBase.set(childName, []);
  21.             this.dataBase.get(childName).push(new Wish(itemName, price, childName));
  22.         }
  23.  
  24.         return 'Wish added'
  25.     }
  26.  
  27.     DeleteWishes(childName) {
  28.         if (!this.dataBase.has(childName)
  29.             || this.dataBase.get(childName).length === 0)
  30.             return 'No Wishes found';
  31.  
  32.         const countWishes = this.dataBase.get(childName).length;
  33.  
  34.         this.dataBase.set(childName, []);
  35.  
  36.         return `${countWishes} Wishes deleted`
  37.     }
  38.  
  39.     FindWishesByPriceRange(from, to) {
  40.         const wishes = [...this.dataBase.values()]
  41.             .flat(1)
  42.             .filter(x => x.price >= +from && x.price <= +to)
  43.             .sort((a, b) => {
  44.                 if (a.itemName > b.itemName) {
  45.                     return 1;
  46.                 } else if (a.itemName < b.itemName) {
  47.                     return -1;
  48.                 } else {
  49.                     return 0;
  50.                 }
  51.             });
  52.  
  53.         if (wishes.length === 0)
  54.             return 'No Wishes found';
  55.  
  56.         return wishes.map(x => `{${x.itemName};${x.childName};${Number(x.price).toFixed(2)}}`);
  57.     }
  58.  
  59.     FindWishesByChild(childName) {
  60.         if (!this.dataBase.get(childName))
  61.             return 'No Wishes found'
  62.  
  63.         if (this.dataBase.get(childName).length === 0)
  64.             return 'No Wishes found'
  65.  
  66.         const childWishes = this.dataBase.get(childName)
  67.             .sort((a, b) => {
  68.                 if (a.itemName > b.itemName) {
  69.                     return 1;
  70.                 } else if (a.itemName < b.itemName) {
  71.                     return -1;
  72.                 } else {
  73.                     return 0;
  74.                 }
  75.             })
  76.             .map(x => `{${x.itemName};${x.childName};${Number(x.price).toFixed(2)}}`);
  77.  
  78.         return childWishes;
  79.     }
  80. }
  81.  
  82. const commandsCount = +gets()
  83.  
  84. const santaManager = new Manager();
  85.  
  86. for (let i = 1; i <= commandsCount; i++) {
  87.     const arr = gets().split(' ');
  88.     const command = String(arr.splice(0, 1));
  89.     const answer = santaManager[command](...arr.join(' ').split(';'));
  90.  
  91.     Array.isArray(answer) ? answer.forEach(x => print(x)) : print(answer);
  92. }
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment