Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6.  
  7. public class FootballTeamGeneratorMain
  8. {
  9.     public static List<FootballTeam> footballTeams = new List<FootballTeam>();
  10.     public static StringBuilder result = new StringBuilder();
  11.  
  12.     public static void Main()
  13.     {
  14.         var teamName = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  15.         var footballTeam = new FootballTeam(teamName[1]);
  16.  
  17.         footballTeams.Add(footballTeam);
  18.  
  19.         var input = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  20.        
  21.         while (!input[0].Equals("END"))
  22.         {
  23.             try
  24.             {
  25.                 CheckIfFootballTeamExist(input[1]);
  26.                 switch (input[0])
  27.                 {
  28.                     case "Add":
  29.                         AddPlayerToTeam(input);
  30.                         break;
  31.                     case "Remove":
  32.                         RemovePlayerFromTeam(input);
  33.                         break;
  34.                     case "Rating":
  35.                         GetTeamRating(input[1]);
  36.                         break;
  37.                     case "Team":
  38.                         footballTeams.Add(footballTeam);
  39.                         break;
  40.                 }
  41.             }
  42.             catch (Exception ex)
  43.             {
  44.                 result.AppendLine(ex.Message);
  45.             }
  46.             input = Console.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  47.         }
  48.  
  49.         Console.WriteLine(result);
  50.  
  51.     }
  52.  
  53.     private static void GetTeamRating(string teamName)
  54.     {
  55.         var footballTeamIndex = FindFootballTeamIndex(teamName);
  56.  
  57.         result.AppendLine($"{teamName} - {footballTeams[footballTeamIndex].AverageSkillLevelOfAllPlayersInTheTeam()}");
  58.     }
  59.  
  60.     private static void RemovePlayerFromTeam(string[] input)
  61.     {
  62.         var teamName = input[1];
  63.         var playerName = input[2];
  64.         var footballTeamIndex = FindFootballTeamIndex(teamName);
  65.  
  66.         footballTeams[footballTeamIndex].RemovePlayer(playerName);
  67.     }
  68.  
  69.     private static void AddPlayerToTeam(string[] input)
  70.     {
  71.         var teamName = input[1];
  72.         var playerName = input[2];
  73.         var copyPartArray = new string[5];
  74.  
  75.         // copying all the stats from the input to a new Array
  76.         Array.Copy(input, 3, copyPartArray, 0, 5);
  77.  
  78.         var playerStats = copyPartArray.Select(double.Parse).ToArray();
  79.    
  80.         // using params I use as a parameter just single Array, not 5 parameters
  81.         var stats = new Stats(playerStats);
  82.  
  83.         var player = new Player(playerName, stats);
  84.  
  85.         var footballTeamIndex = FindFootballTeamIndex(teamName);
  86.  
  87.         footballTeams[footballTeamIndex].AddPlayer(player);
  88.     }
  89.  
  90.     private static int FindFootballTeamIndex(string teamName)
  91.     {
  92.         var footballTeamIndex = footballTeams.
  93.             FindIndex(t => t.Name.Equals(teamName));
  94.         return footballTeamIndex;
  95.     }
  96.  
  97.     private static void CheckIfFootballTeamExist(string name)
  98.     {
  99.         bool exist = footballTeams.Any(t => t.Name.Equals(name));
  100.  
  101.         if (!exist)
  102.         {
  103.             throw new ArgumentException($"Team {name} does not exist.");
  104.         }
  105.     }
  106. }
  107.  
  108.  
  109.  
  110. public class FootballTeam
  111. {
  112.     public List<Player> players { get; private set; }
  113.     private string name;
  114.     //private double rating;
  115.  
  116.     public FootballTeam(string name)
  117.     {
  118.         this.Name = name;
  119.         players = new List<Player>();
  120.     }
  121.  
  122.     public string Name
  123.     {
  124.         get { return name; }
  125.         set
  126.         {
  127.             if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
  128.             {
  129.                 throw new ArgumentException("A name should not be empty. "); // may be there is space after the dot (.)
  130.             }
  131.             name = value;
  132.         }
  133.     }
  134.  
  135.     //public double Rating
  136.     //{
  137.     //    get
  138.     //    {
  139.     //        AverageSkillLevelOfAllPlayersInTheTeam();
  140.     //        return rating;
  141.     //    }
  142.  
  143.     //    private set
  144.     //    {
  145.     //        rating = value; // this may throw exception
  146.     //    }
  147.     //}
  148.  
  149.     public double AverageSkillLevelOfAllPlayersInTheTeam()
  150.     {
  151.         double averageSkillLevel = 0d;
  152.         if (players.Count > 0)
  153.         {
  154.             averageSkillLevel = players.Average(p => p.OverallSkillLevel());
  155.             //this.Rating = Math.Ceiling(averageSkillLevel);
  156.         }
  157.         return Math.Ceiling(averageSkillLevel);
  158.     }
  159.  
  160.     public void AddPlayer(Player player)
  161.     {
  162.         players.Add(player);
  163.     }
  164.  
  165.     public void RemovePlayer(string playerName)
  166.     {
  167.         var playerIndex = players.FindIndex(p => p.Name.Equals(playerName));
  168.  
  169.         if (playerIndex < 0)
  170.         {
  171.             throw new ArgumentException($"Player {playerName} is not in {name} team. "); // may be there is space after the dot (.)
  172.         }
  173.         players.RemoveAt(playerIndex);
  174.     }
  175. }
  176.  
  177.  
  178.  
  179. public class Player
  180. {
  181.     private string name;
  182.     public Stats stats { get; set; }
  183.  
  184.     public Player(string name, Stats stats)
  185.     {
  186.         this.Name = name;
  187.         this.stats = stats;
  188.     }
  189.  
  190.     public string Name
  191.     {
  192.         get { return name; }
  193.         set
  194.         {
  195.             if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value))
  196.             {
  197.                 throw new ArgumentException("A name should not be empty. "); // may be there is space after the dot (.)
  198.             }
  199.             name = value;
  200.         }
  201.     }
  202.  
  203.     public double OverallSkillLevel()
  204.     {
  205.         return (stats.Dribble + stats.Endurance + stats.Passing + stats.Shooting + stats.Sprint) / 5;
  206.     }
  207. }
  208.  
  209.  
  210.  
  211. public class Stats
  212. {
  213.     private const double lowLimit = 0;
  214.     private const double highLimit = 100;
  215.  
  216.     public Stats(params double[] statsValues)
  217.     {
  218.         this.Endurance = statsValues[0];
  219.         this.Sprint = statsValues[1];
  220.         this.Dribble = statsValues[2];
  221.         this.Passing = statsValues[3];
  222.         this.Shooting = statsValues[4];
  223.     }
  224.  
  225.     private double endurance;
  226.     private double sprint;
  227.     private double dribble;
  228.     private double passing;
  229.     private double shooting;
  230.  
  231.     public double Endurance
  232.     {
  233.         get { return endurance; }
  234.         set
  235.         {
  236.             if (value < lowLimit || value > highLimit)
  237.             {
  238.                 throw new ArgumentException($"Endurance should be between 0 and 100.");
  239.             }
  240.             endurance = value;
  241.         }
  242.     }
  243.  
  244.     public double Sprint
  245.     {
  246.         get { return sprint; }
  247.         set
  248.         {
  249.             if (value < lowLimit || value > highLimit)
  250.             {
  251.                 throw new ArgumentException($"Sprint should be between 0 and 100.");
  252.             }
  253.             sprint = value;
  254.         }
  255.     }
  256.  
  257.     public double Dribble
  258.     {
  259.         get { return dribble; }
  260.         set
  261.         {
  262.             if (value < lowLimit || value > highLimit)
  263.             {
  264.                 throw new ArgumentException($"Dribble should be between 0 and 100.");
  265.             }
  266.             dribble = value;
  267.         }
  268.     }
  269.  
  270.     public double Passing
  271.     {
  272.         get { return passing; }
  273.         set
  274.         {
  275.             if (value < lowLimit || value > highLimit)
  276.             {
  277.                 throw new ArgumentException($"Passing should be between 0 and 100.");
  278.             }
  279.             passing = value;
  280.         }
  281.     }
  282.  
  283.     public double Shooting
  284.     {
  285.         get { return shooting; }
  286.         set
  287.         {
  288.             if (value < lowLimit || value > highLimit)
  289.             {
  290.                 throw new ArgumentException($"Shooting should be between 0 and 100.");
  291.             }
  292.             shooting = value;
  293.         }
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement