Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Program {
- public class Bilet {
- private string name;
- private int nomer;
- private DateTime time1;
- private DateTime time2;
- private Decimal cost;
- // Конструктор по умолчанию
- public Bilet()
- {
- name = "Неизвестно";
- nomer = 0;
- time1 = new DateTime(1980, 1, 1, 0, 0, 0);
- time2 = new DateTime(1980, 1, 1, 0, 0, 0);
- cost = 0;
- }
- // Конструктор с параметрами.
- public Bilet(string na, int no, DateTime t1, DateTime t2, Decimal c)
- {
- name = na;
- nomer = no;
- time1 = t1;
- time2 = t2;
- cost = c;
- }
- // Конструктор копирования.
- public Bilet(Bilet prev_bilet)
- {
- name = prev_bilet.name;
- nomer = prev_bilet.nomer;
- time1 = prev_bilet.time1;
- time2 = prev_bilet.time2;
- cost = prev_bilet.cost;
- }
- public TimeSpan GetDuration() => time2.Subtract(time1);
- // Функции для присвоения значений полям.
- public bool SetName(string n)
- {
- if (n.Length > 30)
- {
- return false;
- }
- else
- {
- name = n;
- return true;
- }
- }
- public bool SetNumber(string n) => int.TryParse(n, out nomer);
- public bool SetTime1(string n) => DateTime.TryParse(n, out time1);
- public bool SetTime2(string n) => DateTime.TryParse(n, out time2);
- public bool SetCost(string n) => Decimal.TryParse(n, out cost);
- // Функции для возврата значений полей.
- public string GetName() => name;
- public int GetNumber() => nomer;
- public DateTime GetTime1() => time1;
- public DateTime GetTime2() => time2;
- public Decimal GetCost() => cost;
- // Сравниваем билеты по номеру.
- public static bool operator >(Bilet b1, Bilet b2) => b1.nomer > b2.nomer;
- public static bool operator <(Bilet b1, Bilet b2) => b1.nomer < b2.nomer;
- }
- public static void Main(string[] args)
- {
- const int n = 3;
- string tformat = "[формат: число.месяц.год часы:минуты:секунды]";
- string print_sep = "---------------------------------------------";
- Bilet[] bilet_arr = new Bilet[n]; // Массив из n экземпляров Bilet.
- for (int i = 0; i < n; i++)
- {
- bilet_arr[i] = new Bilet(); // Инициализируем i-ый экземпляр.
- for (;;)
- {
- Console.Write("Введите имя: ");
- if (bilet_arr[i].SetName(Console.ReadLine()))
- break;
- else
- Console.WriteLine("Длина имени не должна превышать 30 символов. Попробуйте еще раз.");
- }
- for (;;)
- {
- Console.Write("Введите номер билета: ");
- if (bilet_arr[i].SetNumber(Console.ReadLine()))
- break;
- else
- Console.WriteLine("Номер билета введен некорректно. Попробуйте еще раз.");
- }
- for (;;)
- {
- Console.Write("Введите время вылета {0}: ", tformat);
- if (bilet_arr[i].SetTime1(Console.ReadLine()))
- break;
- else
- Console.WriteLine("Время вылета введено некорректно. Попробуйте еще раз.");
- }
- for (;;)
- {
- Console.Write("Введите время прилёта {0}: ", tformat);
- if (bilet_arr[i].SetTime2(Console.ReadLine()))
- break;
- else
- Console.WriteLine("Время прилёта введено некорректно. Попробуйте еще раз.");
- }
- for (;;)
- {
- Console.Write("Введите цену билета: ");
- if (bilet_arr[i].SetCost(Console.ReadLine()))
- break;
- else
- Console.WriteLine("Цена билета введена некорретно. Попробуйте еще раз.");
- }
- Console.WriteLine();
- }
- // Сортируем массив.
- for (int i = 0; i < n - 1; i++)
- {
- for (int j = i + 1; j < n; j++)
- {
- if (bilet_arr[i] > bilet_arr[j])
- {
- Bilet t = bilet_arr[i];
- bilet_arr[i] = bilet_arr[j];
- bilet_arr[j] = t;
- }
- }
- }
- Console.WriteLine("Для просмотра содержания билетов нажмите любую клавишу.");
- Console.ReadKey();
- for (int i = 0; i < n; i++)
- {
- Console.WriteLine(print_sep);
- Console.WriteLine("Имя: {0}", bilet_arr[i].GetName());
- Console.WriteLine("Номер: {0}", bilet_arr[i].GetNumber());
- Console.WriteLine("Время вылета: {0}", bilet_arr[i].GetTime1());
- Console.WriteLine("Время прилета: {0}", bilet_arr[i].GetTime2());
- Console.WriteLine("Цена билета: {0}", bilet_arr[i].GetCost());
- Console.WriteLine("Предполагаемое время в пути: {0}", bilet_arr[i].GetDuration());
- }
- Console.ReadKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement