Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SummerCamp {
- constructor(organizer, location) {
- this.organizer = organizer;
- this.location = location;
- this.priceForTheCamp = { "child": 150, "student": 300, "collegian": 500 };
- this.listOfParticipants = [];
- }
- registerParticipant(name, condition, money) {
- let arrOfConditions = Object.keys(this.priceForTheCamp);
- let namesOfPart = this.listOfParticipants.map(obj => obj.name);
- if (!arrOfConditions.includes(condition)) {
- throw new Error("Unsuccessful registration at the camp.");
- } else if (namesOfPart.includes(name)) {
- return `The ${name} is already registered at the camp.`;
- } else if (this.priceForTheCamp[condition] > money) {
- return `The money is not enough to pay the stay at the camp.`;
- } else {
- this.listOfParticipants.push({ name: name, condition: condition, power: 100, wins: 0 });
- return `The ${name} was successfully registered.`;
- }
- }
- unregisterParticipant(name) {
- let namesOfPart = this.listOfParticipants.map(obj => obj.name);
- if (!namesOfPart.includes(name)) {
- throw new Error(`The ${name} is not registered in the camp.`);
- } else {
- this.listOfParticipants.forEach((obj, i) => obj.name === name ? this.listOfParticipants.splice(i, 1) : false);
- return `The ${name} removed successfully.`;
- }
- }
- timeToPlay(typeOfGame, participant1, participant2) {
- let namesOfPart = this.listOfParticipants.map(obj => obj.name);
- let part1Con = '';
- let part2Con = '';
- let powerPrPar1 = 0;
- let powerPrPar2 = 0;
- for (let el of this.listOfParticipants) {
- if (el.name === participant1) {
- part1Con = el.condition;
- powerPrPar1 = el.power;
- } else {
- part2Con = el.condition;
- powerPrPar2 = el.power;
- }
- }
- if (typeOfGame === 'WaterBalloonFights') {
- if (!namesOfPart.includes(participant1) || !namesOfPart.includes(participant2)) {
- throw new Error(`Invalid entered name/s.`);
- } else if (part1Con !== part2Con) {
- throw new Error(`Choose players with equal condition.`);
- }
- if (powerPrPar1 > powerPrPar2) {
- this.listOfParticipants.forEach(obj => obj.name === participant1 ? obj.wins++ : false);
- return `The ${participant1} is winner in the game ${typeOfGame}.`
- } else if (powerPrPar1 < powerPrPar2) {
- this.listOfParticipants.forEach(obj => obj.name === participant2 ? obj.wins++ : false);
- return `The ${participant2} is winner in the game ${typeOfGame}.`
- } else {
- return `There is no winner.`;
- }
- } else if (typeOfGame === 'Battleship') {
- if (!namesOfPart.includes(participant1)) {
- throw new Error(`Invalid entered name/s.`);
- }
- this.listOfParticipants.forEach(obj => obj.name === participant1 ? obj.power += 20 : false);
- return `The ${participant1} successfully completed the game ${typeOfGame}.`;
- }
- }
- toString() {
- let arr = this.listOfParticipants;
- let numberOfParticipants = arr.length;
- let resultArr = [`${this.organizer} will take ${numberOfParticipants} participants on camping to ${this.location}`];
- arr.sort((a, b) => b.wins - a.wins);
- for (let obj of arr) {
- let cur = `${obj.name} - ${obj.condition} - ${obj.power} - ${obj.wins}`;
- resultArr.push(cur);
- }
- return resultArr.join('\n');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement