示例#1
0
    public VendingMachineSoftware(List <int> coinKinds, int selectionButtonCount, int coinRackCapacity, int popRackCapacity, int receptacleCapacity)
    {
        // process information
        buttonCount             = selectionButtonCount;
        this.coinRackCapacity   = coinRackCapacity;
        this.popRackCapacity    = popRackCapacity;
        this.receptacleCapacity = receptacleCapacity;
        this.coinKinds          = coinKinds;
        // create machine hardware
        vendingHardware = new VendingMachine(coinKinds.ToArray(), selectionButtonCount, coinRackCapacity, popRackCapacity, receptacleCapacity);
        // default information
        pops          = new List <SoftwarePop>();
        softwareRacks = new List <SoftwareCoinRacks>();
        // create software version of coin racks
        for (int i = 0; i < coinKinds.Count; i++)
        {
            SoftwareCoinRacks newCoinRack = new SoftwareCoinRacks(coinKinds[i], 0);
            softwareRacks.Add(newCoinRack);
        }
        insertedAmount = 0;
        // set up handlers
        CoinSlot coinSlot = vendingHardware.CoinSlot;

        coinSlot.CoinRejected += new EventHandler <CoinEventArgs>(printCoinRejected);
        coinSlot.CoinAccepted += new EventHandler <CoinEventArgs>(printCoinAccepted);
        SelectionButton[] buttons = vendingHardware.SelectionButtons;
        // add handler to each button
        foreach (SelectionButton button in buttons)
        {
            button.Pressed += new EventHandler(printButtonPressed);
        }
        CoinRack[] coinRacks = vendingHardware.CoinRacks;
        // add handler to each coin rack
        foreach (CoinRack coinRack in coinRacks)
        {
            coinRack.CoinAdded += new EventHandler <CoinEventArgs>(printCoinLoaded);
        }
        PopCanRack[] popRacks = vendingHardware.PopCanRacks;
        // add handler to each pop rack
        foreach (PopCanRack rack in popRacks)
        {
            rack.PopCanRemoved += new EventHandler <PopCanEventArgs>(printPopDispensed);
        }
    }
示例#2
0
    public void loadCoins(int coinKindIndex, List <Coin> coins)
    {
        // load in software
        SoftwareCoinRacks getRack = softwareRacks[coinKindIndex];

        getRack.incQuantity(coins.Count);
        // load in hardware
        CoinRack[] racks    = vendingHardware.CoinRacks;
        CoinRack   coinRack = null;

        try
        {
            coinRack = racks[coinKindIndex];
        } catch (Exception e)
        {
            Console.WriteLine("Invalid coin kind index");
        }
        coinRack.LoadCoins(coins);
        //Console.WriteLine("Load: " + coinKinds[coinKindIndex]);
    }
示例#3
0
    public int dispenseChange(int change)
    {
        /* Change Algorithm */
        // change algorithm from A1 (slightly modified)
        int val        = change;
        int upperBound = val + 1;

        int largestCoinVal = 0;

        // loop change process until either
        // no more valid coins, or change
        // value is met (where the loop will
        // be broken)
        while (true)
        {
            // loop through coin slots to find
            // next denomination
            // if found, sets the largestSlot
            // to a Coin, which has a value
            SoftwareCoinRacks largestSlot = null;
            int slotIndex = 0;
            int counter   = 0;
            foreach (SoftwareCoinRacks slot in softwareRacks)
            {
                int rackValue = slot.getValue();
                if (rackValue >= largestCoinVal && rackValue < upperBound)
                {
                    largestCoinVal = rackValue;
                    largestSlot    = slot;
                    slotIndex      = counter;
                }
                counter++;
            }

            // check if largest coin is null
            // if so, then we have not found
            // a suitable change coin (we will
            // short change them)
            if (largestSlot == null)
            {
                break;
            }

            // now we have next largest coin
            // decrease until target met, each
            // time removing a coin from the
            // coin slot, and adding it to the
            // coin change list
            bool runLoop        = true;
            bool changeFinished = false;
            while (runLoop && largestSlot.getQuantity() > 0)
            {
                // decrement change total by coin
                // denomination
                val = val - largestCoinVal;
                if (val >= 0)
                {
                    // returned coin may be "incorrect" if the
                    // slot was loaded incorrectly - this is
                    // the desired functionality
                    CoinRack[] coinRacks = vendingHardware.CoinRacks;
                    // release coin in hardware and in software
                    coinRacks[slotIndex].ReleaseCoin();
                    largestSlot.decQuantity();
                    Console.WriteLine("Dispensing coin: " + largestCoinVal);

                    // if new change value is zero, all change
                    // has been added to the coin change list
                    // so terminate loop
                    if (val == 0)
                    {
                        runLoop        = false;
                        changeFinished = true;
                    }
                }
                else
                {
                    // if change value is negative, reverse
                    // last decrement and move to lower
                    // denomination
                    val     = val + largestCoinVal;
                    runLoop = false;
                }
            }

            // if change finished flag is set, then
            // exit main loop
            if (changeFinished)
            {
                break;
            }

            // else, reset variables and start next
            // loop to find lower denomination
            upperBound     = largestCoinVal;
            largestCoinVal = 0;
        }
        return(val);
    }