Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace FundamentalsFinalExam
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<Town> towns = new List<Town>();
  12.  
  13. while (true)
  14. {
  15. string input = Console.ReadLine();
  16.  
  17. if (input == "Sail")
  18. {
  19. break;
  20. }
  21.  
  22. string[] data = input.Split("||").ToArray();
  23.  
  24. string name = data[0];
  25. int population = int.Parse(data[1]);
  26. int gold = int.Parse(data[2]);
  27.  
  28. Town currentTown = new Town(name, population, gold);
  29.  
  30. if (!towns.Any(x => x.Name == name))
  31. {
  32. towns.Add(currentTown);
  33. }
  34. else
  35. {
  36. Town temp = towns.FirstOrDefault(x => x.Name == name);
  37.  
  38. temp.Gold += gold;
  39. temp.People += population;
  40. }
  41. }
  42.  
  43. while (true)
  44. {
  45. string input = Console.ReadLine();
  46.  
  47. if (input == "End")
  48. {
  49. break;
  50. }
  51.  
  52. string[] data = input.Split("=>").ToArray();
  53.  
  54. string command = data[0];
  55.  
  56. if (command == "Plunder")
  57. {
  58. string name = data[1];
  59. int population = int.Parse(data[2]);
  60. int gold = int.Parse(data[3]);
  61.  
  62. Town currentTown = towns.FirstOrDefault(x => x.Name == name);
  63.  
  64. currentTown.People -= population;
  65. currentTown.Gold -= gold;
  66.  
  67. Console.WriteLine($"{name} plundered! {gold} gold stolen, {population} citizens killed.");
  68.  
  69. if (currentTown.People <= 0 || currentTown.Gold <= 0)
  70. {
  71. Console.WriteLine($"{name} has been wiped off the map!");
  72.  
  73. towns.Remove(currentTown);
  74. }
  75. }
  76. else if (command == "Prosper")
  77. {
  78. string name = data[1];
  79. int gold = int.Parse(data[2]);
  80.  
  81. Town currentTown = towns.FirstOrDefault(x => x.Name == name);
  82.  
  83. if (gold < 0)
  84. {
  85. Console.WriteLine($"Gold added cannot be a negative number!");
  86. }
  87. else
  88. {
  89.  
  90. currentTown.Gold += gold;
  91.  
  92. Console.WriteLine($"{gold} gold added to the city treasury. {name} now has {currentTown.Gold} gold.");
  93. }
  94. }
  95. }
  96.  
  97. if (towns.Count > 0)
  98. {
  99. Console.WriteLine($"Ahoy, Captain! There are {towns.Count} wealthy settlements to go to:");
  100.  
  101. foreach (var town in towns.OrderByDescending(x => x.Gold).ThenBy(x => x.Name))
  102. {
  103. Console.WriteLine($"{town.Name} -> Population: {town.People} citizens, Gold: {town.Gold} kg");
  104. }
  105. }
  106. else
  107. {
  108. Console.WriteLine($"Ahoy, Captain! All targets have been plundered and destroyed!");
  109. }
  110. }
  111.  
  112.  
  113. class Town
  114. {
  115. public Town(string name, int people, int gold)
  116. {
  117. Name = name;
  118. People = people;
  119. Gold = gold;
  120. }
  121.  
  122. public string Name { get; set; }
  123. public int People { get; set; }
  124. public int Gold { get; set; }
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement