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(name, grade, budget) {
- if (budget < this.budget) {
- return `${name}'s money is not enough to go on vacation to ${this.destination}.`;
- }
- if (!this.kids.hasOwnProperty(grade)) {
- this.kids[grade] = [];
- }
- if (this.kids[grade].some((x) => {
- return (x.split('-')[0] === name);
- })) {
- return `${name} is already in the list for this ${this.destination} vacation.`;
- } else {
- this.kids[grade].push(`${name}-${budget}`);
- return this.kids[grade];
- }
- }
- removeChild(name, grade) {
- if (this.kids.hasOwnProperty(grade)) {
- const index = this.kids[grade].map((x) => x.split('-')[0]).indexOf(name);
- if (index >= 0) {
- this.kids[grade].splice(index, 1);
- return this.kids[grade];
- }
- }
- return `We couldn't find ${name} in ${grade} grade.`
- }
- toString() {
- if (this.numberOfChildren() === 0) {
- return `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`;
- } else {
- let result = [`${this.organizer} will take ${this.numberOfChildren()} children on trip to ${this.destination}`];
- Object.entries(this.kids)
- .sort((a, b) => {
- return Number(a[0] - Number(b[0]))
- })
- .forEach((x) => {
- result.push(`Grade: ${x[0]}`);
- x[1].reduce((acc, x, index) => {
- acc.push(`${index + 1}. ${x}`);
- return acc;
- }, result);
- result.push('');
- });
- return result.join(`\n`).trim();
- }
- }
- numberOfChildren() {
- let countOfChildren = 0;
- for (let grade in this.kids) {
- if (this.kids.hasOwnProperty(grade)) {
- countOfChildren += this.kids[grade].length;
- }
- }
- return countOfChildren;
- }
- }
- // let vacation = new Vacation('Mr Pesho', 'San diego', 2000);
- // console.log(vacation.registerChild('Gosho', 5, 2000));
- // console.log(vacation.registerChild('Lilly', 6, 2100));
- // console.log(vacation.registerChild('Pesho', 6, 2400));
- // console.log(vacation.registerChild('Gosho', 5, 2000));
- // console.log(vacation.registerChild('Tanya', 5, 6000));
- // console.log(vacation.registerChild('Mitko', 10, 1590));
- // -------------------------------------------------------------
- // let vacation = new Vacation('Mr Pesho', 'San diego', 2000);
- // vacation.registerChild('Gosho', 5, 2000);
- // vacation.registerChild('Lilly', 6, 2100);
- // console.log(vacation.removeChild('Gosho', 9));
- // vacation.registerChild('Pesho', 6, 2400);
- // vacation.registerChild('Gosho', 5, 2000);
- // console.log(vacation.removeChild('Lilly', 6));
- // console.log(vacation.registerChild('Tanya', 5, 6000))
- // -------------------------------------------------------------
- // let vacation = new Vacation('Miss Elizabeth', 'Dubai', 2000);
- // vacation.registerChild('Gosho', 5, 3000);
- // vacation.registerChild('Lilly', 6, 1500);
- // vacation.registerChild('Pesho', 7, 4000);
- // vacation.registerChild('Tanya', 5, 5000);
- // vacation.registerChild('Mitko', 10, 5500);
- // console.log(vacation.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement