Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class footballTeam {
- constructor(clubName, country) {
- this.clubName = clubName;
- this.country = country
- this.invitedPlayers = [];
- }
- newAdditions(footballPlayers) {
- for (let currPlayer of footballPlayers) {
- let [name, age, playerValue] = currPlayer.split('/');
- playerValue = Number(playerValue);
- age = Number(age);
- let newPlayer = {
- name,
- age,
- playerValue
- };
- let foundPlayer = this.invitedPlayers.find(x => x.name == name);
- if (foundPlayer) {
- let foundValue = foundPlayer.playerValue;
- if (foundValue < playerValue) {
- foundPlayer.playerValue += playerValue;
- }
- } else {
- this.invitedPlayers.push(newPlayer);
- }
- }
- let allPlayers = this.invitedPlayers.reduce((acc, x) => {
- acc.push(x.name);
- return acc;
- }, []);
- let finalMessage = `You successfully invite ${allPlayers.join(', ')}.`;
- return finalMessage;
- }
- signContract(selectedPlayer) {
- let [sPlayerName, offer] = selectedPlayer.split('/');
- offer = Number(offer);
- let foundPlayer = this.invitedPlayers.find(x => x.name == sPlayerName);
- if (!foundPlayer) {
- throw new Error(`${sPlayerName} is not invited to the selection list!`)
- }
- if (foundPlayer.playerValue > offer) {
- 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!`);
- } else {
- foundPlayer.playerValue = 'Bought';
- return `Congratulations! You sign a contract with ${sPlayerName} for ${offer} million dollars.`;
- }
- }
- ageLimit(name, age) {
- let limitAge = Number(age);
- let foundPlayer = this.invitedPlayers.find(x => x.name == name);
- if (!foundPlayer) {
- throw new Error(`${name} is not invited to the selection list!`);
- } else {
- let difAge = foundPlayer.age - limitAge;
- if (difAge > -5 && difAge < 0) {
- return `${name} will sign a contract for ${Math.abs(difAge)} years with ${this.clubName} in ${this.country}!`
- } else if (difAge <= -5 && difAge < 0) {
- return `${name} will sign a full 5 years contract for ${this.clubName} in ${this.country}!`;
- } else if (difAge >= 0) {
- return `${name} is above age limit!`;
- }
- }
- }
- transferWindowResult() {
- let allPlayers = this.invitedPlayers.reduce((acc, x) => {
- acc.push(`Player ${x.name}-${x.playerValue}`)
- return acc;
- }, []);
- return `Players list:\n${allPlayers.join('\n')}`
- }
- }
- let fTeam = new footballTeam("Barcelona", "Spain");
- console.log(fTeam.newAdditions(["Kylian Mbappé/23/160", "Lionel Messi/35/50", "Pau Torres/25/52"]));
- console.log(fTeam.signContract("Kylian Mbappé/240"));
- console.log(fTeam.ageLimit("Kylian Mbappé", 30));
- console.log(fTeam.transferWindowResult());
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement