Nina-S

Untitled

Mar 11th, 2022
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. let input = [
  2. "10",
  3. "AddWish Electric Scooter 2000Z;1536.50;Stefan",
  4. "AddWish Fortnite Skin;3000;Stefan",
  5. "AddWish AMD Radeon;4099.99;Pesho",
  6. "AddWish Apple AirPods;200000;Kiril",
  7. "AddWish Socks;10000;Tosho",
  8. "AddWish Swater;999;Stefan",
  9. "FindWishesByChild Stefan",
  10. "DeleteWishes Stefan",
  11. "FindWishesByChild Stefan",
  12. "FindWishesByPriceRange 100000;200000",
  13. ];
  14.  
  15. // let input = [
  16. // "8",
  17. // "AddWish Electric Scooter 2000Z;3500.05;Rayko Petrov",
  18. // "AddWish Fortnite Skin;3000;Rayko Petrov",
  19. // "AddWish AMD Radeon;16400;Hristo",
  20. // "AddWish Apple AirPods;21111.50;Nadya",
  21. // "FindWishesByChild Rayko Petrov",
  22. // "DeleteWishes Rayko Petrov",
  23. // "FindWishesByChild Rayko Petrov",
  24. // "FindWishesByPriceRange 5000;30000",
  25. // ];
  26.  
  27. let print = this.print || console.log;
  28.  
  29. let gets =
  30. this.gets ||
  31. (
  32. (arr, index) => () =>
  33. arr[index++]
  34. )(input, 0);
  35.  
  36. //class implementation
  37.  
  38. class SantaHelper {
  39. constructor() {
  40. this.santaList = new Map();
  41. }
  42.  
  43. addWish(item, price, child) {
  44. if (this.santaList.has(child)) {
  45. this.santaList.get(child).push({ item, price });
  46. } else {
  47. this.santaList.set(child, [{ item: item, price: price }]);
  48. }
  49. print("Wish added");
  50. }
  51.  
  52. deleteWishes(child) {
  53. if (this.santaList.has(child)) {
  54. print(`${this.santaList.get(child).length} Wishes deleted`);
  55. this.santaList.delete(child);
  56. } else {
  57. print("No Wishes found");
  58. }
  59. }
  60.  
  61. findWishesByPriceRange(from, to) {
  62. let array = [];
  63. for (const [key, value] of this.santaList.entries()) {
  64. array.push({ name: key, wishes: value });
  65. }
  66. //sort wishes by name prop
  67. array.sort((a, b) => (a.name > b.name ? 1 : -1));
  68. let flag = false;
  69. //for each wishes and filter from to price
  70. for (const obj of array) {
  71. obj.wishes.forEach((wish) => {
  72. if (+wish.price >= +from && +wish.price <= +to) {
  73. let wishPrice = +wish.price;
  74. print(`{${wish.item};${obj.name};${wishPrice.toFixed(2)}}`);
  75. flag = true;
  76. }
  77. });
  78. }
  79. if (flag === false) {
  80. print("No Wishes found");
  81. }
  82. }
  83.  
  84. findWishesByChild(child) {
  85. if (this.santaList.has(child)) {
  86. let itemsAndPrices = this.santaList.get(child);
  87. for (const itemAndPrice of itemsAndPrices) {
  88. let price = +itemAndPrice.price;
  89. print(`{${itemAndPrice.item};${child};${price.toFixed(2)}}`);
  90. }
  91. } else {
  92. print("No Wishes found");
  93. }
  94. }
  95. }
  96.  
  97. //create an instance of the class
  98. const santaHelper = new SantaHelper();
  99.  
  100. //get the input
  101. const commandsCount = +gets();
  102.  
  103. for (let index = 0; index < commandsCount; index++) {
  104. let command = gets();
  105.  
  106. //get the first string - command
  107. let array = command.split(" ");
  108. let commandString = array.slice(0, 1);
  109.  
  110. //remove the command from the string
  111. const indexOfSpace = command.indexOf(" ");
  112. const itemPriceNameString = command.substring(indexOfSpace + 1);
  113.  
  114. //save the rest in array
  115. itemPriceNameArray = itemPriceNameString.split(";");
  116.  
  117. //if AddWish call the method and spread the itempricenamearray
  118. //call method
  119. if (commandString[0] === "AddWish") {
  120. santaHelper.addWish(...itemPriceNameArray);
  121. } else if (commandString[0] === "DeleteWishes") {
  122. santaHelper.deleteWishes(...itemPriceNameArray);
  123. } else if (commandString[0] === "FindWishesByChild") {
  124. santaHelper.findWishesByChild(...itemPriceNameArray);
  125. } else if (commandString[0] === "FindWishesByPriceRange") {
  126. santaHelper.findWishesByPriceRange(...itemPriceNameArray);
  127. }
  128. }
  129.  
Advertisement
Add Comment
Please, Sign In to add comment