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 = {};
- }
- registerChild(kidName, kidGrade, kidBudget) {
- if (kidBudget >= this.budget) {
- if (!this.kids.hasOwnProperty(kidGrade)) {
- this.kids[kidGrade] = [];
- this.kids[kidGrade].push({ [kidName]: kidBudget });
- return this.kids[kidGrade].map(element => `${Object.keys(element)}-${Object.values(element)}`);
- } else {
- let checker = false;
- for (let students of this.kids[kidGrade]) {
- if (Object.keys(students).includes(kidName)) {
- checker = true;
- }
- }
- if (checker === false) {
- this.kids[kidGrade].push({ [kidName]: kidBudget });
- return this.kids[kidGrade].map(element => `${Object.keys(element)}-${Object.values(element)}`);
- } else {
- return `${kidName} is already in the list for this ${this.destination} vacation.`;
- }
- }
- } else {
- return `${kidName}'s money is not enough to go on vacation to ${this.destination}.`;
- }
- }
- removeChild(kidName, kidGrade) {
- if (this.kids.hasOwnProperty(kidGrade)) {
- for (let student of this.kids[kidGrade]) {
- if (Object.keys(student).includes(kidName)) {
- let index = this.kids[kidGrade].indexOf(student)
- this.kids[kidGrade].splice(index, 1);
- return this.kids[kidGrade].map(element => `${Object.keys(element)}-${Object.values(element)}`);
- } else {
- return `We couldn't find ${kidName} in ${kidGrade} grade.`
- }
- }
- } else {
- return `We couldn't find ${kidName} in ${kidGrade} grade.`
- }
- }
- toString() {
- if (this.numberOfChildren === 0) {
- return `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`;
- }
- Object.entries(this.kids).sort((a, b) => a[0] - b[0]);
- let output = `${this.organizer} will take ${this.numberOfChildren} children on trip to ${this.destination}\n`;
- for (const grade in this.kids) {
- output += `Grade: ${grade}\n`;
- let counter = 1;
- for (let test of this.kids[grade]) {
- output += `${counter++}. ${Object.keys(test)}-${Object.values(test)}\n`
- }
- }
- return output;
- }
- get numberOfChildren() {
- let counter = 0;
- for (let kvp in this.kids) {
- for (let kids of this.kids[kvp]) {
- if (kids !== "") {
- counter++;
- }
- }
- }
- return counter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement