dilyana2001

Untitled

Oct 23rd, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  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.istOfParticipants = [];
  7. }
  8.  
  9. registerParticipant(name, condition, money) {
  10.  
  11. if (!this.priceForTheCamp.hasOwnProperty(condition)) {
  12. throw new Error(`Unsuccessful registration at the camp.`)
  13. }
  14. if (this.istOfParticipants.some(n => n.name == name)) {
  15. return `The ${name} is already registered at the camp.`;
  16. }
  17. if (money < this.priceForTheCamp[condition]) {
  18. return `The money is not enough to pay the stay at the camp.`;
  19. }
  20. this.istOfParticipants.push({ name, condition, power: 100, wins: 0 });
  21. return `The ${name} was successfully registered.`;
  22. }
  23. unregisterParticipant(name) {
  24. if (!this.istOfParticipants.some(n => n.name == name)) {
  25. throw new Error(`The ${name} is not registered in the camp.`)
  26. }
  27. let nameForUnregister = this.istOfParticipants.find(n => n.name == name);
  28. let index = this.istOfParticipants.indexOf(nameForUnregister);
  29. this.istOfParticipants.splice(index, 1);
  30. return `The ${name} removed successfully.`;
  31. }
  32.  
  33. timeToPlay(typeOfGame, participant1, participant2) {
  34.  
  35. let participant1Obj = this.istOfParticipants.find(c => c.name == participant1);
  36. let participant2Obj = this.istOfParticipants.find(c => c.name == participant2);
  37.  
  38. if (participant1Obj.name == undefined && participant2Obj.name == undefined) {
  39. throw new Error(`Invalid entered name/s.`);
  40. }
  41.  
  42. if (typeOfGame == 'Battleship') {
  43. participant1Obj.power = Number(participant1Obj.power) + 20;
  44. participant1Obj.wins = Number(participant1Obj.wins) + 1;
  45.  
  46. return `The ${participant1Obj.name} successfully completed the game ${typeOfGame}.`
  47. }
  48. if (typeOfGame == 'WaterBalloonFights') {
  49. if (participant1Obj.condition != 'child' || participant2Obj.condition != 'child') {
  50. throw new Error(`Choose players with equal condition.`);
  51. }
  52. if (participant1Obj.power > participant2Obj.power) {
  53. participant1Obj.wins = Number(participant1Obj.wins) + 1;
  54. } else if (participant1Obj.power < participant2Obj.power) {
  55. participant2Obj.wins = Number(participant2Obj.wins) + 1;
  56. } else {
  57. return `There is no winner.`
  58. }
  59. let winner = '';
  60. if (participant1Obj.power > participant2Obj.power) {
  61. winner = participant1Obj.name
  62. } else {
  63. winner = participant2Obj.name
  64. }
  65. if (winner) {
  66. return `The ${winner} is winner in the game ${typeOfGame}.`;
  67. }
  68. }
  69. }
  70.  
  71. toString() {
  72. let result = [];
  73. result.push(`${this.organizer} will take ${this.istOfParticipants.length} participants on camping to ${this.location}`);
  74. let sorted = this.istOfParticipants.sort((a, b) => b.wins - a.wins);
  75. for (const part of sorted) {
  76. result.push(`${part.name} ${part.condition} - ${part.power} - ${part.wins}`)
  77. }
  78. return result.join('\n');
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment