anagreg

Santa Helper

Sep 8th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let input = [
  2.     '10',
  3. 'AddWish Electric Scooter 2000Z;1536.50;Stefan',
  4. 'AddWish Fortnite Skin;3000;Stefan',
  5. 'AddWish AMD Radeon;4099.99;Pesho',
  6. 'AddWish Apple AirPods;200000;Kiril',
  7. 'AddWish Socks;10000;Tosho',
  8. 'AddWish Swater;999;Stefan',
  9. 'FindWishesByChild Stefan',
  10. 'DeleteWishes Stefan',
  11. 'FindWishesByChild Stefan',
  12. 'FindWishesByPriceRange 100000;200000'
  13.   ];
  14.  
  15. let print = this.print || console.log;
  16. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  17.  
  18. class SantaHelper {
  19.   constructor() {
  20.     this.wishes = [];
  21.   }
  22.  
  23.   addWish(itemName, estimatedPrice, childName) {
  24.     this.wishes.push({ item: itemName, price: estimatedPrice, child: childName });
  25.     return "Wish added";
  26.   }
  27.  
  28.   deleteWishes(childName) {
  29.     const initialCount = this.wishes.length;
  30.     this.wishes = this.wishes.filter((wish) => wish.child !== childName);
  31.     const deletedCount = initialCount - this.wishes.length;
  32.     if (deletedCount > 0) {
  33.       return `${deletedCount} Wishes deleted`;
  34.     } else {
  35.       return "No Wishes found";
  36.     }
  37.   }
  38.  
  39.   findWishesByPriceRange(fromPrice, toPrice) {
  40.     const foundWishes = this.wishes.filter(
  41.       (wish) => wish.price >= fromPrice && wish.price <= toPrice
  42.     );
  43.     if (foundWishes.length > 0) {
  44.       foundWishes.sort((a, b) => a.item.localeCompare(b.item));
  45.       const result = foundWishes.map((wish) => {
  46.         return `{${wish.item};${wish.child};${wish.price.toFixed(2)}}`;
  47.       });
  48.       return result.join("\n");
  49.     } else {
  50.       return "No Wishes found";
  51.     }
  52.   }
  53.   findWishesByChild(childName) {
  54.     const foundWishes = this.wishes.filter((wish) => wish.child === childName);
  55.     if (foundWishes.length > 0) {
  56.       foundWishes.sort((a, b) => a.item.localeCompare(b.item));
  57.       const result = foundWishes.map((wish) => {
  58.         return `{${wish.item};${wish.child};${wish.price.toFixed(2)}}`;
  59.       });
  60.       return result.join("\n");
  61.     } else {
  62.       return "No Wishes found";
  63.     }
  64.   }
  65.  
  66.   processCommands(commands) {
  67.     const output = [];
  68.  
  69.     for (const command of commands) {
  70.       const [action, ...args] = command.split(" ");
  71.  
  72.       switch (action) {
  73.         case "AddWish":
  74.             const [itemName, estimatedPrice, childName] = args.join(" ").split(";");
  75.             output.push(this.addWish(itemName, Number(estimatedPrice), childName));
  76.           break;
  77.         case "DeleteWishes":
  78.           const [deletedChildName] = args.join(' ').split(';');
  79.           output.push(this.deleteWishes(deletedChildName));
  80.           break;
  81.         case "FindWishesByPriceRange":
  82.           const [fromPrice, toPrice] = args.join(' ').split(';');
  83.           output.push(this.findWishesByPriceRange(Number(fromPrice), Number(toPrice)));
  84.           break;
  85.         case "FindWishesByChild":
  86.           const [childNameToFind] = args.join(' ').split(';');
  87.           output.push(this.findWishesByChild(childNameToFind));
  88.           break;
  89.         default:
  90.           break;
  91.       }
  92.     }
  93.     return output.join("\n");
  94.   }
  95. }
  96.  
  97. const santaHelper = new SantaHelper();
  98. const N = Number(gets());
  99. const commands = [];
  100.  
  101. for (let i = 0; i < N; i++) {
  102.   commands.push(gets());
  103. }
  104.  
  105. const result = santaHelper.processCommands(commands);
  106. print(result);
Advertisement
Add Comment
Please, Sign In to add comment