Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace Exercise9 {
  6. class Program {
  7. private static List<HistoricalEvent> events = new List<HistoricalEvent>();
  8. static void Main(string[] args) {
  9. int choiceDigit = -1;
  10. do {
  11. Menu();
  12. if (!Int32.TryParse(Console.ReadLine(), out choiceDigit))
  13. continue;
  14. switch (choiceDigit) {
  15. case 1: {
  16. Console.Clear();
  17. ShowEvents();
  18. break;
  19. }
  20. case 2: {
  21. Console.Clear();
  22. Add();
  23. break;
  24. }
  25. case 3: {
  26. Console.Clear();
  27. ShowEvents();
  28. Console.WriteLine("Количество дней между событиями: " + Difference());
  29. break;
  30. }
  31. case 4: {
  32. Console.Clear();
  33. try {
  34. ShowEventList(HistoricalEvent.FindLatestEvent(events));
  35. } catch(ListIsEmptyExeption e) {
  36. Console.WriteLine(e.Message);
  37. }
  38. break;
  39. }
  40. case 5: {
  41. Console.Clear();
  42. Console.Write("Введите название: ");
  43. ShowEventList(FindEvent(Console.ReadLine()));
  44. break;
  45. }
  46. case 6: {
  47. Console.Clear();
  48. try {
  49. events.Sort();
  50. } catch(InvalidCastException e) {
  51. Console.WriteLine(e.Message);
  52. }
  53. ShowEventList(events);
  54. break;
  55. }
  56. //case 7: {
  57. // Console.Clear();
  58. // ReadFromFile();
  59. // break;
  60. //}
  61. //case 8: {
  62. // Console.Clear();
  63. // break;
  64. //}
  65. default: {
  66. Console.Clear();
  67. continue;
  68. }
  69. }
  70. } while (choiceDigit != 0);
  71. Console.ReadKey();
  72. }
  73.  
  74. //private static void ReadFromFile() {
  75. // FileStream file = new FileStream("events.txt", FileMode.Open, FileAccess.Read);
  76. //}
  77.  
  78. private static void ShowEventList(List<HistoricalEvent> he) {
  79. uint count = 0;
  80. if (he.Count != 0)
  81. foreach (HistoricalEvent ev in he)
  82. Console.WriteLine($"{count++} - {ev.Event} - " + new DateTime(ev.Year, ev.Mounth, ev.Day).ToShortDateString());
  83. else
  84. Console.WriteLine("События не найдены");
  85. Console.WriteLine();
  86. }
  87.  
  88. private static int Difference() {
  89. int firstIndex, secondIndex;
  90. do {
  91. Console.WriteLine("Введите номера событий\n" +
  92. "Номер первого события: ");
  93. }
  94. while (!Int32.TryParse(Console.ReadLine(), out firstIndex));
  95. do {
  96. Console.WriteLine("Номер второго события: ");
  97. } while (!Int32.TryParse(Console.ReadLine(), out secondIndex));
  98. return events[firstIndex - 1] - events[secondIndex - 1];
  99. }
  100.  
  101. private static void Add() {
  102. Console.Write("Введите название события: ");
  103. string name = Console.ReadLine();
  104. Console.Write("Введите дату события DD-MM-YYYY: ");
  105. string[] date = Console.ReadLine().Split('-');
  106. try {
  107. events.Add(new HistoricalEvent(Convert.ToInt32(date[2]), Convert.ToInt32(date[1]), Convert.ToInt32(date[0]), name));
  108. } catch (DateExeption e) {
  109. Console.WriteLine(e.Message);
  110. } catch (ArgumentOutOfRangeException){
  111. Console.WriteLine("Неверный формат даты.");
  112. } catch (IndexOutOfRangeException) {
  113. Console.WriteLine("Неверный формат даты.");
  114. }
  115. }
  116.  
  117. private static void Menu() {
  118. Console.WriteLine("1 - Вывести список событий\n" +
  119. "2 - Добавить событие\n" +
  120. "3 - Вычислить разницу в днях между событиями\n" +
  121. "4 - Найти наиболее позднее событие\n" +
  122. "5 - Найти событие по названию\n" +
  123. "6 - Сортировка по имени\n" +
  124. //"7 - Добавить события из файла\n" +
  125. //"8 - Записать события в файл" +
  126. "0 - Выход");
  127. }
  128.  
  129. private static void ShowEvents() {
  130. if (events.Count == 0) {
  131. Console.WriteLine("Список пуст");
  132. return;
  133. }
  134. uint count = 1;
  135. foreach (HistoricalEvent ev in events) {
  136. Console.WriteLine(count++ + " - " + ev.Event + " - " + new DateTime(ev.Year, ev.Mounth, ev.Day).ToShortDateString());
  137. }
  138. Console.WriteLine();
  139. }
  140.  
  141. private static List<HistoricalEvent> FindEvent(string name) {
  142. List<HistoricalEvent> temp = new List<HistoricalEvent>();
  143. for (int i = 0; i < events.Count; i++)
  144. if (events[i].Event.ToLower().IndexOf(name.ToLower()) != -1)
  145. temp.Add(events[i]);
  146. return temp;
  147. }
  148. }
  149.  
  150. public class HistoricalEvent : IComparable {
  151. public int Day { get; private set; }
  152. public int Mounth { get; private set; }
  153. public int Year { get; private set; }
  154. public string Event { get; private set; }
  155.  
  156. public static int operator -(HistoricalEvent first, HistoricalEvent second) {
  157. return Math.Abs((new DateTime(first.Year, first.Mounth, first.Day) - new DateTime(second.Year, second.Mounth, second.Day)).Days);
  158. }
  159.  
  160. public static List<HistoricalEvent> FindLatestEvent(List<HistoricalEvent> events) {
  161. if (events.Count == 0)
  162. throw new ListIsEmptyExeption("Список пуст");
  163. List<HistoricalEvent> latestEvents = new List<HistoricalEvent>();
  164. events.Sort(new HistoricalEventDateComparer());
  165. for (int i = events.Count - 1; i >= 0; i--) {
  166. if (events[i] == events[events.Count - 1])
  167. latestEvents.Add(events[i]);
  168. }
  169. return latestEvents;
  170. }
  171.  
  172. public int CompareTo(object obj) {
  173. HistoricalEvent temp = (obj is HistoricalEvent) ? obj as HistoricalEvent : null;
  174. if (temp != null) {
  175. return String.Compare(this.Event, temp.Event);
  176. } else {
  177. throw new FormatException();
  178. }
  179. }
  180.  
  181. public static bool operator <(HistoricalEvent first, HistoricalEvent second) {
  182. return new DateTime(first.Year, first.Mounth, first.Day) < new DateTime(second.Year, second.Mounth, second.Day);
  183. }
  184.  
  185. public static bool operator >(HistoricalEvent first, HistoricalEvent second) {
  186. return new DateTime(first.Year, first.Mounth, first.Day) > new DateTime(second.Year, second.Mounth, second.Day);
  187. }
  188.  
  189. public static bool operator ==(HistoricalEvent first, HistoricalEvent second) {
  190. if (ReferenceEquals(second, null)) {
  191. if (ReferenceEquals(second, null))
  192. return true;
  193. else
  194. return false;
  195. }
  196. return new DateTime(first.Year, first.Mounth, first.Day) == new DateTime(second.Year, second.Mounth, second.Day);
  197. }
  198.  
  199. public static bool operator !=(HistoricalEvent first, HistoricalEvent second) {
  200. if (ReferenceEquals(second, null)) {
  201. if (ReferenceEquals(second, null))
  202. return false;
  203. else
  204. return true;
  205. }
  206. return new DateTime(first.Year, first.Mounth, first.Day) != new DateTime(second.Year, second.Mounth, second.Day);
  207. }
  208.  
  209. public HistoricalEvent() {
  210. }
  211.  
  212. public HistoricalEvent(int year, int mounth, int day, string name) {
  213.  
  214. if (!(new DateTime(year, mounth, day) < DateTime.Now)) {
  215. throw new DateExeption("Событие еще не произошло.");
  216. }
  217. this.Year = year;
  218. this.Mounth = mounth;
  219. this.Day = day;
  220. this.Event = name;
  221. }
  222. }
  223.  
  224. public class HistoricalEventDateComparer : IComparer<HistoricalEvent> {
  225. public int Compare(HistoricalEvent first, HistoricalEvent second) {
  226. if (new DateTime(first.Year, first.Mounth, first.Day) > new DateTime(second.Year, second.Mounth, second.Day))
  227. return 1;
  228. else if (new DateTime(first.Year, first.Mounth, first.Day) < new DateTime(second.Year, second.Mounth, second.Day))
  229. return -1;
  230. else
  231. return 0;
  232. }
  233. }
  234.  
  235. public class ListIsEmptyExeption : Exception {
  236. public ListIsEmptyExeption(string message) : base(message) {
  237. }
  238. }
  239.  
  240. public class DateExeption : Exception {
  241. public DateExeption(string message) : base(message) {
  242. }
  243. }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement