Advertisement
Guest User

i hate my life

a guest
Jul 2nd, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Vacation {
  2.     constructor(organizer, destination, budget) {
  3.         this.organizer = organizer;
  4.         this.destination = destination;
  5.         this.budget = budget;
  6.         this.kidsList = {};
  7.     }
  8.  
  9.     registerChild(kidName, kidGrade, kidBudget) {
  10.         if (kidBudget >= this.budget) {
  11.             if (!this.kidsList.hasOwnProperty(kidGrade)) {
  12.                 this.kidsList[kidGrade] = [];
  13.                 // let addingPattern = `${kidName}-${kidBudget}`;
  14.                 // this.kidsList[kidGrade].push(`${kidName}-${kidBudget}`);
  15.                 // let test = { name: kidName, budget: kidBudget };
  16.  
  17.                 this.kidsList[kidGrade].push({ [kidName]: kidBudget });
  18.                 return this.kidsList[kidGrade].map(element => `${Object.keys(element)}-${Object.values(element)}`);
  19.             } else {
  20.                 // for (let grade of this.kidsList[kidGrade]) {
  21.                 //     if (grade.split("-")[0] === kidName) {
  22.                 //     }
  23.                 // }
  24.                 // let checkArr = this.kidsList[kidGrade].map(element => element.split("-")[0]);
  25.                 let isThere = true;
  26.                 for (let test of this.kidsList[kidGrade]) {
  27.                     if (Object.keys(test).includes(kidName)) {
  28.                         isThere = false;
  29.                     }
  30.                 }
  31.                 // if (this.kidsList[kidGrade] !== kidName) {
  32.                 if (isThere) {
  33.                     // this.kidsList[kidGrade].push(`${kidName}-${kidBudget}`);
  34.                     this.kidsList[kidGrade].push({ [kidName]: kidBudget });
  35.                     return this.kidsList[kidGrade].map(element => `${Object.keys(element)}-${Object.values(element)}`);
  36.                 } else {
  37.                     return `${kidName} is already in the list for this ${this.destination} vacation.`;
  38.                 }
  39.             }
  40.         } else {
  41.             return `${kidName}'s money is not enough to go on vacation to ${this.destination}.`;
  42.        }
  43.    }
  44.  
  45.    removeChild(kidName, kidGrade) {
  46.        if (this.kidsList.hasOwnProperty(kidGrade)) {
  47.            for (let student of this.kidsList[kidGrade]) {
  48.                if (Object.keys(student).includes(kidName)) {
  49.                    let index = this.kidsList[kidGrade].indexOf(student)
  50.                    this.kidsList[kidGrade].splice(index, 1);
  51.                    return this.kidsList[kidGrade].map(element => `${Object.keys(element)}-${Object.values(element)}`);
  52.                } else {
  53.                    return `We couldn't find ${kidName} in ${kidGrade} grade.`
  54.                 }
  55.             }
  56.         } else {
  57.             return `We couldn't find ${kidName} in ${kidGrade} grade.`
  58.        }
  59.    }
  60.  
  61.    toString() {
  62.        if (this.numberOfChildren === 0) {
  63.            return `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`;
  64.        }
  65.        for (const key in this.kidsList) {
  66.            this.kidsList[key] = this.kidsList[key].sort((a, b) => {
  67.                return Number(Object.values(a)) - Number(Object.values(b));
  68.            });
  69.        }
  70.  
  71.        let output = `${this.organizer} will take ${this.numberOfChildren} children on trip to ${this.destination}\n`;
  72.        for (const grade in this.kidsList) {
  73.            output += `Grade: ${grade}\n`;
  74.            let counter = 1;
  75.            for (let test of this.kidsList[grade]) {
  76.                output += `${counter++}. ${Object.keys(test)}-${Object.values(test)}\n`
  77.            }
  78.            output += `\n`;
  79.        }
  80.        return output;
  81.    }
  82.  
  83.    get numberOfChildren() {
  84.        let counter = 0;
  85.        for (let kvp in this.kidsList) {
  86.            for (let kids of this.kidsList[kvp]) {
  87.                if (kids !== "") {
  88.                    counter++;
  89.                }
  90.            }
  91.        }
  92.        return counter;
  93.    }
  94. }
  95.  
  96. // let vacation = new Vacation('Mr Pesho', 'San diego', 2000);
  97. // console.log(vacation.registerChild('Gosho', 5, 2000));
  98. // console.log(vacation.registerChild('Lilly', 6, 2100));
  99. // console.log(vacation.registerChild('Pesho', 6, 2400));
  100. // console.log(vacation.registerChild('Gosho', 5, 2000));
  101. // console.log(vacation.registerChild('Tanya', 5, 6000));
  102. // console.log(vacation.registerChild('Mitko', 10, 1590));
  103.  
  104.  
  105. // let vacation = new Vacation('Mr Pesho', 'San diego', 2000);
  106. // vacation.registerChild('Gosho', 5, 2000);
  107. // vacation.registerChild('Lilly', 6, 2100);
  108.  
  109. // console.log(vacation.removeChild('Gosho', 9));
  110.  
  111. // vacation.registerChild('Pesho', 6, 2400);
  112. // vacation.registerChild('Gosho', 5, 2000);
  113.  
  114. // console.log(vacation.removeChild('Lilly', 6));
  115. // console.log(vacation.registerChild('Tanya', 5, 6000))
  116.  
  117. // console.log(vacation.numberOfChildren);
  118. // console.log(vacation.toString());
  119.  
  120. // let vacation = new Vacation('Miss Elizabeth', 'Dubai', 2000);
  121. // vacation.registerChild('Gosho', 5, 3000);
  122. // vacation.registerChild('Lilly', 6, 1500);
  123. // vacation.registerChild('Pesho', 7, 4000);
  124. // vacation.registerChild('Tanya', 5, 5000);
  125. // vacation.registerChild('Mitko', 10, 5500)
  126. // console.log(vacation.toString());
  127.  
  128. // let vacation = new Vacation('Miss. Elizabeth', 'The bahamas', 400);
  129.  
  130. // vacation.registerChild('Gosho', 12, 3400);
  131. // vacation.registerChild('Pesho', 12, 400);
  132. // vacation.registerChild('Pesho', 12, 400);
  133. // vacation.registerChild('Skaro', 11, 400);
  134. // vacation.registerChild('Gosho', 11, 3444);
  135.  
  136. // console.log(vacation.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement