/// <summary> /// Removes an airline object from list /// </summary> /// <param name="airlineObject">Airline object you need to remove</param> /// <returns>Positive if removed</returns> public virtual bool Delete(AirlineObject airlineObject) { try { AirlineObjects = AirlineObjects.Where(arg => arg != null && arg.ID != airlineObject.ID).ToList(); } catch (Exception ex) { return(false); } return(true); }
/// <summary> /// Removes an airline object from list /// </summary> /// <param name="airlineObject">Airline object you need to remove</param> /// <returns>Positive if removed</returns> public virtual bool Delete(AirlineObject airlineObject) { try { var prevCount = AirlineObjects.Count(); var tmpAirlineObjects = AirlineObjects.Where(arg => arg != null && arg.ID != airlineObject.ID).ToArray(); Array.Resize(ref tmpAirlineObjects, prevCount); AirlineObjects = tmpAirlineObjects; } catch (Exception ex) { return(false); } return(true); }
/// <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 }); } }
protected override bool CanBeResized() { return((AirlineObjects.Count(arg => arg != null) - 1) < _CountOfPassangers); }
public override void ProcessOptions(string[] values) { var id = 0; switch (_selectedOption) { case AirlineOptions.ShowPassangers: CurrentAirlineObjects = AirlineObjects.Where(arg => (arg as Passenger) != null).ToArray(); Options = new AirlineOptions[s_general.Length + s_edit.Length]; s_edit.CopyTo(Options, 0); s_general.CopyTo(Options, s_edit.Length); break; case AirlineOptions.SearchPassangers: if (values.Length > 0) { Find(values); } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Empty values provided", ConsoleColor = ConsoleColor.Red, HasError = true }); } Options = new AirlineOptions[s_general.Length + s_edit.Length]; s_edit.CopyTo(Options, 0); s_general.CopyTo(Options, s_edit.Length); break; case AirlineOptions.AddAPassanger: if (values.Length > 0) { if (Add(values, new Passenger(), null)) { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "The Passanger has been added successfully", ConsoleColor = ConsoleColor.Green }); } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "The Passanger wasn't added", ConsoleColor = ConsoleColor.Red, HasError = true }); } } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Empty values provided", ConsoleColor = ConsoleColor.Red, HasError = true }); } CurrentAirlineObjects = AirlineObjects.ToArray(); Options = new AirlineOptions[s_general.Length + s_edit.Length]; s_edit.CopyTo(Options, 0); s_general.CopyTo(Options, s_edit.Length); break; case AirlineOptions.EditThePassanger: if (values.Length > 0) { if (Edit(values)) { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "The Passanger has been edited successfully", ConsoleColor = ConsoleColor.Green }); } } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Empty values provided", ConsoleColor = ConsoleColor.Red, HasError = true }); } CurrentAirlineObjects = AirlineObjects.ToArray(); Options = new AirlineOptions[s_general.Length + s_edit.Length]; s_edit.CopyTo(Options, 0); s_general.CopyTo(Options, s_edit.Length); break; case AirlineOptions.DeleteThePassanger: if (values.Length > 0) { var optionsArray = values[0].Split(' '); if ((optionsArray.Length == 3) && (int.TryParse(optionsArray[2], out id)) && (id > 0) && (id < CurrentAirlineObjects.Length + 1)) { if (Delete(CurrentAirlineObjects[id - 1])) { CurrentAirlineObjects = AirlineObjects.ToArray(); OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Removed Successfully" }); } } } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Empty values provided", ConsoleColor = ConsoleColor.Red, HasError = true }); } Options = new AirlineOptions[s_general.Length + s_edit.Length]; s_edit.CopyTo(Options, 0); s_general.CopyTo(Options, s_edit.Length); break; case AirlineOptions.ClearTheConsole: OnDisplayInfoChanged(new AirlineObjectEventArgs { ClearConsole = true }); CurrentAirlineObjects = null; Options = s_general; break; case AirlineOptions.Info: OnDisplayInfoChanged(new AirlineObjectEventArgs { ConsoleColor = ConsoleColor.Yellow, DisplayInfo = "You are inside flight where you can work with passangers and it's info, \n" + "In case if you need to go back to Airline manager, just chose 'Exit or level up' menu item" }); CurrentAirlineObjects = null; Options = s_general; break; case AirlineOptions.ExitOrLevelUp: CurrentAirlineManager = null; CurrentAirlineObjects = null; Options = s_general; Reset(); break; } }
/// <summary> /// Updates the airline object /// </summary> /// <param name="objectId">Airline object for update</param> /// <param name="updateValues">Fields and their values to update</param> /// <returns>Positive if updated</returns> private bool Update(int objectId, string[][] updateValues) { var airlineObject = CurrentAirlineObjects[objectId]; if (airlineObject == null) { return(false); } try { foreach (string[] updateValue in updateValues) { if (Properties.Contains(updateValue[fieldNameIndex])) { var originalAirlineObject = airlineObject; var isAlreadyProcessed = false; secondChance: try { if (airlineObject[updateValue[fieldNameIndex]].GetType().IsEnum) { try { var enumType = airlineObject[updateValue[fieldNameIndex]].GetType(); var enumString = char.ToUpper(updateValue[fieldValueIndex][0]) + updateValue[fieldValueIndex].Substring(1).ToLower(); airlineObject[updateValue[fieldNameIndex]] = Enum.Parse(enumType, enumString); } catch { } } else { switch (Type.GetTypeCode(airlineObject[updateValue[fieldNameIndex]].GetType())) { case TypeCode.Int32: try { airlineObject[updateValue[fieldNameIndex]] = Convert.ToInt32(updateValue[fieldValueIndex]); } catch { } break; case TypeCode.String: try { airlineObject[updateValue[fieldNameIndex]] = updateValue[fieldValueIndex]; } catch { } break; case TypeCode.DateTime: try { airlineObject[updateValue[fieldNameIndex]] = Convert.ToDateTime(updateValue[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()) { objectId = AirlineObjects.FindIndex((arg) => arg.ID == airlineObject.ID); if (objectId > -1) { AirlineObjects[objectId] = airlineObject; return(true); } else { throw new Exception("Can't find the airline object using the indeex provided"); } } } catch (Exception ex) { OnDisplayInfoChanged(new AirlineObjectEventArgs { HasError = true, ConsoleColor = ConsoleColor.Red, DisplayInfo = $"Error during update, message: {ex.Message}" }); } return(false); }
/// <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); }
public override void ProcessOptions(string[] values) { var id = 0; switch (_selectedOption) { case AirlineOptions.ShowAllFlights: CurrentAirlineObjects = AirlineObjects.Where(arg => (arg as Flight) != null).ToList(); if (CurrentAirlineObjects.Count > 0) { Options.Clear(); Options.AddRange(s_edit); Options.AddRange(s_general.Where(arg => Options.IndexOf(arg) < 0)); } break; case AirlineOptions.ShowArrivals: CurrentAirlineObjects = AirlineObjects.Where(arg => (arg as Flight) != null && ((Flight)arg).Type == FlightType.Arrival).ToList(); if (CurrentAirlineObjects.Count > 0) { Options.Clear(); Options.AddRange(s_edit); Options.AddRange(s_general.Where(arg => Options.IndexOf(arg) < 0)); } break; case AirlineOptions.ShowDepartues: CurrentAirlineObjects = AirlineObjects.Where(arg => (arg as Flight) != null && ((Flight)arg).Type == FlightType.Departure).ToList(); if (CurrentAirlineObjects.Count > 0) { Options.Clear(); Options.AddRange(s_edit); Options.AddRange(s_general.Where(arg => Options.IndexOf(arg) < 0)); } break; case AirlineOptions.SearchFlights: if (values.Length > 0) { Find(values); } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Empty values provided", ConsoleColor = ConsoleColor.Red, HasError = true }); } Options.Clear(); Options.AddRange(s_edit); Options.AddRange(s_general.Where(arg => Options.IndexOf(arg) < 0)); break; case AirlineOptions.AddAFlight: if (values.Length > 0) { if (Add(values)) { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "The Flight has been added successfully", ConsoleColor = ConsoleColor.Green }); } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "The Flight wasn't added", ConsoleColor = ConsoleColor.Red, HasError = true }); } } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Empty values provided", ConsoleColor = ConsoleColor.Red, HasError = true }); } CurrentAirlineObjects = AirlineObjects; Options.Clear(); Options.AddRange(s_edit); Options.AddRange(s_general.Where(arg => Options.IndexOf(arg) < 0)); break; case AirlineOptions.EditTheFlight: if (values.Length > 0) { if (Edit(values)) { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "The Flight has been edited successfully", ConsoleColor = ConsoleColor.Green }); } } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Empty values provided", ConsoleColor = ConsoleColor.Red, HasError = true }); } CurrentAirlineObjects = AirlineObjects; Options.Clear(); Options.AddRange(s_edit); Options.AddRange(s_general.Where(arg => Options.IndexOf(arg) < 0)); break; case AirlineOptions.EditPassangersOfTheFlight: if (values.Length > 0) { var optionsArray = values[0].Split(' '); if ((optionsArray.Length == 3) && (int.TryParse(optionsArray[2], out id)) && (id > 0) && (id < CurrentAirlineObjects.Count + 1)) { var flight = CurrentAirlineObjects[id - 1] as Flight; if (flight != null) { IndexOfCurrentAirlineManager = id - 1; CurrentAirlineManager = flight; } } } CurrentAirlineObjects = null; Options = new List <AirlineOptions>(); Options.AddRange(s_general); break; case AirlineOptions.DeleteTheFlight: if (values.Length > 0) { var optionsArray = values[0].Split(' '); if ((optionsArray.Length == 3) && (int.TryParse(optionsArray[2], out id)) && (id > 0) && (id < CurrentAirlineObjects.Count + 1)) { if (Delete(CurrentAirlineObjects[id - 1])) { CurrentAirlineObjects = AirlineObjects; OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Removed Successfully" }); } } } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Empty values provided", ConsoleColor = ConsoleColor.Red, HasError = true }); } Options.Clear(); Options.AddRange(s_edit); Options.AddRange(s_general.Where(arg => Options.IndexOf(arg) < 0)); break; case AirlineOptions.ClearTheConsole: OnDisplayInfoChanged(new AirlineObjectEventArgs { ClearConsole = true }); CurrentAirlineObjects = null; Options = new List <AirlineOptions>(); Options.AddRange(s_general); break; case AirlineOptions.Info: OnDisplayInfoChanged(new AirlineObjectEventArgs { ConsoleColor = ConsoleColor.Yellow, DisplayInfo = "You are inside Airport manager where you can work with flights and it's info, " + "if you would like to receive an information about passanger and edit it, please go to 'Edit passangers of the flight'.\n" + "In case if you need to exit from Application, just chose 'Exit or level up' menu item" }); CurrentAirlineObjects = null; Options = new List <AirlineOptions>(); Options.AddRange(s_general); break; case AirlineOptions.LoadFromFile: if (values.Length > 0) { try { if (OpenFromFile(values[0])) { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Loaded successfully", ConsoleColor = ConsoleColor.Green }); CurrentAirlineObjects = AirlineObjects; } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Can't load from file", ConsoleColor = ConsoleColor.Red, HasError = true }); } } catch (Exception ex) { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = $"Couldn't load data from file because of: {ex.Message}", ConsoleColor = ConsoleColor.Red, HasError = true }); } } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Empty values provided", ConsoleColor = ConsoleColor.Red, HasError = true }); } break; case AirlineOptions.SaveToFile: if (values.Length > 0) { try { if (SaveToFile(values[0])) { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Saved successfully", ConsoleColor = ConsoleColor.Green }); } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Can't save to file", ConsoleColor = ConsoleColor.Red, HasError = true }); } } catch (Exception ex) { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = $"Couldn't save data to file because of: {ex.Message}", ConsoleColor = ConsoleColor.Red, HasError = true }); } } else { OnDisplayInfoChanged(new AirlineObjectEventArgs { DisplayInfo = "Empty values provided", ConsoleColor = ConsoleColor.Red, HasError = true }); } break; case AirlineOptions.ExitOrLevelUp: CurrentAirlineManager = null; CurrentAirlineObjects = null; Options = new List <AirlineOptions>(); Options.AddRange(s_general); Reset(); break; } }