Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vacation {
- constructor(organizer, destination, budget) {
- this.organizer = organizer;
- this.destination = destination;
- this.budget = budget;
- this.kids = {};
- this.grade = {};
- this.countKids = 0;
- }
- registerChild(name, grade, budget) {
- let isNotThere = true;
- if (budget < this.budget) {
- return `${name}'s money is not enough to go on vacation to ${this.destination}.`;
- }
- for (let kidsKey in this.kids) {
- if (this.kids[kidsKey].grade === grade) {
- for (let x of this.grade[this.kids[kidsKey].grade]) {
- if (x.split('-')[0] === name) {
- isNotThere = false;
- return `${name} is already in the list for this ${this.destination} vacation.`;
- }
- }
- }
- }
- if (isNotThere) {
- if (typeof this.kids[`${name}`] !== 'undefined') {
- if (this.kids[`${name}`].name !== name || this.kids[`${name}`].grade !== grade) {
- this.kids[`${name}${this.countKids++}`] = {name, grade, budget};
- }
- }else {
- this.kids[`${name}`] = {name, grade, budget};
- }
- if (typeof this.grade[`${grade}`] === "undefined") {
- this.grade[`${grade}`] = [];
- }
- this.grade[`${grade}`].push(`${name}-${budget}`)
- }
- return this.grade[`${grade}`];
- }
- removeChild(name, grade) {
- let isNotExist = true;
- for (let x in this.grade[grade]) {
- let [nameKid, budget] = this.grade[grade][x].split('-');
- if (nameKid === name) {
- isNotExist = false;
- this.grade[grade].splice(x, 1);
- this.kids[`${name}`] = '';
- }
- }
- if (isNotExist) {
- return `We couldn't find ${name} in ${grade} grade.`
- }
- return this.grade[grade];
- }
- get numberOfChildren() {
- let count = 0;
- for (let kidsKey in this.kids) {
- if (this.kids[kidsKey] !== ''){
- count++;
- }
- }
- return count;
- }
- toString() {
- let res = '';
- let count = 0;
- let isThereEl = true;
- res += `${this.organizer} will take ${this.numberOfChildren} children on trip to ${this.destination}\n`;
- for (let gradeKey in this.grade) {
- isThereEl = false;
- res += `Grade: ${gradeKey}\n`;
- let count = 1;
- for (let x of this.grade[gradeKey]) {
- res += `${count}. ${x}\n`;
- count++;
- }
- }
- if (isThereEl){
- res = `No children are enrolled for the trip and the organization of ${this.organizer} falls out...Check the example below for more clarity.`
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement