Advertisement
Guest User

Done

a guest
Jul 3rd, 2019
292
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.kids = {};
  7.     }
  8.  
  9.     registerChild(kidName, kidGrade, kidBudget) {
  10.         if (kidBudget >= this.budget) {
  11.  
  12.             if (!this.kids.hasOwnProperty(kidGrade)) {
  13.                 this.kids[kidGrade] = [];
  14.                 this.kids[kidGrade].push({ [kidName]: kidBudget });
  15.                 return this.kids[kidGrade].map(element => `${Object.keys(element)}-${Object.values(element)}`);
  16.  
  17.             } else {
  18.                 let checker = false;
  19.                 for (let students of this.kids[kidGrade]) {
  20.                     if (Object.keys(students).includes(kidName)) {
  21.                         checker = true;
  22.                     }
  23.                 }
  24.  
  25.                 if (checker === false) {
  26.                     this.kids[kidGrade].push({ [kidName]: kidBudget });
  27.                     return this.kids[kidGrade].map(element => `${Object.keys(element)}-${Object.values(element)}`);
  28.                 } else {
  29.                     return `${kidName} is already in the list for this ${this.destination} vacation.`;
  30.                 }
  31.             }
  32.         } else {
  33.             return `${kidName}'s money is not enough to go on vacation to ${this.destination}.`;
  34.        }
  35.    }
  36.  
  37.    removeChild(kidName, kidGrade) {
  38.        if (this.kids.hasOwnProperty(kidGrade)) {
  39.            for (let student of this.kids[kidGrade]) {
  40.                if (Object.keys(student).includes(kidName)) {
  41.                    let index = this.kids[kidGrade].indexOf(student)
  42.                    this.kids[kidGrade].splice(index, 1);
  43.                    return this.kids[kidGrade].map(element => `${Object.keys(element)}-${Object.values(element)}`);
  44.                } else {
  45.                    return `We couldn't find ${kidName} in ${kidGrade} grade.`
  46.                 }
  47.             }
  48.         } else {
  49.             return `We couldn't find ${kidName} in ${kidGrade} grade.`
  50.        }
  51.    }
  52.  
  53.    toString() {
  54.        if (this.numberOfChildren === 0) {
  55.            return `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`;
  56.        }
  57.  
  58.        Object.entries(this.kids).sort((a, b) => a[0] - b[0]);
  59.  
  60.        let output = `${this.organizer} will take ${this.numberOfChildren} children on trip to ${this.destination}\n`;
  61.        for (const grade in this.kids) {
  62.            output += `Grade: ${grade}\n`;
  63.            let counter = 1;
  64.            for (let test of this.kids[grade]) {
  65.                output += `${counter++}. ${Object.keys(test)}-${Object.values(test)}\n`
  66.            }
  67.        }
  68.  
  69.        return output;
  70.    }
  71.  
  72.    get numberOfChildren() {
  73.        let counter = 0;
  74.        for (let kvp in this.kids) {
  75.            for (let kids of this.kids[kvp]) {
  76.                if (kids !== "") {
  77.                    counter++;
  78.                }
  79.            }
  80.        }
  81.  
  82.        return counter;
  83.    }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement