Exemple #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));
 }
Exemple #2
0
        private void swap(DebtContainer toSwap)
        {
            toSwap.Amount = Math.Abs(toSwap.Amount);
            Person temp = toSwap.OwedTo;

            toSwap.OwedTo   = toSwap.OwedFrom;
            toSwap.OwedFrom = temp;
        }
Exemple #3
0
 void cleanUp(DebtContainer existingDebt)
 {
     if (existingDebt.Amount == 0)
     {
         DebtList.Remove(existingDebt);
     }
     else if (existingDebt.Amount < 0)
     {
         swap(existingDebt);
     }
 }
Exemple #4
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();
                        }
                    }

                }
            }
        }
Exemple #5
0
 public void addDebt(DebtContainer node)
 {
     addDebt(node.Amount, node.OwedTo, node.OwedFrom);
 }
Exemple #6
0
 private void swap(DebtContainer toSwap)
 {
     toSwap.Amount = Math.Abs(toSwap.Amount);
     Person temp = toSwap.OwedTo;
     toSwap.OwedTo = toSwap.OwedFrom;
     toSwap.OwedFrom = temp;
 }
Exemple #7
0
 void cleanUp(DebtContainer existingDebt)
 {
     if (existingDebt.Amount == 0) DebtList.Remove(existingDebt);
     else if (existingDebt.Amount < 0) swap(existingDebt);
 }
Exemple #8
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));
 }
Exemple #9
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();
                        }
                    }
                }
            }
        }
Exemple #10
0
 public void addDebt(DebtContainer node)
 {
     addDebt(node.Amount, node.OwedTo, node.OwedFrom);
 }