public void Add(ICashSet cashSet) { foreach (KeyValuePair <ICash, int> denomination in cashSet.CashStack) { Add(denomination.Key, denomination.Value); } }
public void Remove(ICashSet set) { foreach (KeyValuePair <ICash, int> denomination in set.CashStack) { Remove(denomination.Key, denomination.Value); } }
public void Remove(ICashSet changeSet) { foreach (KeyValuePair <ICash, int> pair in changeSet.CashStack) { ChangeContent(pair.Key, -pair.Value); } }
#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")); } }
public void CanCreateCashSetFromSet() { ICashSet initialSet = Mocks.MockCashSets.StandardSet; CashSet madeSet = new CashSet(initialSet); Assert.AreEqual(initialSet.CashStack, madeSet.CashStack); }
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)); }
/// <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); } }
/// <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)); } }
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(); } }
public TillDrawer(ICashSet initialSet) { Contents = initialSet; }
public TillDrawer() { Contents = new CashSet(); }
/// <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; }
public void Remove(decimal value) { ICashSet setToRemove = Controllers.CashController.SmallestSetForValue(value); Remove(setToRemove); }
public void Add(decimal value) { ICashSet setToAdd = Controllers.CashController.SmallestSetForValue(value); Add(setToAdd); }
public TillDrawer(ICashSet initialContent) { this.DrawerContent = initialContent; }