Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace Exercise9 {
- class Program {
- private static List<HistoricalEvent> events = new List<HistoricalEvent>();
- static void Main(string[] args) {
- int choiceDigit = -1;
- do {
- Menu();
- if (!Int32.TryParse(Console.ReadLine(), out choiceDigit))
- continue;
- switch (choiceDigit) {
- case 1: {
- Console.Clear();
- ShowEvents();
- break;
- }
- case 2: {
- Console.Clear();
- Add();
- break;
- }
- case 3: {
- Console.Clear();
- ShowEvents();
- Console.WriteLine("Количество дней между событиями: " + Difference());
- break;
- }
- case 4: {
- Console.Clear();
- try {
- ShowEventList(HistoricalEvent.FindLatestEvent(events));
- } catch(ListIsEmptyExeption e) {
- Console.WriteLine(e.Message);
- }
- break;
- }
- case 5: {
- Console.Clear();
- Console.Write("Введите название: ");
- ShowEventList(FindEvent(Console.ReadLine()));
- break;
- }
- case 6: {
- Console.Clear();
- try {
- events.Sort();
- } catch(InvalidCastException e) {
- Console.WriteLine(e.Message);
- }
- ShowEventList(events);
- break;
- }
- //case 7: {
- // Console.Clear();
- // ReadFromFile();
- // break;
- //}
- //case 8: {
- // Console.Clear();
- // break;
- //}
- default: {
- Console.Clear();
- continue;
- }
- }
- } while (choiceDigit != 0);
- Console.ReadKey();
- }
- //private static void ReadFromFile() {
- // FileStream file = new FileStream("events.txt", FileMode.Open, FileAccess.Read);
- //}
- private static void ShowEventList(List<HistoricalEvent> he) {
- uint count = 0;
- if (he.Count != 0)
- foreach (HistoricalEvent ev in he)
- Console.WriteLine($"{count++} - {ev.Event} - " + new DateTime(ev.Year, ev.Mounth, ev.Day).ToShortDateString());
- else
- Console.WriteLine("События не найдены");
- Console.WriteLine();
- }
- private static int Difference() {
- int firstIndex, secondIndex;
- do {
- Console.WriteLine("Введите номера событий\n" +
- "Номер первого события: ");
- }
- while (!Int32.TryParse(Console.ReadLine(), out firstIndex));
- do {
- Console.WriteLine("Номер второго события: ");
- } while (!Int32.TryParse(Console.ReadLine(), out secondIndex));
- return events[firstIndex - 1] - events[secondIndex - 1];
- }
- private static void Add() {
- Console.Write("Введите название события: ");
- string name = Console.ReadLine();
- Console.Write("Введите дату события DD-MM-YYYY: ");
- string[] date = Console.ReadLine().Split('-');
- try {
- events.Add(new HistoricalEvent(Convert.ToInt32(date[2]), Convert.ToInt32(date[1]), Convert.ToInt32(date[0]), name));
- } catch (DateExeption e) {
- Console.WriteLine(e.Message);
- } catch (ArgumentOutOfRangeException){
- Console.WriteLine("Неверный формат даты.");
- } catch (IndexOutOfRangeException) {
- Console.WriteLine("Неверный формат даты.");
- }
- }
- private static void Menu() {
- Console.WriteLine("1 - Вывести список событий\n" +
- "2 - Добавить событие\n" +
- "3 - Вычислить разницу в днях между событиями\n" +
- "4 - Найти наиболее позднее событие\n" +
- "5 - Найти событие по названию\n" +
- "6 - Сортировка по имени\n" +
- //"7 - Добавить события из файла\n" +
- //"8 - Записать события в файл" +
- "0 - Выход");
- }
- private static void ShowEvents() {
- if (events.Count == 0) {
- Console.WriteLine("Список пуст");
- return;
- }
- uint count = 1;
- foreach (HistoricalEvent ev in events) {
- Console.WriteLine(count++ + " - " + ev.Event + " - " + new DateTime(ev.Year, ev.Mounth, ev.Day).ToShortDateString());
- }
- Console.WriteLine();
- }
- private static List<HistoricalEvent> FindEvent(string name) {
- List<HistoricalEvent> temp = new List<HistoricalEvent>();
- for (int i = 0; i < events.Count; i++)
- if (events[i].Event.ToLower().IndexOf(name.ToLower()) != -1)
- temp.Add(events[i]);
- return temp;
- }
- }
- public class HistoricalEvent : IComparable {
- public int Day { get; private set; }
- public int Mounth { get; private set; }
- public int Year { get; private set; }
- public string Event { get; private set; }
- public static int operator -(HistoricalEvent first, HistoricalEvent second) {
- return Math.Abs((new DateTime(first.Year, first.Mounth, first.Day) - new DateTime(second.Year, second.Mounth, second.Day)).Days);
- }
- public static List<HistoricalEvent> FindLatestEvent(List<HistoricalEvent> events) {
- if (events.Count == 0)
- throw new ListIsEmptyExeption("Список пуст");
- List<HistoricalEvent> latestEvents = new List<HistoricalEvent>();
- events.Sort(new HistoricalEventDateComparer());
- for (int i = events.Count - 1; i >= 0; i--) {
- if (events[i] == events[events.Count - 1])
- latestEvents.Add(events[i]);
- }
- return latestEvents;
- }
- public int CompareTo(object obj) {
- HistoricalEvent temp = (obj is HistoricalEvent) ? obj as HistoricalEvent : null;
- if (temp != null) {
- return String.Compare(this.Event, temp.Event);
- } else {
- throw new FormatException();
- }
- }
- public static bool operator <(HistoricalEvent first, HistoricalEvent second) {
- return new DateTime(first.Year, first.Mounth, first.Day) < new DateTime(second.Year, second.Mounth, second.Day);
- }
- public static bool operator >(HistoricalEvent first, HistoricalEvent second) {
- return new DateTime(first.Year, first.Mounth, first.Day) > new DateTime(second.Year, second.Mounth, second.Day);
- }
- public static bool operator ==(HistoricalEvent first, HistoricalEvent second) {
- if (ReferenceEquals(second, null)) {
- if (ReferenceEquals(second, null))
- return true;
- else
- return false;
- }
- return new DateTime(first.Year, first.Mounth, first.Day) == new DateTime(second.Year, second.Mounth, second.Day);
- }
- public static bool operator !=(HistoricalEvent first, HistoricalEvent second) {
- if (ReferenceEquals(second, null)) {
- if (ReferenceEquals(second, null))
- return false;
- else
- return true;
- }
- return new DateTime(first.Year, first.Mounth, first.Day) != new DateTime(second.Year, second.Mounth, second.Day);
- }
- public HistoricalEvent() {
- }
- public HistoricalEvent(int year, int mounth, int day, string name) {
- if (!(new DateTime(year, mounth, day) < DateTime.Now)) {
- throw new DateExeption("Событие еще не произошло.");
- }
- this.Year = year;
- this.Mounth = mounth;
- this.Day = day;
- this.Event = name;
- }
- }
- public class HistoricalEventDateComparer : IComparer<HistoricalEvent> {
- public int Compare(HistoricalEvent first, HistoricalEvent second) {
- if (new DateTime(first.Year, first.Mounth, first.Day) > new DateTime(second.Year, second.Mounth, second.Day))
- return 1;
- else if (new DateTime(first.Year, first.Mounth, first.Day) < new DateTime(second.Year, second.Mounth, second.Day))
- return -1;
- else
- return 0;
- }
- }
- public class ListIsEmptyExeption : Exception {
- public ListIsEmptyExeption(string message) : base(message) {
- }
- }
- public class DateExeption : Exception {
- public DateExeption(string message) : base(message) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement