/// <summary> /// Initialize Default array of flights wit ha fake data /// </summary> /// <param name="sizeOfFlights">Amout of fake flights</param> private void InitializeFlightsByDefault(int sizeOfFlights) { if (sizeOfFlights > MaxFlights) { sizeOfFlights = MaxFlights; } for (int i = 0; i < sizeOfFlights; i++) { AirlineObjects.Add(new Flight(MaxFlights) { Type = (i > 2) ? FlightType.Arrival : FlightType.Departure, Airline = "test", ArrivalCity = "Kharkiv", DepartureCity = "Kiev", Gate = "G" + 1 * i, Arrival = DateTime.Now, Departure = DateTime.Now, Number = "72" + 2 * i, Status = (i < 9) ? (FlightStatus)i : (FlightStatus)8, Terminal = "F" + i }); } }
/// <summary> /// Adds the airline object to array /// </summary> /// <param name="FieldsValues">Airline object's fields and their values</param> /// <param name="airlineObject">New Airline object for a collection</param> /// <param name="airlineObjects">New Airline object's list for a main object if it's a manager</param> /// <returns>Positive if added</returns> public virtual bool Add(string[] FieldsValues) { var type = GetElementType(); var airlineObject = Activator.CreateInstance(type) as AirlineObject; var FieldsValuesUpdate = GetAirlineObjectInfo(FieldsValues); var index = AirlineObjects.Count(arg => arg != null); if (index > -1) { if (Size != 0 && Size <= AirlineObjects.Count) { OnDisplayInfoChanged(new AirlineObjectEventArgs { HasError = true, ConsoleColor = ConsoleColor.Red, DisplayInfo = "Can't add an object, max size reached" }); return(false); } try { foreach (string[] fieldsValues in FieldsValuesUpdate) { if (fieldsValues[fieldNameIndex] == "Count") { var airlineManager = airlineObject as AirlineManager; if (airlineManager != null) { int airlineObjectsCount = 0; if (int.TryParse(fieldsValues[fieldValueIndex], out airlineObjectsCount)) { airlineManager.Size = airlineObjectsCount; } } continue; } if (Properties.Contains(fieldsValues[fieldNameIndex])) { var originalAirlineObject = airlineObject; var isAlreadyProcessed = false; secondChance: try { if (airlineObject[fieldsValues[fieldNameIndex]].GetType().IsEnum) { try { var enumType = airlineObject[fieldsValues[fieldNameIndex]].GetType(); var enumString = char.ToUpper(fieldsValues[fieldValueIndex][0]) + fieldsValues[fieldValueIndex].Substring(1).ToLower(); airlineObject[fieldsValues[fieldNameIndex]] = Enum.Parse(enumType, enumString); } catch { } } else { switch (Type.GetTypeCode(airlineObject[fieldsValues[fieldNameIndex]].GetType())) { case TypeCode.Int32: try { airlineObject[fieldsValues[fieldNameIndex]] = Convert.ToInt32(fieldsValues[fieldValueIndex]); } catch { } break; case TypeCode.Decimal: try { airlineObject[fieldsValues[fieldNameIndex]] = Convert.ToDecimal(fieldsValues[fieldValueIndex]); } catch { } break; case TypeCode.String: try { airlineObject[fieldsValues[fieldNameIndex]] = fieldsValues[fieldValueIndex]; } catch { } break; case TypeCode.DateTime: try { airlineObject[fieldsValues[fieldNameIndex]] = Convert.ToDateTime(fieldsValues[fieldValueIndex]); } catch { } break; } } } catch { if (!isAlreadyProcessed) { isAlreadyProcessed = true; var passanger = airlineObject as Passenger; if (passanger != null) { airlineObject = passanger.Ticket; goto secondChance; } } } airlineObject = originalAirlineObject; } } if (airlineObject.IsValid()) { AirlineObjects.Add(airlineObject); return(true); } } catch (Exception ex) { } } return(false); }