alexandrheathen

Lab4

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