Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.7 kB
1
Indexable
Never
//без прайвит фийлдс и без декларирани фийлдс преди конструктора.
//за 1ва задача - doubly linked list.

// let input = [
//   '10',
//   'AddWish Electric Scooter 2000Z;1536.50;Stefan',
//   'AddWish Fortnite Skin;3000;Stefan',
//   'AddWish AMD Radeon;4099.99;Pesho',
//   'AddWish Apple AirPods;200000;Kiril',
//   'AddWish Socks;10000;Tosho',
//   'AddWish Swater;999;Stefan',
//   'FindWishesByChild Stefan',
//   'DeleteWishes Stefan',
//   'FindWishesByChild Stefan',
//   'FindWishesByPriceRange 100000;200000'
// ]

let input = [
  '8',
  'AddWish Electric Scooter 2000Z;3500.05;Rayko Petrov',
  'AddWish Fortnite Skin;3000;Rayko Petrov',
  'AddWish AMD Radeon;16400;Hristo',
  'AddWish Apple AirPods;21111.50;Nadya',
  'FindWishesByChild Rayko Petrov',
  'DeleteWishes Rayko Petrov',
  'FindWishesByChild Rayko Petrov',
  'FindWishesByPriceRange 5000;30000'
]



let print = console.log;
let gets = ((arr, index) => () => arr[index++])(input, 0);

class Wish {
  constructor(itemName, estimatedPrice, childName) {
    this.itemName = itemName;
    this.estimatedPrice = +estimatedPrice;
    this.childName = childName;
  }
}

class SantaList {

  constructor() {
    this.wishList = new Map();
  }

  addWish(itemName, estimatedPrice, childName) {

    if (this.wishList.has(childName)) {
      this.wishList.get(childName).push(new Wish(itemName, estimatedPrice, childName));
    } else {
      this.wishList.set(childName, [new Wish(itemName, estimatedPrice, childName)]);
    }

    print('Wish added')
  }

  deleteWish(childName) {
    if (this.wishList.has(childName)) {
      const wishesCount = this.wishList.get(childName).length;

      this.wishList.delete(childName);

      print(wishesCount + ' Wishes deleted');
    } else {
      print('No Wishes found');
    }
  }

  findWishesByPriceRange(fromPrice, toPrice) {
    const filterred = [...this.wishList.values()].flat().filter(wish => wish.estimatedPrice >= fromPrice && wish.estimatedPrice <= toPrice)
      .sort((a, b) => a.itemName.localeCompare(b.itemName));
    if (!filterred.length) {
      print('No Wishes found');
    } else {
      filterred.forEach(el => print(`{${el.itemName};${el.childName};${el.estimatedPrice.toFixed(2)}}`));
    }

  }

  findWishesByChild(childName) {
    if (this.wishList.has(childName)) {
      this.wishList.get(childName).slice().sort((a, b) => a.itemName.localeCompare(b.itemName))
        .forEach(el => {
          print(`{${el.itemName};${el.childName};${el.estimatedPrice.toFixed(2)}}`)
        });
    } else {
      print('No Wishes found');
    }
  }

}

let category = '';
let rest = [];
let itemName = '';
let childName = '';
let estimatedPrice = 0;
let fromPrice = 0;
let toPrice = 0;
let n = +gets();

const santalist = new SantaList();

for (let index = 0; index < n; index++) {
  [command, ...rest] = gets().split(' ');
  rest = rest.join(' ').split(';');

  if (command === 'AddWish') {
    itemName = rest[0];
    estimatedPrice = rest[1];
    childName = rest[2];
    santalist.addWish(itemName, estimatedPrice, childName);
  } else if (command === 'FindWishesByChild') {
    childName = rest[0];
    santalist.findWishesByChild(childName);
  } else if (command === 'FindWishesByPriceRange') {
    fromPrice = +rest[0];
    toPrice = +rest[1];
    santalist.findWishesByPriceRange(fromPrice, toPrice);
  } else if (command === 'DeleteWishes') {
    childName = rest[0];
    santalist.deleteWish(childName);
  } else {
    print('Such command does not exist');
  }
}