Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. class Vacation {
  2. constructor(organizer, destination, budget) {
  3. this.organizer = organizer;
  4. this.destination = destination;
  5. this.budget = budget;
  6. this.kids = {};
  7. this.grade = {};
  8. this.countKids = 0;
  9. }
  10.  
  11. registerChild(name, grade, budget) {
  12. let isNotThere = true;
  13. if (budget < this.budget) {
  14. return `${name}'s money is not enough to go on vacation to ${this.destination}.`;
  15. }
  16.  
  17. for (let kidsKey in this.kids) {
  18. if (this.kids[kidsKey].grade === grade) {
  19. for (let x of this.grade[this.kids[kidsKey].grade]) {
  20. if (x.split('-')[0] === name) {
  21. isNotThere = false;
  22. return `${name} is already in the list for this ${this.destination} vacation.`;
  23. }
  24. }
  25. }
  26. }
  27. if (isNotThere) {
  28. if (typeof this.kids[`${name}`] !== 'undefined') {
  29. if (this.kids[`${name}`].name !== name || this.kids[`${name}`].grade !== grade) {
  30. this.kids[`${name}${this.countKids++}`] = {name, grade, budget};
  31. }
  32. }else {
  33. this.kids[`${name}`] = {name, grade, budget};
  34. }
  35. if (typeof this.grade[`${grade}`] === "undefined") {
  36. this.grade[`${grade}`] = [];
  37. }
  38. this.grade[`${grade}`].push(`${name}-${budget}`)
  39. }
  40.  
  41. return this.grade[`${grade}`];
  42. }
  43.  
  44. removeChild(name, grade) {
  45. let isNotExist = true;
  46. for (let x in this.grade[grade]) {
  47. let [nameKid, budget] = this.grade[grade][x].split('-');
  48. if (nameKid === name) {
  49. isNotExist = false;
  50. this.grade[grade].splice(x, 1);
  51. this.kids[`${name}`] = '';
  52. }
  53. }
  54.  
  55.  
  56.  
  57. if (isNotExist) {
  58. return `We couldn't find ${name} in ${grade} grade.`
  59. }
  60.  
  61. return this.grade[grade];
  62. }
  63.  
  64. get numberOfChildren() {
  65. let count = 0;
  66. for (let kidsKey in this.kids) {
  67. if (this.kids[kidsKey] !== ''){
  68. count++;
  69. }
  70. }
  71. return count;
  72. }
  73.  
  74. toString() {
  75. let res = '';
  76. let count = 0;
  77. let isThereEl = true;
  78. res += `${this.organizer} will take ${this.numberOfChildren} children on trip to ${this.destination}\n`;
  79.  
  80. for (let gradeKey in this.grade) {
  81. isThereEl = false;
  82. res += `Grade: ${gradeKey}\n`;
  83. let count = 1;
  84. for (let x of this.grade[gradeKey]) {
  85. res += `${count}. ${x}\n`;
  86. count++;
  87. }
  88. }
  89.  
  90. if (isThereEl){
  91. res = `No children are enrolled for the trip and the organization of ${this.organizer} falls out...Check the example below for more clarity.`
  92. }
  93.  
  94. return res;
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement