Advertisement
Guest User

02. Vacation

a guest
Oct 22nd, 2019
148
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(name, grade, budget){
  10.         if(budget < this.budget){
  11.             return `${name}'s money is not enough to go on vacation to ${this.destination}.`;
  12.        }
  13.        if(this.kids.hasOwnProperty(grade)){
  14.  
  15.            for(let kid of this.kids[grade]){
  16.                if(kid === `${name}-${budget}`){
  17.                    return `${name} is already in the list for this ${this.destination} vacation.`;
  18.                }
  19.                this.kids[grade].push(`${name}-${budget}`);
  20.            }
  21.        }else{
  22.  
  23.            this.kids[grade] = [];
  24.            this.kids[grade].push(`${name}-${budget}`);
  25.        }
  26.        return this.kids[grade];
  27.    }
  28.  
  29.    removeChild(name, grade){
  30.        if(this.kids.hasOwnProperty(grade)){
  31.            for(let kid of this.kids[grade]){
  32.                let kidInfo = kid.split('-');
  33.                let kidName = kidInfo[0];
  34.  
  35.                if(kidName === name){
  36.                    let index = this.kids[grade].indexOf(kid);
  37.                    this.kids[grade].splice(index, 1);
  38.                    return this.kids[grade];
  39.                }
  40.            }
  41.        }
  42.  
  43.        return `We couldn't find ${name} in ${grade} grade.`;
  44.     }
  45.  
  46.     get numberOfChildren(){
  47.         this._numberOfChildren = 0 ;
  48.         for(let grade in this.kids){
  49.             this._numberOfChildren = this.kids[grade].length
  50.         }
  51.         return this._numberOfChildren;
  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.         let result = `${this.organizer} will take ${this.numberOfChildren} children on trip to ${this.destination} \n`;
  59.        
  60.         Object.entries(this.kids).sort((a, b) => a[0] - b[0]);
  61.  
  62.         for(let grade in this.kids){
  63.             result+= `Grade: ${grade}\n`;
  64.             let currentNumber =1;
  65.             for(let kid of this.kids[grade]){
  66.                 result += `${currentNumber}. ${kid}\n`;
  67.                 currentNumber++;
  68.             }
  69.         }
  70.  
  71.         return result;
  72.        
  73.     }
  74. }
  75. // let vacat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement