Advertisement
Prohause

Pokemon Trainer

Oct 23rd, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. ?using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace DefiningClasses
  6. {
  7. public class StartUp
  8. {
  9. public static void Main(string[] args)
  10. {
  11. var allTrainers = new List<Trainer>();
  12. string input;
  13.  
  14. while (!(input = Console.ReadLine()).Equals("Tournament"))
  15. {
  16. var tokens = input.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  17. var trainerName = tokens[0];
  18. var pokemonName = tokens[1];
  19. var pokemonElement = tokens[2];
  20. var pokemonHealth = int.Parse(tokens[3]);
  21.  
  22. if (!allTrainers.Any(p => p.GetName().Equals(trainerName)))
  23. {
  24. allTrainers.Add(new Trainer(trainerName));
  25. }
  26.  
  27. allTrainers.First(p => p.GetName().Equals(trainerName))
  28. .AddPokemon(new Pokemon(pokemonName, pokemonElement, pokemonHealth));
  29. }
  30.  
  31. while (!(input = Console.ReadLine()).Equals("End"))
  32. {
  33. foreach (var trainer in allTrainers)
  34. {
  35. if (trainer.HAsElement(input))
  36. {
  37. trainer.AddBadge();
  38. }
  39. else
  40. {
  41. trainer.DecreaseHealth();
  42. }
  43. }
  44. }
  45.  
  46. allTrainers.OrderByDescending(p => p.GetBadges()).ToList().ForEach(Console.WriteLine);
  47. }
  48. }
  49. }
  50. namespace DefiningClasses
  51. {
  52. internal class Pokemon
  53. {
  54. private readonly string _name;
  55. private readonly string _element;
  56. private int _health;
  57.  
  58. public Pokemon(string name, string element, int health)
  59. {
  60. _name = name;
  61. _element = element;
  62. Health = health;
  63. }
  64.  
  65. public int Health
  66. {
  67. get => _health;
  68. set => _health = value;
  69. }
  70.  
  71. public string GetElement()
  72. {
  73. return _element;
  74. }
  75. }
  76. }
  77. using System.Collections.Generic;
  78. using System.Linq;
  79.  
  80. namespace DefiningClasses
  81. {
  82. internal class Trainer
  83. {
  84. private readonly string _name;
  85. private int _badges;
  86. private List<Pokemon> _pokemonList;
  87.  
  88. public Trainer(string name)
  89. {
  90. _name = name;
  91. _badges = 0;
  92. _pokemonList = new List<Pokemon>();
  93. }
  94.  
  95. public int GetBadges()
  96. {
  97. return _badges;
  98. }
  99.  
  100. public string GetName()
  101. {
  102. return _name;
  103. }
  104.  
  105. public void AddBadge()
  106. {
  107. _badges++;
  108. }
  109.  
  110. public void AddPokemon(Pokemon pokemon)
  111. {
  112. _pokemonList.Add(pokemon);
  113. }
  114.  
  115. public void DecreaseHealth()
  116. {
  117. _pokemonList.ForEach(p => p.Health -= 10);
  118. _pokemonList = _pokemonList.Where(p => p.Health > 0).ToList();
  119. }
  120.  
  121. public bool HAsElement(string element)
  122. {
  123. return _pokemonList.Any(p => p.GetElement().Equals(element));
  124. }
  125.  
  126. public override string ToString()
  127. {
  128. return $"{_name} {_badges} {_pokemonList.Count}";
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement