Esempio n. 1
0
        public void FullfillCommitmentAtomic(int amountToCommit)
        {
            if (amountToCommit > amount)
            {
                throw new InventoryException("Cannot move more than there is in commitment!" +
                                             "Currently committing: " + amountToCommit + " but only committed to: " + amount);
            }
            committer.RemoveCommitment(this);
            committedTo.RemoveCommitment(this);
            committer.RemoveFromCurrentAmount(itemType, amountToCommit);
            committedTo.AddToCurrentAmount(itemType, amountToCommit);

            if (amountToCommit < amount)
            {
                var newCommittment = new Commitment(amount - amountToCommit, itemType, committedTo, committer);
                newCommittment.CommitToComitterAndComitee();
            }
        }
Esempio n. 2
0
        public static Commitment MergeCommitmentsOfThisType(ItemType itemType, IInventory committer,
                                                            IInventory committedTo)
        {
            List <Commitment> commitments = committer.GetAllOutgoingCommits().FindAll(commitment =>
                                                                                      commitment.itemType == itemType &&
                                                                                      commitment.committer == committer &&
                                                                                      commitment.committedTo == committedTo);

            if (commitments.Count == 0)
            {
                return(null);
            }

            var merged = new Commitment(commitments.Sum(a => a.amount), itemType, committedTo, committer);

            committer.RemoveCommitments(commitments);
            committedTo.RemoveCommitments(commitments);
            merged.CommitToComitterAndComitee();
            return(merged);
        }
Esempio n. 3
0
        public Commitment CommitToAnInventory(IInventory targetInventory, ItemType itemType, int amount)
        {
            if (!_currentAmount.ContainsKey(itemType))
            {
                throw new CommitmentException("Cannot commit because inventory does not contain this type");
            }
            if (GetCurrentUncommittedAmount(itemType) < amount)
            {
                throw new CommitmentException("Cannot commit because inventory does not contain enough of this type");
            }
            if (!targetInventory.CanAcceptAmount(itemType, amount))
            {
                throw new CommitmentException("Cannot commit because target inventory does not have enough space");
            }

            Commitment newCommitment = new Commitment(amount, itemType, targetInventory, this);

            newCommitment.CommitToComitterAndComitee();
            var merged = Commitment.MergeCommitmentsOfThisType(itemType, this, targetInventory);

            return(merged);
        }