momo3141

1st Task

Mar 9th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Wish {
  2.     constructor(item_name, estimated_price, child_name) {
  3.         this.itemName = item_name;
  4.         this.price = estimated_price;
  5.         this.childName = child_name
  6.     }
  7.     toString() {
  8.         return `{${this.itemName};${this.childName};${this.price.toFixed(2)}}`;
  9.       }
  10. }
  11. class WishList {
  12.     constructor() {
  13.         this.wishes = [];
  14.     }
  15.     addWish(wish) {
  16.         this.wishes.push(wish)
  17.         console.log('Wish added')
  18.     }
  19.     deleteWishes(childName) {
  20.         const deletedWishes = this.wishes.filter(w => w.childName === childName);
  21.         this.wishes = this.wishes.filter(w => w.childName !== childName);
  22.         if(deletedWishes.length === 0 ) {
  23.         console.log('No Wishes found') }
  24.         else{
  25.             console.log(`${deletedWishes.length} Wishes deleted`);
  26.         }
  27.       }
  28.     findWishesByPriceRange(fromPrice, toPrice) {
  29.         const matchingWishes = this.wishes.filter(w => w.price >= fromPrice && w.price <= toPrice);
  30.         if (matchingWishes.length === 0) {
  31.           console.log('No Wishes found');
  32.         }
  33.         matchingWishes.sort((a, b) => a.itemName.localeCompare(b.itemName));
  34.         console.log(matchingWishes.map(w => w.toString()).join('\n'));
  35.       }
  36.       findWishesByChild(childName) {
  37.         const matchingWishes = this.wishes.filter(w => w.childName === childName);
  38.         if (matchingWishes.length === 0) {
  39.           console.log('No Wishes found');
  40.         } else {
  41.         matchingWishes.sort((a, b) => a.itemName.localeCompare(b.itemName));
  42.         console.log(matchingWishes.map(w => w.toString()).join('\n'));}
  43.       }
  44. }
  45.  
  46.  
  47.  
  48.  
  49. const executeCommand = (commandLine, wishlist) => {
  50.     const parts = commandLine.split(' ');
  51.     const command = parts.shift();
  52.    
  53.          if(command.toLowerCase() === 'addwish'){
  54.             const  itemName = parts.join(' ').split(';')[0];
  55.             const  estimatedPrice = parseFloat(parts.join(' ').split(';')[1]);
  56.             const  childName = parts.join(' ').split(';')[2]
  57.             const wish = new Wish (itemName, estimatedPrice, childName);
  58.             wishlist.addWish(wish)
  59.            
  60.          } else if (command.toLowerCase() === 'deletewishes'){        
  61.             const child_name = parts.join(' ');
  62.             wishlist.deleteWishes(child_name);
  63.          } else if (command.toLowerCase() === 'findwishesbypricerange'){
  64.             const priceLow = parseFloat(parts.join(' ').split(';')[0]);
  65.             const priceUpper = parseFloat(parts.join(' ').split(';')[1])
  66.             wishlist.findWishesByPriceRange(priceLow, priceUpper);
  67.          } else if (command.toLowerCase() === 'findwishesbychild') {
  68.             const childName = parts.join(' ')
  69.             wishlist.findWishesByChild(childName)
  70.          }
  71. }
  72. const wishList = new WishList();
  73. let N = Number(gets())
  74.  
  75. for (let i = 1 ; i <= N; i++) {
  76.     let commandLine = gets();
  77.     executeCommand(commandLine, wishList)
  78. }
Advertisement
Add Comment
Please, Sign In to add comment