public string Add(Unit unit)
        {
            // check unique names
            if (unitNames.Contains(unit.Name))
            {
                return string.Format("FAIL: {0} already exists!", unit.Name);
            }

            this.unitNames.Add(unit.Name);

            // add by type
            if (!unitsByType.ContainsKey(unit.Type))
            {
                unitsByType[unit.Type] = new SortedSet<Unit>();
            }

            this.unitsByType[unit.Type].Add(unit);

            // add by attack
            this.allAttacks.Add(unit.Attack);

            if (!this.unitsByAttack.ContainsKey(unit.Attack))
            {
                this.unitsByAttack[unit.Attack] = new SortedSet<Unit>();
            }

            this.unitsByAttack[unit.Attack].Add(unit);
            
            return string.Format("SUCCESS: {0} added!", unit.Name);
        }
Ejemplo n.º 2
0
        public bool AddUnit(Unit unit)
        {
            if (this.units.Contains(unit))
            {
                return false;
            }

            if (!(this.byType.ContainsKey(unit.Type)))
            {
                this.byType[unit.Type] = new SortedSet<Unit>();
            }

            this.units.Add(unit);
            this.sortedUnits.Add(unit);
            this.byType[unit.Type].Add(unit);

            return true;
        }
        public bool Add(Unit unit)
        {
            if (this.unitsByName.ContainsKey(unit.Name))
            {
                return false;
            }

            this.unitsByName[unit.Name] = unit;

            if (!this.unitsByType.ContainsKey(unit.Type))
            {
                this.unitsByType[unit.Type] = new List<Unit>();
            }

            this.unitsByType[unit.Type].Add(unit);

            this.unitsByAttack.Add(unit);

            return true;
        }
Ejemplo n.º 4
0
        public bool RemoveUnit(Unit unitName)
        {
            Unit unitToDelete = new Unit();

            foreach (var item in units)
            {
                if (unitName.Name == item.Name)
                {
                    unitToDelete = item;
                    break;
                }
            }

            if (unitToDelete.Name != null)
            {
                units.Remove(unitToDelete);
                sortedUnits.Remove(unitToDelete);
                byType[unitToDelete.Type].Remove(unitToDelete);
                return true;
            }
            return false;
        }
Ejemplo n.º 5
0
        public static void Main(string[] args)
        {
            var unitsWorkshop = new UnitWorkshop();
            while (true)
            {
                string input = Console.ReadLine();
                var command = Command.ParseCommand(input);
                switch (command.Type)
                {
                    case CommandType.Add:
                        var unit = Unit.ParseUnit(command.Params);
                        var addResult = unitsWorkshop.AddUnit(unit);
                        string format;
                        if (addResult)
                        {
                            format = UnitAddedSuccessFormat;
                        }
                        else
                        {
                            format = UnitAddedErrorFormat;
                        }
                        Console.WriteLine(format, unit.Name);
                        break;
                    case CommandType.Remove:
                        var unitName = new Unit(command.Params);
                        var removeResult = unitsWorkshop.RemoveUnit(unitName);
                        if (removeResult)
                        {
                            format = UnitRemovedSuccesFormat;
                        }
                        else
                        {
                            format = UnitRemovedErrorFormat;
                        }
                        Console.WriteLine(format, unitName.Name);

                        break;
                     case CommandType.Power:
                        var powerUnits = unitsWorkshop.PowerUnits(command.Params);
                        if (powerUnits == null)
                        {
                            Console.WriteLine(UnitsFindErrorFormat, command.Params);
                        }
                        else
                        {
                            Console.WriteLine(UnitsFindSuccesFormat, string.Join(", ", powerUnits));
                        }
                        break;
                      case CommandType.Find:
                          var foundUnits = unitsWorkshop.UnitsByType(command.Params);
                          if (foundUnits == null)
                          {
                              Console.WriteLine(UnitsFindErrorFormat, command.Params);
                          }
                          else
                          {
                              Console.WriteLine(UnitsFindSuccesFormat, string.Join(", ", foundUnits));
                          }
                          break;
                      case CommandType.End:
                          return;
                }
            }
        }