Не работает метод IComparable для первого списка - C#
Формулировка задачи:
Собственно насколько я понял выдаёт ошибку при сравнении элементов списка
Почему это происходит.
Как это можно исправить.
using System; using System.Collections.Generic; using System.Linq; class Program { class AirplanesProperties : IComparable<AirplanesProperties> { public int Number { get; set; } public int ArrivalTime { get; set; } public string City { get; set; } public string Airline { get; set; } public int Terminal { get; set; } public int ShippingFlight { get; set; } public int Bgate { get; set; } public AirplanesProperties() { } public AirplanesProperties(int number, int arrivalTime) { Number = number; ArrivalTime = arrivalTime; } public int CompareTo(AirplanesProperties obj) { if (ArrivalTime > obj.ArrivalTime) return 1; else if (ArrivalTime < obj.ArrivalTime) return -1; return 0; } public override string ToString() { return String.Format(" {1}\tnumber: {0:C}\tarrivalTime: ", ArrivalTime, Number); } } #region ArrivalTime1 private static void Arrivaltime1() { int x, y, z, k; Console.WriteLine("arrivalTime1"); x = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("arrivalTime2"); y = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("arrivalTime3"); z = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("arrivalTime4"); k = Convert.ToInt32(Console.ReadLine()); List<AirplanesProperties> fligths = new List<AirplanesProperties>() { new AirplanesProperties { Number = 1, ArrivalTime = x, }, new AirplanesProperties { Number = 2, ArrivalTime = y, }, new AirplanesProperties { Number = 3, ArrivalTime = z, }, new AirplanesProperties { Number = 4, ArrivalTime = k, } }; fligths.Sort(); foreach (var item in fligths) Console.WriteLine(item); } #endregion #region Airline private static void Airline() { List<AirplanesProperties> fligth = new List<AirplanesProperties>() { new AirplanesProperties { Number = 1, City = "kiyev", Airline = "PAU", Terminal = 1, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 }, new AirplanesProperties { Number = 2, City = "kiyev", Airline = "PAU", Terminal = 1, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 }, new AirplanesProperties { Number = 3, City = "Odessa", Airline = "MAU", Terminal = 3, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 }, new AirplanesProperties { Number = 4, City = "Odessa", Airline = "MAU", Terminal = 5, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 }, new AirplanesProperties { Number = 4, City = "Odessa", Airline = "RAU", Terminal = 5, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 }, new AirplanesProperties { Number = 5, City = "Odessa", Airline = "RAU", Terminal = 5, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 } }; int n = Convert.ToInt32(Console.ReadLine()); var filterFlight = fligth.Where(x => x.Number != n); foreach (var x in filterFlight) { Console.WriteLine(x.Airline + x.ArrivalTime + x.Bgate + x.City + x.Number); } } #endregion #region gate private static void gate() { List<AirplanesProperties> fligth = new List<AirplanesProperties>() { new AirplanesProperties { Number = 1, City = "kiyev", Airline = "PAU", Terminal = 1, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 }, new AirplanesProperties { Number = 1, City = "kiyev", Airline = "PAU", Terminal = 1, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 }, new AirplanesProperties { Number = 1, City = "Odessa", Airline = "MAU", Terminal = 3, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 }, new AirplanesProperties { Number = 1, City = "Odessa", Airline = "MAU", Terminal = 5, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 }, new AirplanesProperties { Number = 1, City = "Odessa", Airline = "RAU", Terminal = 5, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 }, new AirplanesProperties { Number = 1, City = "Odessa", Airline = "MAU", Terminal = 5, ArrivalTime = 0, ShippingFlight = 0, Bgate = 1 } }; string z = Console.ReadLine(); var result = fligth.Where(x => x.Airline == z); foreach (var x in result) { Console.WriteLine(x.Airline + x.ArrivalTime + x.Bgate + x.City + x.Number); } } #endregion static void Main() { Console.WriteLine(@"Please, type the number: 1. arrivalTime1 2. udalenie elementa massiva 3. Poisk po aviakompanii "); int a = int.Parse(Console.ReadLine()); switch (a) { case 1: Arrivaltime1(); Console.WriteLine(""); break; case 2: Airline(); Console.WriteLine(""); break; case 3: gate(); Console.WriteLine(""); break; default: Console.WriteLine("Exit"); break; } Console.ReadKey(); } }
Решение задачи: «Не работает метод IComparable для первого списка»
textual
Листинг программы
public override string ToString() { return String.Format(" {0}\tnumber: {1:C}\tarrivalTime: ", this.arrivalTime, this.number); }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д