예제 #1
0
        public void Construction()
        {
            Change fivePence = new Change(5, 1);
            Assert.AreEqual(fivePence.Denomination, 5);
            Assert.AreEqual(fivePence.Quantity, 1);

            // Try a "strange" denomination
            Change change = new Change(204, 2);
            Assert.AreEqual(change.Denomination, 204);
            Assert.AreEqual(change.Quantity, 2);
        }
예제 #2
0
        /// <summary>
        /// Adds change to the balance of the vending machine
        /// </summary>
        /// <param name="change">The change to add</param>
        public void Add(Change change)
        {
            if(change == null) throw new ArgumentNullException("change");

            // We order the change list from highest denomination to lowest.
            // This will simplify certain change algorithms (like greedy)
            // where we go from highest to lowest

            int index = m_Change.BinarySearch(change, HighToLowChangeComparer.Instance);
            if(index < 0)
            {
                // We've not seen this denomination before so insert it
                m_Change.Insert(~index, change);
            }
            else
            {
                // Just update the existing item
                m_Change[index] = m_Change[index].Add(change);
            }
        }
예제 #3
0
        /// <summary>
        /// Removes change from the balance of the vending machine
        /// </summary>
        /// <param name="change">The change to add</param>
        public void Remove(Change change)
        {
            if(change == null) throw new ArgumentNullException("change");

            int index = m_Change.FindIndex(c => c.Denomination == change.Denomination);

            if(index == -1)
            {
                // If we don't have the specified denomination then flag is as an error
                throw new ArgumentException("the vending machine is not holding the specified denomination", "change");
            }

            Change vendingChange = m_Change[index];

            // If we've been asked to remove more of the denomintion than we've got then it's an error
            if(change.Quantity > vendingChange.Quantity)
            {
                throw new ArgumentException("the vending machine is not holding enough of that denomination", "change");
            }

            int newQuantity = vendingChange.Quantity - change.Quantity;

            if(newQuantity == 0)
            {
                // We've got no more instances of the denomination, so remove it
                m_Change.RemoveAt(index);
            }
            else
            {
                // We've still got some left, so update it be removing the supplied quantity
                m_Change[index] = vendingChange.Add(-change.Quantity);
            }
        }
예제 #4
0
 public void Construction_InvalidQuantity()
 {
     Change change = new Change(5, 0);
 }
예제 #5
0
 public void Construction_InvalidDenomination()
 {
     Change change = new Change(0, 10);
 }
예제 #6
0
        /// <summary>
        /// Creates a new change instance which is the sum 
        /// of this quanty and the supplied quantiy
        /// </summary>
        /// <param name="change">The change to add</param>
        /// <returns>A new change object</returns>
        public Change Add(Change change)
        {
            if(change == null) throw new ArgumentNullException("change");
            if(change.Denomination != m_Denomination) throw new ArgumentException("change has different denomination", "change");

            return new Change(m_Denomination, m_Quantity + change.Quantity);
        }