momo3141

Untitled

Mar 9th, 2023
116
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. let print = this.print || console.log;
  15. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  16. //Code
  17. class Wish {
  18.     constructor(item_name, estimated_price, child_name) {
  19.         this.itemName = item_name;
  20.         this.price = estimated_price;
  21.         this.childName = child_name
  22.     }
  23.     toString() {
  24.         return `{${this.itemName};${this.childName};${this.price.toFixed(2)}}`;
  25.       }
  26. }
  27. class WishList {
  28.     constructor() {
  29.         this.wishes = [];
  30.     }
  31.     addWish(wish) {
  32.         this.wishes.push(wish)
  33.         console.log('Wish added')
  34.     }
  35.     deleteWishes(childName) {
  36.         const deletedWishes = this.wishes.filter(w => w.childName === childName);
  37.         this.wishes = this.wishes.filter(w => w.childName !== childName);
  38.         console.log(`${deletedWishes.length} Wishes deleted`);
  39.       }
  40.     findWishesByPriceRange(fromPrice, toPrice) {
  41.         const matchingWishes = this.wishes.filter(w => w.price >= fromPrice && w.price <= toPrice);
  42.         if (matchingWishes.length === 0) {
  43.           console.log('No Wishes found');
  44.         }
  45.         matchingWishes.sort((a, b) => a.itemName.localeCompare(b.itemName));
  46.         console.log(matchingWishes.map(w => w.toString()).join('\n'));
  47.       }
  48.       findWishesByChild(childName) {
  49.         const matchingWishes = this.wishes.filter(w => w.childName === childName);
  50.         if (matchingWishes.length === 0) {
  51.           console.log('No Wishes found');
  52.         } else {
  53.         matchingWishes.sort((a, b) => a.itemName.localeCompare(b.itemName));
  54.         console.log(matchingWishes.map(w => w.toString()).join('\n'));}
  55.       }
  56. }
  57. // const commandLine = 'AddWish Electric Scooter 2000Z;1536.50;Stefan'
  58. // const parts = commandLine.split(' ')
  59. // console.log(parts.shift())
  60. // console.log(parts.join(' ').split(';'))
  61.  
  62.  
  63.  
  64.  
  65. const executeCommand = (commandLine, wishlist) => {
  66.     const parts = commandLine.split(' ');
  67.     const command = parts.shift();
  68.    
  69.          if(command.toLowerCase() === 'addwish'){
  70.             const  itemName = parts.join(' ').split(';')[0];
  71.             const  estimatedPrice = parseFloat(parts.join(' ').split(';')[1]);
  72.             const  childName = parts.join(' ').split(';')[2]
  73.             const wish = new Wish (itemName, estimatedPrice, childName);
  74.             wishlist.addWish(wish)
  75.            
  76.          } else if (command.toLowerCase() === 'deletewishes'){        
  77.             const child_name = parts[0];
  78.             wishlist.deleteWishes(child_name);
  79.          } else if (command.toLowerCase() === 'findwishesbypricerange'){
  80.             const priceLow = parseFloat(parts.join(' ').split(';')[0]);
  81.             const priceUpper = parseFloat(parts.join(' ').split(';')[1])
  82.             wishlist.findWishesByPriceRange(priceLow, priceUpper);
  83.          } else if (command.toLowerCase() === 'findwishesbychild') {
  84.             const childName = parts[0]
  85.             wishlist.findWishesByChild(childName)
  86.          }
  87. }
  88. const wishList = new WishList();
  89. let N = Number(gets())
  90. console.log(N);
  91. for (let i = 1 ; i <= N; i++) {
  92.     let commandLine = gets();
  93.     executeCommand(commandLine, wishList)
  94. }
Advertisement
Add Comment
Please, Sign In to add comment