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 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 = this.print || console.log;
- let gets =
- this.gets ||
- (
- (arr, index) => () =>
- arr[index++]
- )(input, 0);
- //class implementation
- class SantaHelper {
- constructor() {
- this.santaList = new Map();
- }
- addWish(item, price, child) {
- if (this.santaList.has(child)) {
- this.santaList.get(child).push({ item, price });
- } else {
- this.santaList.set(child, [{ item: item, price: price }]);
- }
- print("Wish added");
- }
- deleteWishes(child) {
- if (this.santaList.has(child)) {
- print(`${this.santaList.get(child).length} Wishes deleted`);
- this.santaList.delete(child);
- } else {
- print("No Wishes found");
- }
- }
- findWishesByPriceRange(from, to) {
- let array = [];
- for (const [key, value] of this.santaList.entries()) {
- array.push({ name: key, wishes: value });
- }
- //sort wishes by name prop
- array.sort((a, b) => (a.name > b.name ? 1 : -1));
- let flag = false;
- //for each wishes and filter from to price
- for (const obj of array) {
- obj.wishes.forEach((wish) => {
- if (+wish.price >= +from && +wish.price <= +to) {
- let wishPrice = +wish.price;
- print(`{${wish.item};${obj.name};${wishPrice.toFixed(2)}}`);
- flag = true;
- }
- });
- }
- if (flag === false) {
- print("No Wishes found");
- }
- }
- findWishesByChild(child) {
- if (this.santaList.has(child)) {
- let itemsAndPrices = this.santaList.get(child);
- for (const itemAndPrice of itemsAndPrices) {
- let price = +itemAndPrice.price;
- print(`{${itemAndPrice.item};${child};${price.toFixed(2)}}`);
- }
- } else {
- print("No Wishes found");
- }
- }
- }
- //create an instance of the class
- const santaHelper = new SantaHelper();
- //get the input
- const commandsCount = +gets();
- for (let index = 0; index < commandsCount; index++) {
- let command = gets();
- //get the first string - command
- let array = command.split(" ");
- let commandString = array.slice(0, 1);
- //remove the command from the string
- const indexOfSpace = command.indexOf(" ");
- const itemPriceNameString = command.substring(indexOfSpace + 1);
- //save the rest in array
- itemPriceNameArray = itemPriceNameString.split(";");
- //if AddWish call the method and spread the itempricenamearray
- //call method
- if (commandString[0] === "AddWish") {
- santaHelper.addWish(...itemPriceNameArray);
- } else if (commandString[0] === "DeleteWishes") {
- santaHelper.deleteWishes(...itemPriceNameArray);
- } else if (commandString[0] === "FindWishesByChild") {
- santaHelper.findWishesByChild(...itemPriceNameArray);
- } else if (commandString[0] === "FindWishesByPriceRange") {
- santaHelper.findWishesByPriceRange(...itemPriceNameArray);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment