Ejemplo n.º 1
0
        public void CalculateChange()
        {
            // combine cash in hand and cash received
            CashRegister newCashInHand = CashRegister.Combine(_cashInHands, _cashReceived);

            _cashChange = new CashRegister(CashDenomination.ALL_DONOMINATIONS);
            int change = _cashReceived.Amount - _amountDue;

            // deduct from max to min denominations as needed
            newCashInHand.Sort(ListSortDirection.Descending);
            foreach (CashSet fromSet in newCashInHand.CashSets)
            {
                if (change > fromSet.Denomination.Amount)
                {
                    int qty = (int)(change / fromSet.Denomination.Amount);
                    if (qty > fromSet.Quantity)
                    {
                        qty = fromSet.Quantity;
                    }

                    fromSet.Quantity = fromSet.Quantity - qty;

                    CashSet toSet = _cashChange.CashSets.First(s => s.Denomination.Amount == fromSet.Denomination.Amount);
                    toSet.Quantity = toSet.Quantity + qty;

                    change -= fromSet.Denomination.Amount * qty;
                }

                if (change <= 0)
                {
                    break;
                }
            }

            // if still remained, deduct from the min to max denomination to make it zero
            if (change > 0)
            {
                newCashInHand.Sort(ListSortDirection.Ascending);
                foreach (CashSet fromSet in newCashInHand.CashSets)
                {
                    int qty = (int)(change / fromSet.Denomination.Amount) + 1;
                    if (qty > fromSet.Quantity)
                    {
                        qty = fromSet.Quantity;
                    }

                    fromSet.Quantity = fromSet.Quantity - qty;

                    CashSet toSet = _cashChange.CashSets.First(s => s.Denomination.Amount == fromSet.Denomination.Amount);
                    toSet.Quantity = toSet.Quantity + qty;

                    change -= fromSet.Denomination.Amount * qty;

                    if (change <= 0)
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void SwitchState(MachineState newState)
        {
            ClearInput();
            State = newState;

            switch (State)
            {
            case MachineState.SelectProduct:
                ClearProductSelected();
                CashReceived = new CashRegister(CashDenomination.ALL_DONOMINATIONS);
                CashChange   = new CashRegister(CashDenomination.ALL_DONOMINATIONS);
                _messages.Add("Please select a product:");
                break;

            case MachineState.EnterQuantity:
                AddMessage("Please enter quantity:");
                break;

            case MachineState.SelectPaymentMethod:
                AddMessage("Please select a payment method (1-Cash 2-Credit Card):");
                break;

            case MachineState.CollectCash:
                AddMessage(string.Format("Please put cash in the cash collector for the amount Due: {0}", FormatAmount(Transaction.AmountDue)));
                break;

            case MachineState.EnterCreditCardNumber:
                AddMessage("Please enter credit card number:");
                break;

            case MachineState.ConfirmTransaction:
                AddMessage("Are you sure you want to make the purchase?");
                break;
            }
        }
Ejemplo n.º 3
0
        public Machine(IList <Product> products, CashRegister cashInHand)
        {
            Products   = products;
            CashInHand = cashInHand;

            CashReceived = new CashRegister(CashDenomination.ALL_DONOMINATIONS);
            CashChange   = new CashRegister(CashDenomination.ALL_DONOMINATIONS);

            _messages = new List <string>();
            SwitchState(MachineState.SelectProduct);
        }
Ejemplo n.º 4
0
        public static CashRegister Combine(CashRegister register1, CashRegister register2)
        {
            CashRegister newCashRegister = new CashRegister(CashDenomination.ALL_DONOMINATIONS);

            foreach (CashDenomination d in CashDenomination.ALL_DONOMINATIONS)
            {
                CashSet set = newCashRegister.CashSets.FirstOrDefault(s => s.Denomination.Amount == d.Amount);

                CashSet s1 = register1.CashSets.FirstOrDefault(s => s.Denomination.Amount == d.Amount);
                if (s1 != null)
                {
                    set.Quantity = set.Quantity + s1.Quantity;
                }

                CashSet s2 = register2.CashSets.FirstOrDefault(s => s.Denomination.Amount == d.Amount);
                if (s2 != null)
                {
                    set.Quantity = set.Quantity + s2.Quantity;
                }
            }

            return(newCashRegister);
        }
Ejemplo n.º 5
0
 public CashPayment(CashRegister cashInHand, CashRegister cashReceived)
 {
     _cashInHands  = cashInHand;
     _cashReceived = cashReceived;
 }