Advertisement
Guest User

l6

a guest
Apr 8th, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.92 KB | None | 0 0
  1. using System;
  2.  
  3. public class Program {
  4.     public class Bilet {
  5.     private string name;
  6.     private int nomer;
  7.     private DateTime time1;
  8.     private DateTime time2;
  9.     private Decimal cost;
  10.    
  11.     // Конструктор по умолчанию
  12.         public Bilet()
  13.     {
  14.         name = "Неизвестно";
  15.         nomer = 0;
  16.         time1 = new DateTime(1980, 1, 1, 0, 0, 0);
  17.         time2 = new DateTime(1980, 1, 1, 0, 0, 0);
  18.         cost = 0;
  19.     }
  20.     // Конструктор с параметрами.
  21.     public Bilet(string na, int no, DateTime t1, DateTime t2, Decimal c)
  22.     {
  23.         name = na;
  24.         nomer = no;
  25.         time1 = t1;
  26.         time2 = t2;
  27.         cost = c;
  28.     }
  29.     // Конструктор копирования.
  30.     public Bilet(Bilet prev_bilet)
  31.     {
  32.         name = prev_bilet.name;
  33.         nomer = prev_bilet.nomer;
  34.         time1 = prev_bilet.time1;
  35.         time2 = prev_bilet.time2;
  36.         cost = prev_bilet.cost;
  37.     }
  38.    
  39.         public TimeSpan GetDuration() => time2.Subtract(time1);
  40.    
  41.     // Функции для присвоения значений полям.
  42.         public bool SetName(string n)
  43.     {
  44.         if (n.Length > 30)
  45.         {
  46.             return false;
  47.         }
  48.         else
  49.         {
  50.         name = n;
  51.         return true;
  52.         }
  53.     }
  54.     public bool SetNumber(string n) => int.TryParse(n, out nomer);
  55.         public bool SetTime1(string n) => DateTime.TryParse(n, out time1);
  56.         public bool SetTime2(string n) => DateTime.TryParse(n, out time2);
  57.         public bool SetCost(string n) => Decimal.TryParse(n, out cost);
  58.    
  59.     // Функции для возврата значений полей.
  60.     public string GetName() => name;
  61.         public int GetNumber() => nomer;
  62.         public DateTime GetTime1() => time1;
  63.         public DateTime GetTime2() => time2;
  64.     public Decimal GetCost() => cost;
  65.    
  66.    
  67.     // Сравниваем билеты по номеру.
  68.     public static bool operator >(Bilet b1, Bilet b2) => b1.nomer > b2.nomer;
  69.     public static bool operator <(Bilet b1, Bilet b2) => b1.nomer < b2.nomer;
  70.     }
  71.    
  72.     public static void Main(string[] args)
  73.     {
  74.     const int n = 3;
  75.     string tformat = "[формат: число.месяц.год часы:минуты:секунды]";
  76.     string print_sep = "---------------------------------------------";
  77.    
  78.     Bilet[] bilet_arr = new Bilet[n]; // Массив из n экземпляров Bilet.
  79.    
  80.     for (int i = 0; i < n; i++)
  81.     {
  82.         bilet_arr[i] = new Bilet(); // Инициализируем i-ый экземпляр.
  83.         for (;;)
  84.         {
  85.         Console.Write("Введите имя: ");
  86.         if (bilet_arr[i].SetName(Console.ReadLine()))
  87.             break;
  88.         else
  89.             Console.WriteLine("Длина имени не должна превышать 30 символов. Попробуйте еще раз.");
  90.         }
  91.         for (;;)
  92.         {
  93.         Console.Write("Введите номер билета: ");
  94.         if (bilet_arr[i].SetNumber(Console.ReadLine()))
  95.             break;
  96.         else
  97.             Console.WriteLine("Номер билета введен некорректно. Попробуйте еще раз.");
  98.         }
  99.         for (;;)
  100.         {
  101.         Console.Write("Введите время вылета {0}: ", tformat);
  102.         if (bilet_arr[i].SetTime1(Console.ReadLine()))
  103.             break;
  104.         else
  105.             Console.WriteLine("Время вылета введено некорректно. Попробуйте еще раз.");
  106.         }
  107.         for (;;)
  108.         {
  109.         Console.Write("Введите время прилёта {0}: ", tformat);
  110.         if (bilet_arr[i].SetTime2(Console.ReadLine()))
  111.             break;
  112.         else
  113.             Console.WriteLine("Время прилёта введено некорректно. Попробуйте еще раз.");
  114.         }
  115.         for (;;)
  116.         {
  117.         Console.Write("Введите цену билета: ");
  118.         if (bilet_arr[i].SetCost(Console.ReadLine()))
  119.             break;
  120.         else
  121.             Console.WriteLine("Цена билета введена некорретно. Попробуйте еще раз.");
  122.         }
  123.         Console.WriteLine();
  124.     }
  125.    
  126.     // Сортируем массив.
  127.     for (int i = 0; i < n - 1; i++)
  128.     {
  129.         for (int j = i + 1; j < n; j++)
  130.         {
  131.         if (bilet_arr[i] > bilet_arr[j])
  132.         {
  133.             Bilet t = bilet_arr[i];
  134.             bilet_arr[i] = bilet_arr[j];
  135.             bilet_arr[j] = t;
  136.         }
  137.         }
  138.     }
  139.    
  140.     Console.WriteLine("Для просмотра содержания билетов нажмите любую клавишу.");
  141.     Console.ReadKey();
  142.    
  143.     for (int i = 0; i < n; i++)
  144.     {
  145.         Console.WriteLine(print_sep);
  146.         Console.WriteLine("Имя: {0}", bilet_arr[i].GetName());
  147.         Console.WriteLine("Номер: {0}", bilet_arr[i].GetNumber());
  148.         Console.WriteLine("Время вылета: {0}", bilet_arr[i].GetTime1());
  149.         Console.WriteLine("Время прилета: {0}", bilet_arr[i].GetTime2());
  150.         Console.WriteLine("Цена билета: {0}", bilet_arr[i].GetCost());
  151.         Console.WriteLine("Предполагаемое время в пути: {0}", bilet_arr[i].GetDuration());
  152.     }
  153.    
  154.     Console.ReadKey();
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement