Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Wish {
- constructor(itemName, price, childName) {
- this.itemName = itemName;
- this.price = price;
- this.childName = childName;
- }
- }
- class Manager {
- constructor() {
- this.dataBase = new Map();
- }
- AddWish(itemName, price, childName) {
- if (this.dataBase.has(childName)) {
- this.dataBase.get(childName).push(new Wish(itemName, price, childName));
- } else {
- this.dataBase.set(childName, []);
- this.dataBase.get(childName).push(new Wish(itemName, price, childName));
- }
- return 'Wish added'
- }
- DeleteWishes(childName) {
- if (!this.dataBase.has(childName)
- || this.dataBase.get(childName).length === 0)
- return 'No Wishes found';
- const countWishes = this.dataBase.get(childName).length;
- this.dataBase.set(childName, []);
- return `${countWishes} Wishes deleted`
- }
- FindWishesByPriceRange(from, to) {
- const wishes = [...this.dataBase.values()]
- .flat(1)
- .filter(x => x.price >= +from && x.price <= +to)
- .sort((a, b) => {
- if (a.itemName > b.itemName) {
- return 1;
- } else if (a.itemName < b.itemName) {
- return -1;
- } else {
- return 0;
- }
- });
- if (wishes.length === 0)
- return 'No Wishes found';
- return wishes.map(x => `{${x.itemName};${x.childName};${Number(x.price).toFixed(2)}}`);
- }
- FindWishesByChild(childName) {
- if (!this.dataBase.get(childName))
- return 'No Wishes found'
- if (this.dataBase.get(childName).length === 0)
- return 'No Wishes found'
- const childWishes = this.dataBase.get(childName)
- .sort((a, b) => {
- if (a.itemName > b.itemName) {
- return 1;
- } else if (a.itemName < b.itemName) {
- return -1;
- } else {
- return 0;
- }
- })
- .map(x => `{${x.itemName};${x.childName};${Number(x.price).toFixed(2)}}`);
- return childWishes;
- }
- }
- const commandsCount = +gets()
- const santaManager = new Manager();
- for (let i = 1; i <= commandsCount; i++) {
- const arr = gets().split(' ');
- const command = String(arr.splice(0, 1));
- const answer = santaManager[command](...arr.join(' ').split(';'));
- Array.isArray(answer) ? answer.forEach(x => print(x)) : print(answer);
- }
Advertisement
Add Comment
Please, Sign In to add comment