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 SantaSystem {
- constructor() {
- this.map = new Map();
- }
- AddWish(itemName, price, childName) {
- const wish = new Wish(itemName, price, childName);
- if (this.map.has(childName)) {
- this.map.get(childName).push(wish)
- } else {
- this.map.set(childName, [wish]);
- }
- return 'Wish added'
- }
- DeleteWishes(childName) {
- if (this.map.has(childName)) {
- const count = this.map.get(childName).length;
- this.map.delete(childName);
- return `${count} Wishes deleted`
- }
- return 'No Wishes found'
- }
- FindWishesByPriceRange(from, to) {
- const result = [...this.map.values()]
- .flat(1)
- .filter(x => x.price <= to && x.price >= from)
- .sort((a, b) => {
- if (a.itemName > b.itemName) {
- return 1;
- } else if (a.itemName < b.itemName) {
- return -1;
- } else {
- return 0;
- }
- })
- if (result.length === 0) return 'No Wishes found';
- return result
- .reduce((string, el) => {
- string += `{${el.itemName};${el.childName};${el.price.toFixed(2)}}` + '\n'
- return string;
- }, '')
- .trimEnd()
- }
- FindWishesByChild(childName) {
- if (!this.map.has(childName)) {
- return 'No Wishes found'
- }
- const childWishes = this.map.get(childName);
- const result = childWishes
- .sort((a, b) => {
- if (a.itemName > b.itemName) {
- return 1;
- } else if (a.itemName < b.itemName) {
- return -1;
- } else {
- return 0;
- }
- })
- return result
- .reduce((string, el) => {
- string += `{${el.itemName};${el.childName};${el.price.toFixed(2)}}` + '\n'
- return string;
- }, '')
- .trimEnd()
- }
- }
- const commandCount = +gets();
- const test = new SantaSystem();
- for (let i = 0; i < commandCount; i++) {
- const string = gets().split(' ')
- const command = string[0];
- const args = string.slice(1).join(' ').split(';');
- print(test[command](args[0], +args[1], args[2]));
- }
Advertisement
Add Comment
Please, Sign In to add comment