kalinx

gsdg

Oct 23rd, 2021
499
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[condition]) {
  11.             throw new Error("Unsuccessful registration at the camp.");
  12.         }
  13.         if (this.listOfParticipants.some(p => p.name == name)) {
  14.             return `The ${name} is already registered at the camp.`;
  15.         }
  16.         if (money < this.priceForTheCamp[condition]) {
  17.             return `The money is not enough to pay the stay at the camp.`;
  18.         }
  19.         this.listOfParticipants.push({
  20.             name,
  21.             condition,
  22.             power: 100,
  23.             wins: 0
  24.         });
  25.         return `The ${name} was successfully registered.`;
  26.     }
  27.  
  28.     unregisterParticipant(name) {
  29.         let part = this.listOfParticipants.filter(p => p.name == name);
  30.         if (part.length == 0) {
  31.             throw new Error(`The ${name} is not registered in the camp.`);
  32.         }
  33.         let ind = this.listOfParticipants.indexOf(part[0]);
  34.         this.listOfParticipants.splice(ind, 1);
  35.         return `The ${name} removed successfully.`;
  36.     }
  37.  
  38.     timeToPlay(typeOfGame, participant1, participant2) {
  39.         if (typeOfGame == 'WaterBalloonFights') {
  40.             if (!this.listOfParticipants.some(p => p.name == participant1) || !this.listOfParticipants.some(p => p.name == participant2)) {
  41.                 throw new Error(`Invalid entered name/s.`);
  42.             }
  43.             let part1 = this.listOfParticipants.filter(p => p.name == participant1)[0];
  44.             let part2 = this.listOfParticipants.filter(p => p.name == participant2)[0];
  45.             if (part1.condition != part2.condition) {
  46.                 throw new Error(`Choose players with equal condition.`);
  47.             }
  48.             if (part1.power > part2.power) {
  49.                 part1.wins++;
  50.                 return `The ${part1.name} is winner in the game ${typeOfGame}.`;
  51.             } else if (part2.power > part1.power) {
  52.                 part2.wins++;
  53.                 return `The ${part2.name} is winner in the game ${typeOfGame}.`;
  54.             } else {
  55.                 return `There is no winner.`;
  56.             }
  57.         } else if (typeOfGame == 'Battleship') {
  58.             if (!this.listOfParticipants.some(p => p.name == participant1)) {
  59.                 throw new Error(`Invalid entered name/s.`);
  60.             }
  61.             let part1 = this.listOfParticipants.filter(p => p.name == participant1)[0];
  62.             part1.power += 20;
  63.             return `The ${part1.name} successfully completed the game ${typeOfGame}.`
  64.         }
  65.     }
  66.  
  67.     toString() {
  68.         let output = [`${this.organizer} will take ${this.listOfParticipants.length} participants on camping to ${this.location}`];
  69.         this.listOfParticipants.sort((p1, p2) => p2.wins - p1.wins).forEach(p => {
  70.             output.push(`${p.name} - ${p.condition} - ${p.power} - ${p.wins}`);
  71.         });
  72.         return output.join('\n');
  73.     }
  74. }
RAW Paste Data