alexandrheathen

Untitled

May 14th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace StructConsole
  8. {
  9.     class Program
  10.     {
  11.         public struct Date
  12.         {
  13.             public int Year;
  14.             public int Month;
  15.             public int Day;
  16.             public int Hours;
  17.             public int Minutes;
  18.             public Date(int day, int month, int year, int hours, int minutes)
  19.             {
  20.                 Day = day;
  21.                 Month = month;
  22.                 Year = year;
  23.                 Hours = hours;
  24.                 Minutes = minutes;
  25.             }
  26.         }
  27.  
  28.         public struct Airplane
  29.         {
  30.             public string StartCity;
  31.             public Date StartDate;
  32.             public string FinishCity;
  33.             public Date FinishDate;
  34.             public Airplane(string startCity, Date startDate, string finishCity, Date finishDate)
  35.             {
  36.                 StartCity = startCity;
  37.                 StartDate = startDate;
  38.                 FinishCity = finishCity;
  39.                 FinishDate = finishDate;
  40.             }
  41.  
  42.             public double GetTotalTime()
  43.             {
  44.                 DateTime departDate = new DateTime(StartDate.Year, StartDate.Month, StartDate.Day, StartDate.Hours, StartDate.Minutes, 0);
  45.                 DateTime arrivalDate = new DateTime(FinishDate.Year, FinishDate.Month, FinishDate.Day, FinishDate.Hours, FinishDate.Minutes, 0);
  46.                 return (arrivalDate - departDate).TotalMinutes;
  47.             }
  48.  
  49.             public bool IsArrivingToday()
  50.             {
  51.                 if (StartDate.Day == FinishDate.Day)
  52.                     return true;
  53.                 else
  54.                     return false;
  55.             }
  56.         }
  57.  
  58.         public static Date ReadDate()
  59.         {
  60.             Date air;
  61.             bool flag = true;
  62.             do
  63.             {
  64.                 Console.ForegroundColor = ConsoleColor.Green;
  65.                 Console.Write("Enter day: ");
  66.                 if (!int.TryParse(Console.ReadLine(), out air.Day) || air.Day < 0 || air.Day > 2025)
  67.                 {
  68.                     flag = false;
  69.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  70.                     Console.WriteLine("Error404. Try to enter again!");
  71.                 }
  72.                 else
  73.                     flag = true;
  74.             } while (!flag);
  75.  
  76.             do
  77.             {
  78.                 Console.ForegroundColor = ConsoleColor.Green;
  79.                 Console.Write("Enter month: ");
  80.                 if (!int.TryParse(Console.ReadLine(), out air.Month) || air.Month < 0 || air.Month > 12)
  81.                 {
  82.                     flag = false;
  83.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  84.                     Console.WriteLine("Error404. Try to enter again!");
  85.                 }
  86.                 else
  87.                     flag = true;
  88.             } while (!flag);
  89.  
  90.             do
  91.             {
  92.                 Console.ForegroundColor = ConsoleColor.Green;
  93.                 Console.Write("Enter year: ");
  94.                 if (!int.TryParse(Console.ReadLine(), out air.Year) || air.Year < 2018 || air.Year > 2025)
  95.                 {
  96.                     flag = false;
  97.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  98.                     Console.WriteLine("Error404. Try to enter again!");
  99.                 }
  100.                 else
  101.                     flag = true;
  102.             } while (!flag);
  103.  
  104.             do
  105.             {
  106.                 Console.ForegroundColor = ConsoleColor.Green;
  107.                 Console.Write("Enter hours: ");
  108.                 if (!int.TryParse(Console.ReadLine(), out air.Hours) || air.Hours < 0 || air.Hours > 24)
  109.                 {
  110.                     flag = false;
  111.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  112.                     Console.WriteLine("Error404. Try to enter again!");
  113.                 }
  114.                 else
  115.                     flag = true;
  116.             } while (!flag);
  117.  
  118.             do
  119.             {
  120.                 Console.ForegroundColor = ConsoleColor.Green;
  121.                 Console.Write("Enter minutes: ");
  122.                 if (!int.TryParse(Console.ReadLine(), out air.Minutes) || air.Minutes < 0 || air.Minutes > 60)
  123.                 {
  124.                     flag = false;
  125.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  126.                     Console.WriteLine("Error404. Try to enter again!");
  127.                 }
  128.                 else
  129.                     flag = true;
  130.             } while (!flag);
  131.  
  132.             return air;
  133.         }
  134.  
  135.         public static Airplane[] ReadAllFlights()
  136.         {
  137.             Console.Clear();
  138.             int len;
  139.             bool flag = true;
  140.             do
  141.             {
  142.                 Console.ForegroundColor = ConsoleColor.Yellow;
  143.                 Console.Write("Enter amount of flights: ");
  144.                 if (!int.TryParse(Console.ReadLine(), out len))
  145.                 {
  146.                     flag = false;
  147.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  148.                     Console.WriteLine("Error404. Try to enter again!");
  149.                 }
  150.                 else
  151.                     flag = true;
  152.             } while (!flag);
  153.             Console.Clear();
  154.             Airplane[] flight = new Airplane[len];
  155.             for (int i = 0; i < flight.Length; i++)
  156.             {
  157.                 Console.Clear();
  158.                 Console.ForegroundColor = ConsoleColor.Red;
  159.                 Console.WriteLine("~ ~ ~ ~ ~ Flight #{0} ~ ~ ~ ~ ~", i);
  160.                 flight[i] = ReadAirplane();
  161.             }
  162.             return flight;
  163.         }
  164.  
  165.         public static Airplane ReadAirplane()
  166.         {
  167.             Airplane flight;
  168.             Console.ForegroundColor = ConsoleColor.DarkMagenta;
  169.             Console.WriteLine("~ ~ ~ DEPART ~ ~ ~");
  170.             Console.ForegroundColor = ConsoleColor.Green;
  171.             Console.Write("Enter depart city: ");
  172.             flight.StartCity = Console.ReadLine();
  173.             Console.Clear();
  174.             Console.ForegroundColor = ConsoleColor.Cyan;
  175.             Console.WriteLine("- - - Enter depart date - - -");
  176.             flight.StartDate = ReadDate();
  177.             Console.ForegroundColor = ConsoleColor.Green;
  178.             Console.Clear();
  179.             Console.ForegroundColor = ConsoleColor.DarkGray;
  180.             Console.WriteLine("~ ~ ~ ARRIVAL ~ ~ ~");
  181.             Console.ForegroundColor = ConsoleColor.Cyan;
  182.             Console.Write("Enter arrival city: ");
  183.             flight.FinishCity = Console.ReadLine();
  184.             Console.Clear();
  185.             Console.WriteLine("- - - Enter arrival date - - -");
  186.             flight.FinishDate = ReadDate();
  187.             return flight;
  188.         }
  189.  
  190.         public static void PrintDate(Date date)
  191.         {
  192.             Console.ForegroundColor = ConsoleColor.White;
  193.             Console.WriteLine("Date: {0}.{1}.{2} - {3}:{4}", date.Day, date.Month, date.Year, date.Hours, date.Minutes);
  194.         }
  195.  
  196.         public static void PrintAirplane(Airplane airplane)
  197.         {
  198.             Console.ForegroundColor = ConsoleColor.DarkCyan;
  199.             Console.Write("Depart city: ");
  200.             Console.WriteLine(airplane.StartCity);
  201.             Console.WriteLine("Depart time: ");
  202.             PrintDate(airplane.StartDate);
  203.             Console.ForegroundColor = ConsoleColor.DarkRed;
  204.             Console.Write("Arrival city: ");
  205.             Console.WriteLine(airplane.FinishCity);
  206.             Console.WriteLine("Arrival time: ");
  207.             PrintDate(airplane.FinishDate);
  208.         }
  209.  
  210.         public static void PrintAirplanes(Airplane[] airplanes)
  211.         {
  212.             Console.Clear();
  213.             for (int i = 0; i < airplanes.Length; i++)
  214.             {
  215.                 Console.ForegroundColor = ConsoleColor.Red;
  216.                 Console.WriteLine("~ ~ ~ ~ Flight #{0} ~ ~ ~ ~", i);
  217.                 PrintAirplane(airplanes[i]);
  218.             }
  219.             Console.ForegroundColor = ConsoleColor.White;
  220.             Console.WriteLine("Press enter to continue...");
  221.             Console.ReadKey();
  222.         }
  223.  
  224.         public static void GetAirplaneInfo(Airplane[] airplanes, out double shortFlight, out double longFlight)
  225.         {
  226.             Console.Clear();
  227.             longFlight = double.MinValue;
  228.             shortFlight = double.MaxValue;
  229.             for (int i = 0; i < airplanes.Length; i++)
  230.             {
  231.                 if (airplanes[i].GetTotalTime() < shortFlight)
  232.                     shortFlight = airplanes[i].GetTotalTime();
  233.                 if (airplanes[i].GetTotalTime() > longFlight)
  234.                     longFlight = airplanes[i].GetTotalTime();
  235.             }
  236.             Console.ForegroundColor = ConsoleColor.Cyan;
  237.             Console.WriteLine("The shortest duration of flight: {0}\nThe longest duration of flight: {1}", shortFlight, longFlight);
  238.             Console.ForegroundColor = ConsoleColor.White;
  239.             Console.WriteLine("Press enter to continue...");
  240.             Console.ReadKey();
  241.         }
  242.  
  243.         public static int SortAirplnesByTotalTime(Airplane a, Airplane b)
  244.         {
  245.             double avgA = a.GetTotalTime(), avgB = b.GetTotalTime();
  246.             if (avgA > avgB)
  247.                 return 1;
  248.             if (avgA < avgB)
  249.                 return -1;
  250.             return 0;
  251.         }
  252.  
  253.         public static int SortAirplnesByDate(Airplane a, Airplane b)
  254.         {
  255.             DateTime fstDateA = new DateTime(a.StartDate.Year, a.StartDate.Month, a.StartDate.Day, a.StartDate.Hours, a.StartDate.Minutes, 0);
  256.             DateTime sndDateB = new DateTime(b.StartDate.Year, b.StartDate.Month, b.StartDate.Day, b.StartDate.Hours, b.StartDate.Minutes, 0);
  257.             return DateTime.Compare(fstDateA, sndDateB);
  258.         }
  259.  
  260.         static void Menu()
  261.         {
  262.             Console.Clear();
  263.             Console.ForegroundColor = ConsoleColor.Red;
  264.             Console.WriteLine("\t~ ~ ~ ~ MENU ~ ~ ~ ~");
  265.             Console.ForegroundColor = ConsoleColor.Cyan;
  266.             Console.WriteLine("1. Add flight;\n2. Show all flights;\n3. Show the longest and shortest flight;\n4. Sort by DATE ARRIVAL;\n5. Sort by DURATION of FLIGHT;\n");
  267.             Console.ForegroundColor = ConsoleColor.DarkRed;
  268.             Console.WriteLine("\tTo QUIT - Press 0");
  269.         }
  270.  
  271.  
  272.         static void Main(string[] args)
  273.         {
  274.             Console.Title = "Лабораторна робота №4";
  275.             ConsoleKeyInfo hotKey;
  276.             int elLen = 0;
  277.             double shortFlight, longFlight;
  278.             Airplane[] allAirplanes = new Airplane[0];
  279.             do
  280.             {
  281.                 Menu();
  282.                 hotKey = Console.ReadKey();
  283.                 switch (hotKey.Key)
  284.                 {
  285.                     case ConsoleKey.D1:
  286.                         allAirplanes = ReadAllFlights();
  287.                         elLen = allAirplanes.Length;
  288.                         break;
  289.                     case ConsoleKey.D2:
  290.                         PrintAirplanes(allAirplanes);
  291.                         break;
  292.                     case ConsoleKey.D3:
  293.                         GetAirplaneInfo(allAirplanes, out shortFlight, out longFlight);
  294.                         break;
  295.                     case ConsoleKey.D4:
  296.                         Array.Sort(allAirplanes, SortAirplnesByDate);
  297.                         Array.Reverse(allAirplanes);
  298.                         break;
  299.                     case ConsoleKey.D5:
  300.                         Array.Sort(allAirplanes, SortAirplnesByTotalTime);
  301.                         break;
  302.                     case ConsoleKey.D0:
  303.                         Console.Clear();
  304.                         Console.ForegroundColor = ConsoleColor.Green;
  305.                         Console.WriteLine("See you later!");
  306.                         Environment.Exit(0);
  307.                         break;
  308.                     default:
  309.                         Console.WriteLine("\nPress another button!");
  310.                         break;
  311.                 }
  312.             } while (true);
  313.         }
  314.     }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment