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) {
- const keyes = Object.keys(this.priceForTheCamp)
- if (!keyes.includes(condition)) {
- throw new Error("Unsuccessful registration at the camp.")
- }
- if (this.listOfParticipants.length > 0) {
- for (let i = 0; i < this.listOfParticipants.length; i++) {
- const partName = this.listOfParticipants[i]['name']
- if (partName == name) {
- return(`The ${name} is already registered at the camp.`)
- }
- }
- }
- if (money < this.priceForTheCamp[condition]) {
- return(`The money is not enough to pay the stay at the camp.`)
- }
- this.listOfParticipants.push({"name": name, "condition": condition, "power": 100, "wins": 0})
- return(`The ${name} was successfully registered.`)
- }
- unregisterParticipant(name) {
- for (let i = 0; i < this.listOfParticipants.length; i++) {
- const partName = this.listOfParticipants[i]['name']
- if (partName == name) {
- this.listOfParticipants.splice(i, 1)
- }
- }
- throw new Error(`The ${name} is not registered in the camp.`);
- }
- timeToPlay(typeOfGame, participant1, participant2) {
- if (typeOfGame == "WaterBalloonFights") {
- let foundpart1 = false
- let foundpart2 = false
- let part1 = null
- let part2 = null
- for (let i = 0; i < this.listOfParticipants.length; i++) {
- const partName = this.listOfParticipants[i]['name']
- if (partName == participant1) {
- foundpart1 = true
- part1 = this.listOfParticipants[i]
- }
- if (partName == participant2) {
- foundpart2 = true
- part2 = this.listOfParticipants[i]
- }
- }
- if (foundpart1 && foundpart2) {
- if (part1.condition == part2.condition) {
- let selectedWinner = null
- if (part1.power > part2.power) {
- selectedWinner = part1
- } else if (part2.power > part1.power) {
- selectedWinner = part2
- }
- if (selectedWinner == null) {
- return `There is no winner.`;
- } else {
- selectedWinner.wins += 1
- return `The ${selectedWinner.name} is winner in the game ${typeOfGame}.`;
- }
- } else {
- throw new Error(`Choose players with equal condition.`)
- }
- } else {
- throw new Error('Invalid entered name/s.')
- }
- } else if (typeOfGame == "Battleship") {
- let foundpart = null
- for (let i = 0; i < this.listOfParticipants.length; i++) {
- const partName = this.listOfParticipants[i]['name']
- if (partName == participant1) {
- foundpart = this.listOfParticipants[i]
- }
- }
- if (foundpart) {
- foundpart.power += 20
- return `The ${foundpart.name} successfully completed the game ${typeOfGame}.`
- } else {
- throw new Error('Invalid entered name/s.')
- }
- }
- }
- toString() {
- let returnstring = `${this.organizer} will take ${this.listOfParticipants.length} participants on camping to ${this.location}\n`
- function compare(a, b) {
- let comparison = 0;
- if (a.wins > b.wins) {
- comparison = 1;
- } else if (a.wins < b.wins) {
- comparison = -1;
- }
- return comparison;
- }
- let sortedFags = this.listOfParticipants.sort(compare)
- for (let i = 0; i < sortedFags; i++) {
- const participant = sortedFags[i]
- returnstring += `${participant.name} - ${participant.condition} - ${participant.power} - ${participant.wins}\n`
- }
- return returnstring
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement