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) {
- if (!this.priceForTheCamp.hasOwnProperty(condition)) {
- throw new Error('Unsuccessful registration at the camp.');
- }
- if (this._findParticipant(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, condition, power: 100, wins: 0 });
- return `The ${name} was successfully registered.`;
- }
- unregisterParticipant(name) {
- let idx = this._findParticipantsIdx(name);
- if (idx === -1) {
- throw new Error(`The ${name} is not registered in the camp.`);
- }
- this.listOfParticipants.splice(idx, 1);
- return `The ${name} removed successfully.`;
- }
- timeToPlay(typeOfGame, participant1, participant2) {
- let player1 = this._findParticipant(participant1);
- let player2 = this._findParticipant(participant2);
- if (!player1 || (!player2 && participant2 !== undefined)) {
- throw new Error(`Invalid entered name/s.`);
- }
- if (
- participant2 !== undefined &&
- player1.condition !== player2.condition
- ) {
- throw new Error(`Choose players with equal condition.`);
- }
- if (participant2 === undefined) {
- player1.power += 20;
- return `The ${player1.name} successfully completed the game ${typeOfGame}.`;
- }
- let winner = '';
- if (player1.power > player2.power) {
- winner = player1.name;
- player1.wins++;
- } else if (player1.power < player2.power) {
- winner = player2.name;
- player2.wins++;
- }
- if (!winner) {
- return `There is no winner.`;
- }
- return `The ${winner} is winner in the game ${typeOfGame}.`;
- }
- toString() {
- let result = [
- `${this.organizer} will take ${this.listOfParticipants.length} participants on camping to ${this.location}`,
- ];
- this.listOfParticipants
- .sort((a, b) => b.wins - a.wins)
- .forEach((p) =>
- result.push(
- `${p.name} - ${p.condition} - ${p.power} - ${p.wins}`
- )
- );
- return result.join('\n');
- }
- _findParticipantsIdx(name) {
- return this.listOfParticipants.findIndex((p) => p.name === name);
- }
- _findParticipant(name) {
- return this.listOfParticipants.find((p) => p.name === name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement