Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace StructConsole
- {
- class Program
- {
- public struct Date
- {
- public int Year;
- public int Month;
- public int Day;
- public int Hours;
- public int Minutes;
- public Date(int day, int month, int year, int hours, int minutes)
- {
- Day = day;
- Month = month;
- Year = year;
- Hours = hours;
- Minutes = minutes;
- }
- }
- public struct Airplane
- {
- public string StartCity;
- public Date StartDate;
- public string FinishCity;
- public Date FinishDate;
- public Airplane(string startCity, Date startDate, string finishCity, Date finishDate)
- {
- StartCity = startCity;
- StartDate = startDate;
- FinishCity = finishCity;
- FinishDate = finishDate;
- }
- public double GetTotalTime()
- {
- DateTime departDate = new DateTime(StartDate.Year, StartDate.Month, StartDate.Day, StartDate.Hours, StartDate.Minutes, 0);
- DateTime arrivalDate = new DateTime(FinishDate.Year, FinishDate.Month, FinishDate.Day, FinishDate.Hours, FinishDate.Minutes, 0);
- return (arrivalDate - departDate).TotalMinutes;
- }
- public bool IsArrivingToday()
- {
- if (StartDate.Day == FinishDate.Day)
- return true;
- else
- return false;
- }
- }
- public static Date ReadDate()
- {
- Date air;
- bool flag = true;
- do
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Enter day: ");
- if (!int.TryParse(Console.ReadLine(), out air.Day) || air.Day < 0 || air.Day > 2025)
- {
- flag = false;
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("Error404. Try to enter again!");
- }
- else
- flag = true;
- } while (!flag);
- do
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Enter month: ");
- if (!int.TryParse(Console.ReadLine(), out air.Month) || air.Month < 0 || air.Month > 12)
- {
- flag = false;
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("Error404. Try to enter again!");
- }
- else
- flag = true;
- } while (!flag);
- do
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Enter year: ");
- if (!int.TryParse(Console.ReadLine(), out air.Year) || air.Year < 2018 || air.Year > 2025)
- {
- flag = false;
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("Error404. Try to enter again!");
- }
- else
- flag = true;
- } while (!flag);
- do
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Enter hours: ");
- if (!int.TryParse(Console.ReadLine(), out air.Hours) || air.Hours < 0 || air.Hours > 24)
- {
- flag = false;
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("Error404. Try to enter again!");
- }
- else
- flag = true;
- } while (!flag);
- do
- {
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Enter minutes: ");
- if (!int.TryParse(Console.ReadLine(), out air.Minutes) || air.Minutes < 0 || air.Minutes > 60)
- {
- flag = false;
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("Error404. Try to enter again!");
- }
- else
- flag = true;
- } while (!flag);
- return air;
- }
- public static Airplane[] ReadAllFlights()
- {
- Console.Clear();
- int len;
- bool flag = true;
- do
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write("Enter amount of flights: ");
- if (!int.TryParse(Console.ReadLine(), out len))
- {
- flag = false;
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("Error404. Try to enter again!");
- }
- else
- flag = true;
- } while (!flag);
- Console.Clear();
- Airplane[] flight = new Airplane[len];
- for (int i = 0; i < flight.Length; i++)
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("~ ~ ~ ~ ~ Flight #{0} ~ ~ ~ ~ ~", i);
- flight[i] = ReadAirplane();
- }
- return flight;
- }
- public static Airplane ReadAirplane()
- {
- Airplane flight;
- Console.ForegroundColor = ConsoleColor.DarkMagenta;
- Console.WriteLine("~ ~ ~ DEPART ~ ~ ~");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Enter depart city: ");
- flight.StartCity = Console.ReadLine();
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine("- - - Enter depart date - - -");
- flight.StartDate = ReadDate();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.DarkGray;
- Console.WriteLine("~ ~ ~ ARRIVAL ~ ~ ~");
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.Write("Enter arrival city: ");
- flight.FinishCity = Console.ReadLine();
- Console.Clear();
- Console.WriteLine("- - - Enter arrival date - - -");
- flight.FinishDate = ReadDate();
- return flight;
- }
- public static void PrintDate(Date date)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("Date: {0}.{1}.{2} - {3}:{4}", date.Day, date.Month, date.Year, date.Hours, date.Minutes);
- }
- public static void PrintAirplane(Airplane airplane)
- {
- Console.ForegroundColor = ConsoleColor.DarkCyan;
- Console.Write("Depart city: ");
- Console.WriteLine(airplane.StartCity);
- Console.WriteLine("Depart time: ");
- PrintDate(airplane.StartDate);
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.Write("Arrival city: ");
- Console.WriteLine(airplane.FinishCity);
- Console.WriteLine("Arrival time: ");
- PrintDate(airplane.FinishDate);
- }
- public static void PrintAirplanes(Airplane[] airplanes)
- {
- Console.Clear();
- for (int i = 0; i < airplanes.Length; i++)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("~ ~ ~ ~ Flight #{0} ~ ~ ~ ~", i);
- PrintAirplane(airplanes[i]);
- }
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("Press enter to continue...");
- Console.ReadKey();
- }
- public static void GetAirplaneInfo(Airplane[] airplanes, out double shortFlight, out double longFlight)
- {
- Console.Clear();
- longFlight = double.MinValue;
- shortFlight = double.MaxValue;
- for (int i = 0; i < airplanes.Length; i++)
- {
- if (airplanes[i].GetTotalTime() < shortFlight)
- shortFlight = airplanes[i].GetTotalTime();
- if (airplanes[i].GetTotalTime() > longFlight)
- longFlight = airplanes[i].GetTotalTime();
- }
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine("The shortest duration of flight: {0}\nThe longest duration of flight: {1}", shortFlight, longFlight);
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("Press enter to continue...");
- Console.ReadKey();
- }
- public static int SortAirplnesByTotalTime(Airplane a, Airplane b)
- {
- double avgA = a.GetTotalTime(), avgB = b.GetTotalTime();
- if (avgA > avgB)
- return 1;
- if (avgA < avgB)
- return -1;
- return 0;
- }
- public static int SortAirplnesByDate(Airplane a, Airplane b)
- {
- DateTime fstDateA = new DateTime(a.StartDate.Year, a.StartDate.Month, a.StartDate.Day, a.StartDate.Hours, a.StartDate.Minutes, 0);
- DateTime sndDateB = new DateTime(b.StartDate.Year, b.StartDate.Month, b.StartDate.Day, b.StartDate.Hours, b.StartDate.Minutes, 0);
- return DateTime.Compare(fstDateA, sndDateB);
- }
- static void Menu()
- {
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("\t~ ~ ~ ~ MENU ~ ~ ~ ~");
- Console.ForegroundColor = ConsoleColor.Cyan;
- 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");
- Console.ForegroundColor = ConsoleColor.DarkRed;
- Console.WriteLine("\tTo QUIT - Press 0");
- }
- static void Main(string[] args)
- {
- Console.Title = "Лабораторна робота №4";
- ConsoleKeyInfo hotKey;
- int elLen = 0;
- double shortFlight, longFlight;
- Airplane[] allAirplanes = new Airplane[0];
- do
- {
- Menu();
- hotKey = Console.ReadKey();
- switch (hotKey.Key)
- {
- case ConsoleKey.D1:
- allAirplanes = ReadAllFlights();
- elLen = allAirplanes.Length;
- break;
- case ConsoleKey.D2:
- PrintAirplanes(allAirplanes);
- break;
- case ConsoleKey.D3:
- GetAirplaneInfo(allAirplanes, out shortFlight, out longFlight);
- break;
- case ConsoleKey.D4:
- Array.Sort(allAirplanes, SortAirplnesByDate);
- Array.Reverse(allAirplanes);
- break;
- case ConsoleKey.D5:
- Array.Sort(allAirplanes, SortAirplnesByTotalTime);
- break;
- case ConsoleKey.D0:
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("See you later!");
- Environment.Exit(0);
- break;
- default:
- Console.WriteLine("\nPress another button!");
- break;
- }
- } while (true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment