Пример #1
0
 private void AddToDebtList(DebtContainer nodeOne, DebtContainer nodeTwo, DebtContainer newNodeOne, DebtContainer newNodeTwo)
 {
     DebtList.Remove(nodeOne);
     DebtList.Remove(nodeTwo);
     DebtList.Add(new DebtContainer(newNodeOne.OwedTo, newNodeOne.OwedFrom, newNodeOne.Amount));
     DebtList.Add(new DebtContainer(newNodeTwo.OwedTo, newNodeTwo.OwedFrom, newNodeTwo.Amount));
 }
Пример #2
0
 void cleanUp(DebtContainer existingDebt)
 {
     if (existingDebt.Amount == 0)
     {
         DebtList.Remove(existingDebt);
     }
     else if (existingDebt.Amount < 0)
     {
         swap(existingDebt);
     }
 }
Пример #3
0
        public void Combine()
        {
            for (int i = 0; i < DebtList.Count; i++)
            {
                for (int j = 0; j < DebtList.Count; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    var nodeOne    = DebtList.ElementAt(i);
                    var nodeTwo    = DebtList.ElementAt(j);
                    var newNodeOne = new DebtContainer();
                    var newNodeTwo = new DebtContainer();


                    //check to see if they're the same owedTo and owedFrom
                    if (nodeOne.OwedTo == nodeTwo.OwedTo && nodeOne.OwedFrom == nodeTwo.OwedFrom)
                    {
                        nodeOne.Amount += nodeTwo.Amount;
                        DebtList.Remove(nodeTwo);
                        continue;
                    }
                    else if (nodeOne.OwedTo == nodeTwo.OwedFrom && nodeOne.OwedFrom == nodeTwo.OwedTo)
                    {
                        if (nodeOne.Amount - nodeTwo.Amount > 0)
                        {
                            nodeOne.Amount -= nodeTwo.Amount;
                            DebtList.Remove(nodeTwo);
                        }
                        else if (nodeOne.Amount - nodeTwo.Amount < 0)
                        {
                            nodeTwo.Amount -= nodeOne.Amount;
                            DebtList.Remove(nodeOne);
                        }
                        else
                        {
                            DebtList.Remove(nodeOne);
                            DebtList.Remove(nodeTwo);
                        }

                        continue;
                    }


                    //check to see if they can be transitivly combined.
                    // (A -> B), (B -> C) ->> (A -> C)
                    if (nodeOne.OwedFrom == nodeTwo.OwedTo)
                    {
                        nodeOne = DebtList.ElementAt(j);
                        nodeTwo = DebtList.ElementAt(i);
                    }
                    if (nodeOne.OwedTo == nodeTwo.OwedFrom)
                    {
                        if (nodeOne.Amount > nodeTwo.Amount)
                        {
                            newNodeOne.OwedTo   = nodeTwo.OwedTo;
                            newNodeOne.OwedFrom = nodeOne.OwedFrom;
                            newNodeOne.Amount   = nodeTwo.Amount;

                            newNodeTwo.OwedFrom = nodeOne.OwedFrom;
                            newNodeTwo.OwedTo   = nodeOne.OwedTo;
                            newNodeTwo.Amount   = nodeOne.Amount - nodeTwo.Amount;

                            //if adding them will result in a combination, add them then call the recursive function
                            if (DebtList.Any(x => (x.OwedFrom == newNodeOne.OwedFrom && x.OwedTo == newNodeOne.OwedTo) ||
                                             (x.OwedFrom == newNodeOne.OwedTo && x.OwedTo == newNodeOne.OwedFrom)))
                            {
                                AddToDebtList(nodeOne, nodeTwo, newNodeOne, newNodeTwo);
                                Combine();
                            }
                        }
                        else if (nodeOne.Amount < nodeTwo.Amount)
                        {
                            newNodeOne.OwedFrom = nodeOne.OwedFrom;
                            newNodeOne.OwedTo   = nodeTwo.OwedTo;
                            newNodeOne.Amount   = nodeOne.Amount;


                            newNodeTwo.OwedFrom = nodeTwo.OwedFrom;
                            newNodeTwo.OwedTo   = nodeTwo.OwedTo;
                            newNodeTwo.Amount   = nodeTwo.Amount - nodeOne.Amount;

                            //if adding them will result in a combination, add them then call the recursive function
                            if (DebtList.Any(x => (x.OwedFrom == newNodeOne.OwedFrom && x.OwedTo == newNodeOne.OwedTo) ||
                                             (x.OwedFrom == newNodeOne.OwedTo && x.OwedTo == newNodeOne.OwedFrom)))
                            {
                                AddToDebtList(nodeOne, nodeTwo, newNodeOne, newNodeTwo);
                                Combine();
                            }
                        }

                        else
                        {
                            newNodeOne.OwedFrom = nodeOne.OwedFrom;
                            newNodeOne.OwedTo   = nodeTwo.OwedTo;
                            newNodeOne.Amount   = nodeOne.Amount;


                            DebtList.Remove(nodeOne);
                            DebtList.Remove(nodeTwo);
                            DebtList.Add(new DebtContainer(newNodeOne.OwedTo, newNodeOne.OwedFrom, newNodeOne.Amount));
                            Combine();
                        }
                    }
                }
            }
        }