Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- public class FootballTeamGeneratorMain
- {
- public static List<FootballTeam> footballTeams = new List<FootballTeam>();
- public static StringBuilder result = new StringBuilder();
- public static void Main()
- {
- var teamName = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- var footballTeam = new FootballTeam(teamName[1]);
- footballTeams.Add(footballTeam);
- var input = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- while (!input[0].Equals("END"))
- {
- try
- {
- CheckIfFootballTeamExist(input[1]);
- switch (input[0])
- {
- case "Add":
- AddPlayerToTeam(input);
- break;
- case "Remove":
- RemovePlayerFromTeam(input);
- break;
- case "Rating":
- GetTeamRating(input[1]);
- break;
- case "Team":
- footballTeams.Add(footballTeam);
- break;
- }
- }
- catch (Exception ex)
- {
- result.AppendLine(ex.Message);
- }
- input = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
- }
- Console.WriteLine(result);
- }
- private static void GetTeamRating(string teamName)
- {
- var footballTeamIndex = FindFootballTeamIndex(teamName);
- result.AppendLine($"{teamName} - {footballTeams[footballTeamIndex].AverageSkillLevelOfAllPlayersInTheTeam()}");
- }
- private static void RemovePlayerFromTeam(string[] input)
- {
- var teamName = input[1];
- var playerName = input[2];
- var footballTeamIndex = FindFootballTeamIndex(teamName);
- footballTeams[footballTeamIndex].RemovePlayer(playerName);
- }
- private static void AddPlayerToTeam(string[] input)
- {
- var teamName = input[1];
- var playerName = input[2];
- var copyPartArray = new string[5];
- // copying all the stats from the input to a new Array
- Array.Copy(input, 3, copyPartArray, 0, 5);
- var playerStats = copyPartArray.Select(double.Parse).ToArray();
- // using params I use as a parameter just single Array, not 5 parameters
- var stats = new Stats(playerStats);
- var player = new Player(playerName, stats);
- var footballTeamIndex = FindFootballTeamIndex(teamName);
- footballTeams[footballTeamIndex].AddPlayer(player);
- }
- private static int FindFootballTeamIndex(string teamName)
- {
- var footballTeamIndex = footballTeams.
- FindIndex(t => t.Name.Equals(teamName));
- return footballTeamIndex;
- }
- private static void CheckIfFootballTeamExist(string name)
- {
- bool exist = footballTeams.Any(t => t.Name.Equals(name));
- if (!exist)
- {
- throw new ArgumentException($"Team {name} does not exist.");
- }
- }
- }
- public class FootballTeam
- {
- public List<Player> players { get; private set; }
- private string name;
- //private double rating;
- public FootballTeam(string name)
- {
- this.Name = name;
- players = new List<Player>();
- }
- public string Name
- {
- get { return name; }
- set
- {
- if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
- {
- throw new ArgumentException("A name should not be empty. "); // may be there is space after the dot (.)
- }
- name = value;
- }
- }
- //public double Rating
- //{
- // get
- // {
- // AverageSkillLevelOfAllPlayersInTheTeam();
- // return rating;
- // }
- // private set
- // {
- // rating = value; // this may throw exception
- // }
- //}
- public double AverageSkillLevelOfAllPlayersInTheTeam()
- {
- double averageSkillLevel = 0d;
- if (players.Count > 0)
- {
- averageSkillLevel = players.Average(p => p.OverallSkillLevel());
- //this.Rating = Math.Ceiling(averageSkillLevel);
- }
- return Math.Ceiling(averageSkillLevel);
- }
- public void AddPlayer(Player player)
- {
- players.Add(player);
- }
- public void RemovePlayer(string playerName)
- {
- var playerIndex = players.FindIndex(p => p.Name.Equals(playerName));
- if (playerIndex < 0)
- {
- throw new ArgumentException($"Player {playerName} is not in {name} team. "); // may be there is space after the dot (.)
- }
- players.RemoveAt(playerIndex);
- }
- }
- public class Player
- {
- private string name;
- public Stats stats { get; set; }
- public Player(string name, Stats stats)
- {
- this.Name = name;
- this.stats = stats;
- }
- public string Name
- {
- get { return name; }
- set
- {
- if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
- {
- throw new ArgumentException("A name should not be empty. "); // may be there is space after the dot (.)
- }
- name = value;
- }
- }
- public double OverallSkillLevel()
- {
- return (stats.Dribble + stats.Endurance + stats.Passing + stats.Shooting + stats.Sprint) / 5;
- }
- }
- public class Stats
- {
- private const double lowLimit = 0;
- private const double highLimit = 100;
- public Stats(params double[] statsValues)
- {
- this.Endurance = statsValues[0];
- this.Sprint = statsValues[1];
- this.Dribble = statsValues[2];
- this.Passing = statsValues[3];
- this.Shooting = statsValues[4];
- }
- private double endurance;
- private double sprint;
- private double dribble;
- private double passing;
- private double shooting;
- public double Endurance
- {
- get { return endurance; }
- set
- {
- if (value < lowLimit || value > highLimit)
- {
- throw new ArgumentException($"Endurance should be between 0 and 100.");
- }
- endurance = value;
- }
- }
- public double Sprint
- {
- get { return sprint; }
- set
- {
- if (value < lowLimit || value > highLimit)
- {
- throw new ArgumentException($"Sprint should be between 0 and 100.");
- }
- sprint = value;
- }
- }
- public double Dribble
- {
- get { return dribble; }
- set
- {
- if (value < lowLimit || value > highLimit)
- {
- throw new ArgumentException($"Dribble should be between 0 and 100.");
- }
- dribble = value;
- }
- }
- public double Passing
- {
- get { return passing; }
- set
- {
- if (value < lowLimit || value > highLimit)
- {
- throw new ArgumentException($"Passing should be between 0 and 100.");
- }
- passing = value;
- }
- }
- public double Shooting
- {
- get { return shooting; }
- set
- {
- if (value < lowLimit || value > highLimit)
- {
- throw new ArgumentException($"Shooting should be between 0 and 100.");
- }
- shooting = value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement