Ejemplo n.º 1
0
        public void AddElement(SushiBindModel model)
        {
            int maxID = 0;

            for (int i = 0; i < source.Sushis.Count; ++i)
            {
                if (source.Sushis[i].ID > maxID)
                {
                    maxID = source.Sushis[i].ID;
                }
                if (source.Sushis[i].SushiName == model.SushiName)
                {
                    throw new Exception("Уже есть изделие с таким названием");
                }
            }
            source.Sushis.Add(new Sushi
            {
                ID        = maxID + 1,
                SushiName = model.SushiName,
                Price     = model.Price
            });
            int maxPCID = 0;

            for (int i = 0; i < source.ElementRequirements.Count; ++i)
            {
                if (source.ElementRequirements[i].ID > maxPCID)
                {
                    maxPCID = source.ElementRequirements[i].ID;
                }
            }
            for (int i = 0; i < model.ElementRequirements.Count; ++i)
            {
                for (int j = 1; j < model.ElementRequirements.Count; ++j)
                {
                    if (model.ElementRequirements[i].ElementID ==
                        model.ElementRequirements[j].ElementID)
                    {
                        model.ElementRequirements[i].Count +=
                            model.ElementRequirements[j].Count;
                        model.ElementRequirements.RemoveAt(j--);
                    }
                }
            }
            for (int i = 0; i < model.ElementRequirements.Count; ++i)
            {
                source.ElementRequirements.Add(new ElementRequirement
                {
                    ID        = ++maxPCID,
                    SushiID   = maxID + 1,
                    ElementID = model.ElementRequirements[i].ElementID,
                    Count     = model.ElementRequirements[i].Count
                });
            }
        }
Ejemplo n.º 2
0
        public void UpdElement(SushiBindModel model)
        {
            int index = -1;

            for (int i = 0; i < source.Sushis.Count; ++i)
            {
                if (source.Sushis[i].ID == model.ID)
                {
                    index = i;
                }
                if (source.Sushis[i].SushiName == model.SushiName &&
                    source.Sushis[i].ID != model.ID)
                {
                    throw new Exception("Уже есть изделие с таким названием");
                }
            }
            if (index == -1)
            {
                throw new Exception("Элемент не найден");
            }
            source.Sushis[index].SushiName = model.SushiName;
            source.Sushis[index].Price     = model.Price;
            int maxPCID = 0;

            for (int i = 0; i < source.ElementRequirements.Count; ++i)
            {
                if (source.ElementRequirements[i].ID > maxPCID)
                {
                    maxPCID = source.ElementRequirements[i].ID;
                }
            }
            for (int i = 0; i < source.ElementRequirements.Count; ++i)
            {
                if (source.ElementRequirements[i].SushiID == model.ID)
                {
                    bool flag = true;
                    for (int j = 0; j < model.ElementRequirements.Count; ++j)
                    {
                        if (source.ElementRequirements[i].ID == model.ElementRequirements[j].ID)
                        {
                            source.ElementRequirements[i].Count = model.ElementRequirements[j].Count;
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                    {
                        source.ElementRequirements.RemoveAt(i--);
                    }
                }
            }
            for (int i = 0; i < model.ElementRequirements.Count; ++i)
            {
                if (model.ElementRequirements[i].ID == 0)
                {
                    for (int j = 0; j < source.ElementRequirements.Count; ++j)
                    {
                        if (source.ElementRequirements[j].SushiID == model.ID &&
                            source.ElementRequirements[j].ElementID == model.ElementRequirements[i].ElementID)
                        {
                            source.ElementRequirements[j].Count += model.ElementRequirements[i].Count;
                            model.ElementRequirements[i].ID      = source.ElementRequirements[j].ID;
                            break;
                        }
                    }
                    if (model.ElementRequirements[i].ID == 0)
                    {
                        source.ElementRequirements.Add(new ElementRequirement
                        {
                            ID        = ++maxPCID,
                            SushiID   = model.ID,
                            ElementID = model.ElementRequirements[i].ElementID,
                            Count     = model.ElementRequirements[i].Count
                        });
                    }
                }
            }
        }