/// <summary> /// Removes the Sensor /// </summary> /// <param name="args"></param> /// <returns></returns> private IEnumerable <char> RemoveSensor(params string[] args) { if (args.Length < 3 && args.Length != 2) { throw new CommandInputException("Invalid command usage, should be : sensor remove <model> [side] [sensor] [sensor]"); } ; CarModel model; Direction side; CreateSensor sensorFactory; try { model = CarModel.GetModel(args[1]); } catch (Exception e) { throw new CommandInputException("Invalid command: Model doesn't exist."); } listPotentialRemoved = model.Sensors; var countSensorsModel = model.Sensors.Count; if (args.Length == 2) { printListPotentialRemoved(); App.Console.SendCommand += removeSensors; return(null); } else { try { side = StringConverters.StringToDirection(args[2]); } catch (Exception e) { throw new CommandInputException("Invalid command: Side is wrong used."); } try { sensorFactory = StringConverters.GetSensor(args[3]); } catch (Exception e) { throw new CommandInputException("Invalid command: Could not create sensor."); } var count = model.Sensors .FindAll(sensor => sensor.Create.Equals(sensorFactory) && side == sensor.Direction) .Count; model.Sensors.RemoveAll(sensor => sensor.Create.Equals(sensorFactory) && side == sensor.Direction); return($"Removed {count} {args[3]} sensors."); } }
/// <summary> /// Add's a sensor /// </summary> /// <param name="args"></param> /// <returns></returns> private IEnumerable <char> AddSensor(params string[] args) { if (args.Length < 4) { throw new CommandInputException("Invalid command usage, should be : sensor add <model> <side> <sensor> [range]"); } ; if (args[0] != "add") { throw new CommandInputException("Invalid command useage, should be : sensor add <model> <side> <sensor> [range]"); } ; CarModel model; Direction side; CreateSensor sensorFactory; try { model = CarModel.GetModel(args[1]); } catch (Exception) { throw new CommandInputException("Invalid command: Model doesn't exist."); } try { side = StringConverters.StringToDirection(args[2]); } catch (Exception) { throw new CommandInputException("Invalid command: Side is wrong used."); } try { sensorFactory = StringConverters.GetSensor(args[3]); } catch (Exception) { throw new CommandInputException("Invalid command: Could not create sensor."); } var range = 0; if (args.Length == 5) { try { range = int.Parse(args[4]); } catch (Exception) { throw new CommandInputException("Invalid command: Could not parse range."); } } var sensor = new SensorPrototype { Direction = side, Range = range, Create = sensorFactory }; model.Sensors.Add(sensor); CitySystem.City.Instance.Cars .FindAll(car => car.Model == model) .ForEach(car => car.Sensors.Add(sensor.Create(car, sensor.Direction, sensor.Range))); return($"Added sensor {args[3]} to {model.Name}"); }