Example #1
0
        public TripAirfield(Trip trip, Airfield airfield, AirfieldDirectionEnum airfieldDirection)
        {
            this.Trip              = trip;
            this.Airfield          = airfield;
            this.AirfieldDirection = airfieldDirection;

            this.TripId     = this.Trip.TripId;
            this.AirfieldId = this.Airfield.AirfieldId;
        }
Example #2
0
        public static Trip LoadTripFromLegacy(string tripLine, int tripId, List <Pilot> pilots, List <Airfield> airfields)
        {
            var sections       = tripLine.Split(new string[] { "\t" }, StringSplitOptions.RemoveEmptyEntries);
            var timeSplitChars = new char[] { '.', ',' };

            if (sections.Length < 14)
            {
                throw new Exception("Insufficient items on row");
            }

            DateTime date;
            string   planeType, regNumber, remarks;
            var      pilotLinkers = new List <Tuple <Pilot, PilotPositionEnum> >();
            var      airfieldLinkers = new List <Tuple <Airfield, AirfieldDirectionEnum> >();
            DateTime departureTime, arrivalTime;
            TimeSpan dayHours, nightHours;
            double   btrt;
            bool     landed;

            if (!DateTime.TryParse(sections[0], out date))
            {
                throw new Exception($"{sections[0]} is not recognised as a date");
            }

            planeType = sections[1];
            regNumber = sections[2];

            int lastPilotIndex = 3;

            while (!IsAirfield(sections[lastPilotIndex]))
            {
                var pilotName = sections[lastPilotIndex];

                var foundPilot = pilots.SingleOrDefault(p => p.Name == pilotName);

                if (foundPilot == null)
                {
                    foundPilot = new Pilot()
                    {
                        Name = pilotName
                    };
                    pilots.Add(foundPilot);
                }

                pilotLinkers.Add(
                    new Tuple <Pilot, PilotPositionEnum>(
                        foundPilot,
                        lastPilotIndex == 3 ? PilotPositionEnum.Captain : PilotPositionEnum.FirstOfficer
                        )
                    );

                lastPilotIndex++;
            }

            var departureAirfieldName = sections[lastPilotIndex];

            var departureAirfield = airfields.SingleOrDefault(a => a.AirfieldName == departureAirfieldName);

            if (departureAirfield == null)
            {
                departureAirfield = new Airfield()
                {
                    AirfieldName = departureAirfieldName
                };
                airfields.Add(departureAirfield);
            }

            airfieldLinkers.Add(
                new Tuple <Airfield, AirfieldDirectionEnum>(
                    departureAirfield,
                    AirfieldDirectionEnum.Departure)
                );

            var arrivalAirfieldName = sections[lastPilotIndex + 1];

            var arrivalAirfield = airfields.SingleOrDefault(a => a.AirfieldName == arrivalAirfieldName);

            if (arrivalAirfield == null)
            {
                arrivalAirfield = new Airfield()
                {
                    AirfieldName = arrivalAirfieldName
                };
                airfields.Add(arrivalAirfield);
            }

            airfieldLinkers.Add(
                new Tuple <Airfield, AirfieldDirectionEnum>(
                    arrivalAirfield,
                    AirfieldDirectionEnum.Arrival)
                );

            var departureformatString = sections[lastPilotIndex + 2].Contains('.')
                ? "HH.mm"
                : "HH,mm";

            if (DateTime.TryParseExact(
                    sections[lastPilotIndex + 2],
                    departureformatString,
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out DateTime departureDateTime))
            {
                departureTime = departureDateTime;
            }
            else
            {
                throw new Exception($"{sections[lastPilotIndex + 2]} is not recognised as a time");
            }

            var arrivalformatString = sections[lastPilotIndex + 3].Contains('.')
                ? "HH.mm"
                : "HH,mm";

            if (DateTime.TryParseExact(
                    sections[lastPilotIndex + 3],
                    arrivalformatString,
                    CultureInfo.InvariantCulture,
                    DateTimeStyles.None,
                    out DateTime arrivalDateTime))
            {
                arrivalTime = arrivalDateTime;
            }
            else
            {
                throw new Exception($"{sections[lastPilotIndex + 3]} is not recognised as a time");
            }

            try
            {
                var dayHoursParts = sections[lastPilotIndex + 4].Split(timeSplitChars);

                var minutesString = dayHoursParts[1].Length == 1
                    ? dayHoursParts[1] + "0"
                    : dayHoursParts[1];

                dayHours = new TimeSpan(
                    int.Parse(dayHoursParts[0]),
                    int.Parse(minutesString),
                    0);
            }
            catch (Exception)
            {
                throw new Exception($"{sections[lastPilotIndex + 4]} is not recognised as a time");
            }

            try
            {
                var nightHoursParts = sections[lastPilotIndex + 5].Split(timeSplitChars);

                var minutesString = nightHoursParts[1].Length == 1
                    ? nightHoursParts[1] + "0"
                    : nightHoursParts[1];

                nightHours = new TimeSpan(
                    int.Parse(nightHoursParts[0]),
                    int.Parse(minutesString),
                    0);
            }
            catch (Exception)
            {
                throw new Exception($"{sections[lastPilotIndex + 5]} is not recognised as a time");
            }

            if (!double.TryParse(sections[lastPilotIndex + 6], out btrt))
            {
                throw new Exception($"{sections[lastPilotIndex + 6]} is not recognised as a double");
            }

            landed  = sections[lastPilotIndex + 7] == "y";
            remarks = sections[lastPilotIndex + 8];

            var newTrip = new Trip(
                tripId,
                date,
                planeType,
                regNumber,
                pilotLinkers,
                airfieldLinkers,
                departureTime,
                arrivalTime,
                dayHours,
                nightHours,
                btrt,
                landed,
                remarks
                );

            Debug.WriteLine(newTrip);

            return(newTrip);
        }