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 (condition !== 'child' && condition !== 'student' && condition !== 'collegian') {
- throw new Error('Unsuccessful registration at the camp.');
- }
- if (this.listOfParticipants.some(partic => partic.name == name)) {
- return `The ${name} is already registered at the camp.`;
- }
- let price = -1;
- if (condition == 'child') {
- price = this.priceForTheCamp.child;
- }
- else if (condition == 'student') {
- price = this.priceForTheCamp.student;
- }
- else if (condition == 'collegian') {
- price = this.priceForTheCamp.collegian;
- }
- if (Number(money) < price) {
- return `The money is not enough to pay the stay at the camp.`;
- }
- let participan = {
- name: name,
- condition: condition,
- power: 100,
- wins: 0
- };
- this.listOfParticipants.push(participan);
- return `The ${participan.name} was successfully registered.`
- }
- unregisterParticipant (name){
- if (!this.listOfParticipants.some(p => p.name == name)) {
- throw new Error(`The ${name} is not registered in the camp.`)
- }
- this.listOfParticipants = removeByAttr(this.listOfParticipants, 'name', name);
- function removeByAttr(arr, attr, value){
- var i = arr.length;
- while(i--){
- if( arr[i]
- && arr[i].hasOwnProperty(attr)
- && (arguments.length > 2 && arr[i][attr] === value ) ){
- arr.splice(i,1);
- }
- }
- return arr;
- }
- return `The ${name} removed successfully.`;
- }
- timeToPlay (typeOfGame, participant1, participant2){
- if (typeOfGame == 'WaterBalloonFights') {
- let player1 = this.listOfParticipants.find(p => p.name == participant1);
- let player2 = this.listOfParticipants.find(p => p.name == participant2);
- if (!player1 || !player2) {
- throw new Error('Invalid entered name/s.')
- }
- if (player1.condition != player2.condition) {
- throw new Error('Choose players with equal condition.');
- }
- if (player1.power > player2.power) {
- player1.wins += 1;
- return `The ${player1.name} is winner in the game ${typeOfGame}.`
- }
- if (player1.power < player2.power) {
- player2.wins += 1;
- return `The ${player2.name} is winner in the game ${typeOfGame}.`
- }
- else {
- return `There is no winner.`;
- }
- }
- else if(typeOfGame == 'Battleship'){
- let player1 = this.listOfParticipants.find(p => p.name == participant1);
- if (!player1) {
- throw new Error('Invalid entered name/s.')
- }
- player1.power += 20;
- return `The ${player1.name} successfully completed the game ${typeOfGame}.`;
- }
- }
- toString () {
- this.listOfParticipants.sort((a, b) => b.power - a.power);
- let result = `${this.organizer} will take ${this.listOfParticipants.length} participants on camping to ${this.location}\n`;
- for (let index = 0; index < this.listOfParticipants.length; index++) {
- const player = this.listOfParticipants[index];
- result += `${player.name} - ${player.condition} - ${player.power} - ${player.wins}\n`
- }
- return result.trim();
- }
- }
- const summerCamp = new SummerCamp("Jane Austen", "Pancharevo Sofia 1137, Bulgaria");
- console.log(summerCamp.registerParticipant("Petar Petarson", "student", 300));
- console.log(summerCamp.timeToPlay("Battleship", "Petar Petarson"));
- console.log(summerCamp.registerParticipant("Sara Dickinson", "child", 200));
- //console.log(summerCamp.timeToPlay("WaterBalloonFights", "Petar Petarson", "Sara Dickinson"));
- console.log(summerCamp.registerParticipant("Dimitur Kostov", "student", 300));
- console.log(summerCamp.timeToPlay("WaterBalloonFights", "Petar Petarson", "Dimitur Kostov"));
- console.log(summerCamp.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement