Advertisement
Mitfreex

footballTeam-Classes exam 22/10/2022

Oct 22nd, 2022
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class footballTeam {
  2.  
  3.     constructor(clubName, country) {
  4.         this.clubName = clubName;
  5.         this.country = country
  6.         this.invitedPlayers = [];
  7.     }
  8.  
  9.     newAdditions(footballPlayers) {
  10.  
  11.         for (let currPlayer of footballPlayers) {
  12.  
  13.             let [name, age, playerValue] = currPlayer.split('/');
  14.             playerValue = Number(playerValue);
  15.             age = Number(age);
  16.  
  17.             let newPlayer = {
  18.                 name,
  19.                 age,
  20.                 playerValue
  21.             };
  22.  
  23.             let foundPlayer = this.invitedPlayers.find(x => x.name == name);
  24.  
  25.             if (foundPlayer) {
  26.  
  27.                 let foundValue = foundPlayer.playerValue;
  28.  
  29.                 if (foundValue < playerValue) {
  30.                     foundPlayer.playerValue += playerValue;
  31.                 }
  32.             } else {
  33.                 this.invitedPlayers.push(newPlayer);
  34.             }
  35.  
  36.         }
  37.  
  38.         let allPlayers = this.invitedPlayers.reduce((acc, x) => {
  39.             acc.push(x.name);
  40.             return acc;
  41.         }, []);
  42.  
  43.         let finalMessage = `You successfully invite ${allPlayers.join(', ')}.`;
  44.  
  45.         return finalMessage;
  46.  
  47.     }
  48.  
  49.     signContract(selectedPlayer) {
  50.  
  51.         let [sPlayerName, offer] = selectedPlayer.split('/');
  52.  
  53.         offer = Number(offer);
  54.  
  55.         let foundPlayer = this.invitedPlayers.find(x => x.name == sPlayerName);
  56.  
  57.         if (!foundPlayer) {
  58.  
  59.             throw new Error(`${sPlayerName} is not invited to the selection list!`)
  60.         }
  61.  
  62.         if (foundPlayer.playerValue > offer) {
  63.             throw (`The manager's offer is not enough to sign a contract with ${sPlayerName}, ${foundPlayer.playerValue-offer} million more are needed to sign the contract!`);
  64.  
  65.        } else {
  66.            foundPlayer.playerValue = 'Bought';
  67.            return `Congratulations! You sign a contract with ${sPlayerName} for ${offer} million dollars.`;
  68.        }
  69.  
  70.  
  71.  
  72.    }
  73.  
  74.    ageLimit(name, age) {
  75.  
  76.        let limitAge = Number(age);
  77.  
  78.        let foundPlayer = this.invitedPlayers.find(x => x.name == name);
  79.  
  80.        if (!foundPlayer) {
  81.            throw new Error(`${name} is not invited to the selection list!`);
  82.        } else {
  83.  
  84.            let difAge = foundPlayer.age - limitAge;
  85.  
  86.            if (difAge > -5 && difAge < 0) {
  87.  
  88.                return `${name} will sign a contract for ${Math.abs(difAge)} years with ${this.clubName} in ${this.country}!`
  89.  
  90.  
  91.            } else if (difAge <= -5 && difAge < 0) {
  92.  
  93.                return `${name} will sign a full 5 years contract for ${this.clubName} in ${this.country}!`;
  94.  
  95.            } else if (difAge >= 0) {
  96.  
  97.                return `${name} is above age limit!`;
  98.  
  99.            }
  100.  
  101.  
  102.        }
  103.  
  104.  
  105.    }
  106.  
  107.    transferWindowResult() {
  108.  
  109.        let allPlayers = this.invitedPlayers.reduce((acc, x) => {
  110.  
  111.            acc.push(`Player ${x.name}-${x.playerValue}`)
  112.            return acc;
  113.        }, []);
  114.  
  115.        return `Players list:\n${allPlayers.join('\n')}`
  116.    }
  117.  
  118.  
  119.  
  120. }
  121.  
  122. let fTeam = new footballTeam("Barcelona", "Spain");
  123. console.log(fTeam.newAdditions(["Kylian Mbappé/23/160", "Lionel Messi/35/50", "Pau Torres/25/52"]));
  124. console.log(fTeam.signContract("Kylian Mbappé/240"));
  125. console.log(fTeam.ageLimit("Kylian Mbappé", 30));
  126. console.log(fTeam.transferWindowResult());
  127.  
  128. //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement