Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 print = this.print || console.log;
- let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
- class SantaHelper {
- constructor() {
- this.wishes = [];
- }
- addWish(itemName, estimatedPrice, childName) {
- this.wishes.push({ item: itemName, price: estimatedPrice, child: childName });
- return "Wish added";
- }
- deleteWishes(childName) {
- const initialCount = this.wishes.length;
- this.wishes = this.wishes.filter((wish) => wish.child !== childName);
- const deletedCount = initialCount - this.wishes.length;
- if (deletedCount > 0) {
- return `${deletedCount} Wishes deleted`;
- } else {
- return "No Wishes found";
- }
- }
- findWishesByPriceRange(fromPrice, toPrice) {
- const foundWishes = this.wishes.filter(
- (wish) => wish.price >= fromPrice && wish.price <= toPrice
- );
- if (foundWishes.length > 0) {
- foundWishes.sort((a, b) => a.item.localeCompare(b.item));
- const result = foundWishes.map((wish) => {
- return `{${wish.item};${wish.child};${wish.price.toFixed(2)}}`;
- });
- return result.join("\n");
- } else {
- return "No Wishes found";
- }
- }
- findWishesByChild(childName) {
- const foundWishes = this.wishes.filter((wish) => wish.child === childName);
- if (foundWishes.length > 0) {
- foundWishes.sort((a, b) => a.item.localeCompare(b.item));
- const result = foundWishes.map((wish) => {
- return `{${wish.item};${wish.child};${wish.price.toFixed(2)}}`;
- });
- return result.join("\n");
- } else {
- return "No Wishes found";
- }
- }
- processCommands(commands) {
- const output = [];
- for (const command of commands) {
- const [action, ...args] = command.split(" ");
- switch (action) {
- case "AddWish":
- const [itemName, estimatedPrice, childName] = args.join(" ").split(";");
- output.push(this.addWish(itemName, Number(estimatedPrice), childName));
- break;
- case "DeleteWishes":
- const [deletedChildName] = args.join(' ').split(';');
- output.push(this.deleteWishes(deletedChildName));
- break;
- case "FindWishesByPriceRange":
- const [fromPrice, toPrice] = args.join(' ').split(';');
- output.push(this.findWishesByPriceRange(Number(fromPrice), Number(toPrice)));
- break;
- case "FindWishesByChild":
- const [childNameToFind] = args.join(' ').split(';');
- output.push(this.findWishesByChild(childNameToFind));
- break;
- default:
- break;
- }
- }
- return output.join("\n");
- }
- }
- const santaHelper = new SantaHelper();
- const N = Number(gets());
- const commands = [];
- for (let i = 0; i < N; i++) {
- commands.push(gets());
- }
- const result = santaHelper.processCommands(commands);
- print(result);
Advertisement
Add Comment
Please, Sign In to add comment