Advertisement
viligen

camping

Jun 16th, 2022
995
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class SummerCamp {
  2.     constructor(organizer, location) {
  3.         this.organizer = organizer;
  4.         this.location = location;
  5.         this.priceForTheCamp = { child: 150, student: 300, collegian: 500 };
  6.         this.listOfParticipants = [];
  7.     }
  8.  
  9.     registerParticipant(name, condition, money) {
  10.         if (!this.priceForTheCamp.hasOwnProperty(condition)) {
  11.             throw new Error('Unsuccessful registration at the camp.');
  12.         }
  13.  
  14.         if (this._findParticipant(name)) {
  15.             return `The ${name} is already registered at the camp.`;
  16.         }
  17.         if (money < this.priceForTheCamp[condition]) {
  18.             return `The money is not enough to pay the stay at the camp.`;
  19.         }
  20.         this.listOfParticipants.push({ name, condition, power: 100, wins: 0 });
  21.         return `The ${name} was successfully registered.`;
  22.     }
  23.     unregisterParticipant(name) {
  24.         let idx = this._findParticipantsIdx(name);
  25.         if (idx === -1) {
  26.             throw new Error(`The ${name} is not registered in the camp.`);
  27.         }
  28.         this.listOfParticipants.splice(idx, 1);
  29.         return `The ${name} removed successfully.`;
  30.     }
  31.     timeToPlay(typeOfGame, participant1, participant2) {
  32.         let player1 = this._findParticipant(participant1);
  33.         let player2 = this._findParticipant(participant2);
  34.         if (!player1 || (!player2 && participant2 !== undefined)) {
  35.             throw new Error(`Invalid entered name/s.`);
  36.         }
  37.         if (
  38.             participant2 !== undefined &&
  39.             player1.condition !== player2.condition
  40.         ) {
  41.             throw new Error(`Choose players with equal condition.`);
  42.         }
  43.  
  44.         if (participant2 === undefined) {
  45.             player1.power += 20;
  46.             return `The ${player1.name} successfully completed the game ${typeOfGame}.`;
  47.         }
  48.         let winner = '';
  49.         if (player1.power > player2.power) {
  50.             winner = player1.name;
  51.             player1.wins++;
  52.         } else if (player1.power < player2.power) {
  53.             winner = player2.name;
  54.             player2.wins++;
  55.         }
  56.         if (!winner) {
  57.             return `There is no winner.`;
  58.         }
  59.         return `The ${winner} is winner in the game ${typeOfGame}.`;
  60.     }
  61.     toString() {
  62.         let result = [
  63.             `${this.organizer} will take ${this.listOfParticipants.length} participants on camping to ${this.location}`,
  64.         ];
  65.         this.listOfParticipants
  66.             .sort((a, b) => b.wins - a.wins)
  67.             .forEach((p) =>
  68.                 result.push(
  69.                     `${p.name} - ${p.condition} - ${p.power} - ${p.wins}`
  70.                 )
  71.             );
  72.  
  73.         return result.join('\n');
  74.     }
  75.  
  76.     _findParticipantsIdx(name) {
  77.         return this.listOfParticipants.findIndex((p) => p.name === name);
  78.     }
  79.     _findParticipant(name) {
  80.         return this.listOfParticipants.find((p) => p.name === name);
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement