GIGABABAIT

SummerCamp 60/100

Oct 23rd, 2021
658
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.         const keyes = Object.keys(this.priceForTheCamp)
  11.         if (!keyes.includes(condition)) {
  12.             throw new Error("Unsuccessful registration at the camp.")
  13.         }
  14.  
  15.         if (this.listOfParticipants.length > 0) {
  16.             for (let i = 0; i < this.listOfParticipants.length; i++) {
  17.                 const partName = this.listOfParticipants[i]['name']
  18.                 if (partName == name) {
  19.                     return(`The ${name} is already registered at the camp.`)
  20.                 }
  21.             }
  22.         }
  23.  
  24.         if (money < this.priceForTheCamp[condition]) {
  25.             return(`The money is not enough to pay the stay at the camp.`)
  26.         }
  27.  
  28.         this.listOfParticipants.push({"name": name, "condition": condition, "power": 100, "wins": 0})
  29.         return(`The ${name} was successfully registered.`)
  30.     }
  31.  
  32.     unregisterParticipant(name) {
  33.         for (let i = 0; i < this.listOfParticipants.length; i++) {
  34.             const partName = this.listOfParticipants[i]['name']
  35.             if (partName == name) {
  36.                 this.listOfParticipants.splice(i, 1)
  37.             }
  38.         }
  39.  
  40.         throw new Error(`The ${name} is not registered in the camp.`);
  41.     }
  42.  
  43.     timeToPlay(typeOfGame, participant1, participant2) {
  44.         if (typeOfGame == "WaterBalloonFights") {
  45.             let foundpart1 = false
  46.             let foundpart2 = false
  47.             let part1 = null
  48.             let part2 = null
  49.             for (let i = 0; i < this.listOfParticipants.length; i++) {
  50.                 const partName = this.listOfParticipants[i]['name']
  51.                 if (partName == participant1) {
  52.                     foundpart1 = true
  53.                     part1 = this.listOfParticipants[i]
  54.                 }
  55.  
  56.                 if (partName == participant2) {
  57.                     foundpart2 = true
  58.                     part2 = this.listOfParticipants[i]
  59.                 }
  60.             }
  61.  
  62.             if (foundpart1 && foundpart2) {
  63.                 if (part1.condition == part2.condition) {
  64.                     let selectedWinner = null
  65.                     if (part1.power > part2.power) {
  66.                         selectedWinner = part1
  67.                     } else if (part2.power > part1.power) {
  68.                         selectedWinner = part2
  69.                     }
  70.  
  71.                     if (selectedWinner == null) {
  72.                         return `There is no winner.`;
  73.                     } else {
  74.                         selectedWinner.wins += 1
  75.                         return `The ${selectedWinner.name} is winner in the game ${typeOfGame}.`;
  76.                     }
  77.  
  78.                 } else {
  79.                     throw new Error(`Choose players with equal condition.`)
  80.                 }
  81.  
  82.             } else {
  83.                 throw new Error('Invalid entered name/s.')
  84.             }
  85.         } else if (typeOfGame == "Battleship") {
  86.             let foundpart = null
  87.             for (let i = 0; i < this.listOfParticipants.length; i++) {
  88.                 const partName = this.listOfParticipants[i]['name']
  89.                 if (partName == participant1) {
  90.                     foundpart = this.listOfParticipants[i]
  91.                 }
  92.             }
  93.  
  94.             if (foundpart) {
  95.                 foundpart.power += 20
  96.                 return `The ${foundpart.name} successfully completed the game ${typeOfGame}.`
  97.             } else {
  98.                 throw new Error('Invalid entered name/s.')
  99.             }
  100.         }
  101.     }
  102.  
  103.     toString() {
  104.         let returnstring = `${this.organizer} will take ${this.listOfParticipants.length} participants on camping to ${this.location}\n`
  105.  
  106.         function compare(a, b) {
  107.             let comparison = 0;
  108.             if (a.wins > b.wins) {
  109.               comparison = 1;
  110.             } else if (a.wins < b.wins) {
  111.               comparison = -1;
  112.             }
  113.             return comparison;
  114.           }
  115.          
  116.  
  117.         let sortedFags = this.listOfParticipants.sort(compare)
  118.  
  119.         for (let i = 0; i < sortedFags; i++) {
  120.             const participant = sortedFags[i]
  121.             returnstring += `${participant.name} - ${participant.condition} - ${participant.power} - ${participant.wins}\n`
  122.         }
  123.         return returnstring
  124.     }
  125. }
RAW Paste Data