コード例 #1
0
 public void Add(ICashSet cashSet)
 {
     foreach (KeyValuePair <ICash, int> denomination in cashSet.CashStack)
     {
         Add(denomination.Key, denomination.Value);
     }
 }
コード例 #2
0
 public void Remove(ICashSet set)
 {
     foreach (KeyValuePair <ICash, int> denomination in set.CashStack)
     {
         Remove(denomination.Key, denomination.Value);
     }
 }
コード例 #3
0
 public void Remove(ICashSet changeSet)
 {
     foreach (KeyValuePair <ICash, int> pair in changeSet.CashStack)
     {
         ChangeContent(pair.Key, -pair.Value);
     }
 }
コード例 #4
0
#pragma warning disable CS0246 // The type or namespace name 'Success' could not be found (are you missing a using directive or an assembly reference?)
        /// <summary>
        /// Handles the payment
        /// </summary>
        /// <param name="moneyGiven"></param>
        /// <returns></returns>
        private Success HandlePayment(decimal moneyGiven)
#pragma warning restore CS0246 // The type or namespace name 'Success' could not be found (are you missing a using directive or an assembly reference?)
        {
            decimal valueToReturn = moneyGiven - Cart.GetTransactionValue();

            if (tillDrawer.ContainsEnough(valueToReturn))
            {
                ICashSet givenSet = CashController.SmallestSetForValue(moneyGiven);

                ICashSet availableCash = new CashSet(tillDrawer.DrawerContent);
                availableCash.Add(givenSet);

                ICashSet returnSet = PaymentController.Payout(valueToReturn, availableCash);

                if (returnSet != null)
                {
                    consoleDisplay.Print(string.Format("\nReturn {0}, distributed as: {1}",
                                                       valueToReturn.ToString(),
                                                       returnSet.ToString()));

                    tillDrawer.Add(givenSet);
                    tillDrawer.Remove(returnSet);

                    return(new Success(true));
                }
                else
                {
                    return(new Success(false, "\nUnable to allocate sufficient cash; request a different payment method"));
                }
            }
            else
            {
                return(new Success(false, "\nNot enough money in the drawer; request a different payment method"));
            }
        }
コード例 #5
0
        public void CanCreateCashSetFromSet()
        {
            ICashSet initialSet = Mocks.MockCashSets.StandardSet;

            CashSet madeSet = new CashSet(initialSet);

            Assert.AreEqual(initialSet.CashStack, madeSet.CashStack);
        }
コード例 #6
0
        private void RaisePrintReturnCash(ICashSet setToReturn)
        {
            string changeNotification = string.Format("Return {0}, distributed as {1}",
                                                      setToReturn.GetSum().ToString(),
                                                      setToReturn.AsString());

            ChangeFound?.Invoke(this, new ConsolePrintEventArgs(changeNotification));
        }
コード例 #7
0
        /// <summary>
        /// Builds an ICashSet containing the smallest amount of items totalling at the defined value from a resticted list
        /// </summary>
        /// <param name="value">The desired value of the ICashSet</param>
        /// <param name="availableItems">The set of ICashItems that can be used to build the resurnset</param>
        public static ICashSet SmallestSetForValue(decimal value, ICashSet availableItems)
        {
            if (value <= 0)
            {
                return(null);
            }

            ICashSet returnSet       = new CashSet();
            decimal  valueToAllocate = value;
            decimal  setValue        = availableItems.GetSum();

            if (setValue == value)
            {
                return(availableItems);
            }
            else if (setValue > value)
            {
                foreach (KeyValuePair <ICash, int> denomination in availableItems.CashStack)
                {
                    int amountNeeded = (int)(valueToAllocate / denomination.Key.UnitValue);

                    if (amountNeeded == 0 || denomination.Value == 0)
                    {
                        //we either don't need this denomination, or we don't have any to use => skip adding it to the set
                        continue;
                    }
                    else if (denomination.Value >= amountNeeded)
                    {
                        //There are sufficient units of this value available => add the desired amount to the set
                        returnSet.Add(denomination.Key, amountNeeded);
                        valueToAllocate -= denomination.Key.UnitValue * amountNeeded;
                    }
                    else
                    {
                        //There are insufficient units available (but more than none) => use them all
                        returnSet.Add(denomination.Key, denomination.Value);
                        valueToAllocate -= denomination.Key.UnitValue * denomination.Value;
                    }

                    if (valueToAllocate == 0)
                    {
                        //All value has been accounted for: return the set
                        return(returnSet);
                    }
                }
                return(null);
            }
            else
            {
                return(null);
            }
        }
コード例 #8
0
        /// <summary>
        /// Creates a Payout cashset from a set of cashitems
        /// </summary>
        /// <param name="valueToPay">The desired value of the returned cashset</param>
        /// <param name="drawerContent">The cashset from which to return a subset for payout</param>
        /// <returns></returns>
        public ICashSet Payout(decimal valueToPay, ICashSet drawerContent)
        {
            ICashSet payOut = CashController.SmallestSetForValue(valueToPay, drawerContent);

            if (payOut != null)
            {
                //Return the payout set
                return(payOut);
            }
            else
            {
                //Round the payout set to the closest 5 cents and try again
                decimal roundedValue = Math.Round(valueToPay * 20) / 20;

                return(CashController.SmallestSetForValue(roundedValue, drawerContent));
            }
        }
コード例 #9
0
        public void DetermineChange(object s, ProvideChangeEventArgs pce)
        {
            ICashSet transactionSet = new CashSet(TillDrawer.Contents);
            CashSet  givenSet       = (CashSet)CashController.SmallestSetForValue(pce.CashGiven);

            transactionSet.Add(givenSet);

            decimal valueToPayout = pce.CashGiven - pce.TransactionValue;

            ICashSet setToReturn = Payout(valueToPayout, transactionSet);

            if (setToReturn != null)
            {
                RaisePrintReturnCash(setToReturn);
                RaisePaymentPossible(givenSet, (CashSet)setToReturn);
            }
            else
            {
                RaiseNoChangeFound();
            }
        }
コード例 #10
0
 public TillDrawer(ICashSet initialSet)
 {
     Contents = initialSet;
 }
コード例 #11
0
 public TillDrawer()
 {
     Contents = new CashSet();
 }
コード例 #12
0
 /// <summary>
 /// Creates a copy of an existing ICashSet
 /// </summary>
 /// <param name="initialSet">The ICashSet to copy</param>
 public CashSet(ICashSet initialSet)
 {
     this.CashStack = initialSet.CashStack;
 }
コード例 #13
0
        public void Remove(decimal value)
        {
            ICashSet setToRemove = Controllers.CashController.SmallestSetForValue(value);

            Remove(setToRemove);
        }
コード例 #14
0
        public void Add(decimal value)
        {
            ICashSet setToAdd = Controllers.CashController.SmallestSetForValue(value);

            Add(setToAdd);
        }
コード例 #15
0
 public TillDrawer(ICashSet initialContent)
 {
     this.DrawerContent = initialContent;
 }