//=====This is the function that tell the vending machine what to do after a button is being pressed.
        public void buttonPressed(object sender, EventArgs args)
        {
            Cents           funds         = paymentFacade.funds;       //funds is the amount of coins that left in the vending machine
            SelectionButton buttonPressed = sender as SelectionButton; //event notified

            if (buttonPressed == null)                                 //if the button is invalid
            {
                throw new Exception("'Button pressed' event didn't return a button");
            }

            Cents cost = productFacade.getCost(this.communicationFacade.index); //get the price of the product

            if (funds >= cost)                                                  //if the funds that user provided is greater than the selected product's price
            {
                if (this.productFacade.hasProduct(this.communicationFacade.index))
                {
                    paymentFacade.makeChange(funds - cost);                    //make the change and set avaliable funds
                    productFacade.vendProduct(this.communicationFacade.index); //dispense the product
                }
                else
                {
                    this.communicationFacade.disableButton(this.communicationFacade.index);
                    this.communicationFacade.displayNotEnoughProduct();
                }
            }
            else
            {
                this.communicationFacade.notEnoughFunds();
            }
        }
 //=========This facade using Hardware settings and the CoinKinds list
 public PaymentFacade(HardwareFacade hardware, List <Cents> newCoinKinds)
 {
     this.Hardware  = hardware;
     this.coinKinds = newCoinKinds;
     funds          = new Cents(0);                                                    //initialize funds, funds is the amount of coins that inside the vending machine
     Hardware.CoinSlot.CoinAccepted += new EventHandler <CoinEventArgs>(coinAccepted); //Coin accepted event
 }
        //=========This function using the settings inside the Hardware,and the coinRacks to make change and update the avaliable funds (if possible)
        public void makeChange(Cents change)
        {
            this.Hardware.CoinReceptacle.StoreCoins(); //store coins in the coin racks
            if (change.Value > 0)                      //if the value of change is valid
            {
                //Get the list of the coins in a certain coins kind.
                List <Cents> coinskind = this.coinKinds.OrderBy(coin => coin.Value).ToList();
                for (int i = coinskind.Count - 1; i >= 0; i--)
                {
                    var coins = coinskind[i]; //current coins in a certain coins kind
                                              //if the amount of change is greater than the amount of the current coin kind, get the coin rack for the coin kind
                    if (change.Value >= coins.Value)
                    {
                        var coinRack = this.Hardware.GetCoinRackForCoinKind(coins);
                        if (coinRack.Count != 0)    //if there are some coins of this kind left
                        {
                            coinRack.ReleaseCoin(); //release the coins
                            change -= coinskind[i]; //remove the coins that just being released
                            i++;                    //inc.
                        }
                    }
                    else
                    {
                        coinskind.RemoveAt(i);//Otherwise directly remove the coin at the list
                    }
                }

                funds = change; //the funds left in machine
            }
        }
 public override bool Equals(object obj)
 {
     if (obj is Cents)
     {
         Cents cents = (Cents)obj;
         return(this.Value == cents.Value);
     }
     return(false);
 }
Exemplo n.º 5
0
    public void SelectionMadeHandler(object sender, EventArgs e)
    {
        var index = this.selectionButtonToIndex[(SelectionButton)sender];

        this.productName   = this.hf.ProductKinds[index];
        this.productCost   = this.hf.ProductKinds[index].Cost;
        this.buttonPressed = (ISelectionButton)sender;

        this.SelectionMade(this, new EventArgs());
    }
        /**
         * Accesses the coin rack that handles coins of the specified kind. If none
         * exists, null is returned.
         *
         * @param kind
         *            The value of the coin kind for which the rack is sought.
         * @return The relevant device.
         */
        public CoinRack GetCoinRackForCoinKind(Cents kind)
        {
            var cc = this.coinRackChannels[kind];

            if (cc != null)
            {
                return((CoinRack)cc.Sink);
            }
            return(null);
        }
Exemplo n.º 7
0
    public void ProccessWithCredit()
    {
        this.hf.CoinReceptacle.StoreCoins();

        this.credit          = new Cents(this.credit.Value - (this.productCost.Value - this.availableFunds.Value));
        this.availableFunds  = new Cents(0);
        this.insertedCoins   = new List <Coin>();
        this.changeNeeded    = new Cents(0);
        this.sufficiantFunds = false;
    }
Exemplo n.º 8
0
    public PaymentFacade(HardwareFacade hf)
    {
        this.hf = hf;

        this.productCost    = new Cents(0);
        this.availableFunds = new Cents(0);
        this.changeNeeded   = new Cents(0);
        this.credit         = new Cents(0);

        hf.CoinSlot.CoinAccepted += new EventHandler <CoinEventArgs>(insertSuccessful);
    }
 // Set a new price for a product
 public void setNewPrice(int index, Cents price)
 {
     if (index >= 0 && index < this.hw.ProductKinds.Length)
     {
         this.hw.ProductKinds[index].Cost = price;
     }
     else
     {
         this.error(this, new ErrorEventArgs {
             message = "Error: Incorrect Product selected for configuration"
         });
     }
 }
Exemplo n.º 10
0
        public PaymentFacade(HardwareFacade hardware)
        {
            this.hardware  = hardware;
            availableFunds = 0;
            credit         = new Cents(0);

            this.hardware.CoinSlot.CoinAccepted += new EventHandler <CoinEventArgs>(InsertCoin);

            this.coinKindToCoinRackIndex = new Dictionary <Cents, int>();
            for (int i = 0; i < this.hardware.CoinRacks.Length; i++)
            {
                this.coinKindToCoinRackIndex[this.hardware.GetCoinKindForCoinRack(i)] = i;
            }
        }
Exemplo n.º 11
0
    public void checkFunds(Cents productCost)
    {
        this.productCost = productCost;

        if (this.availableFunds >= productCost)
        {
            this.sufficiantFunds = true;
            this.changeNeeded    = new Cents(availableFunds.Value - productCost.Value);
            this.FundsSufficient(this, new EventArgs());
        }
        else
        {
            this.sufficiantFunds = false;
            this.FundsInsufficient(this, new EventArgs());
        }
    }
Exemplo n.º 12
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (Id.Length != 0)
            {
                hash ^= Id.GetHashCode();
            }
            if (CategoryId != 0)
            {
                hash ^= CategoryId.GetHashCode();
            }
            if (category_ != null)
            {
                hash ^= Category.GetHashCode();
            }
            if (UserId != 0)
            {
                hash ^= UserId.GetHashCode();
            }
            if (Date != 0UL)
            {
                hash ^= Date.GetHashCode();
            }
            if (Cents != 0)
            {
                hash ^= Cents.GetHashCode();
            }
            if (Description.Length != 0)
            {
                hash ^= Description.GetHashCode();
            }
            if (Card.Length != 0)
            {
                hash ^= Card.GetHashCode();
            }
            if (CheckNumber != 0)
            {
                hash ^= CheckNumber.GetHashCode();
            }
            return(hash);
        }
Exemplo n.º 13
0
    public void returnChange()
    {
        this.hf.CoinReceptacle.StoreCoins();

        List <int> coinKinds = new List <int>();

        for (int i = 0; i < this.hf.CoinRacks.Length; i++)
        {
            coinKinds.Add(this.hf.GetCoinKindForCoinRack(i).Value);
        }
        coinKinds.Sort(); coinKinds.Reverse();

        foreach (int i in coinKinds)
        {
            int count   = (int)(changeNeeded.Value / i);
            int n       = 0;
            int counter = 0;
            while (n < count)
            {
                n++;
                try
                {
                    this.hf.GetCoinRackForCoinKind(new Cents(i)).ReleaseCoin();
                    counter++;
                }
                catch
                {
                    System.Console.WriteLine("Ran out of coins");
                }
            }
            changeNeeded = new Cents(changeNeeded.Value - (i * counter));
        }
        this.credit = changeNeeded;

        this.availableFunds  = new Cents(0);
        this.insertedCoins   = new List <Coin>();
        this.changeNeeded    = new Cents(0);
        this.sufficiantFunds = false;
    }
Exemplo n.º 14
0
        public void DispenseChange(Cents cost)
        {   //change method by Tony Tang, from assignment 2
            availableFunds = credit.Value;
            int changeNeeded = availableFunds - cost.Value;

            //make a dictionary
            while (changeNeeded > 0)
            {
                var coinRacksWithMoney = this.coinKindToCoinRackIndex.Where(ck => ck.Key.Value <= changeNeeded && this.hardware.CoinRacks[ck.Value].Count > 0).OrderByDescending(ck => ck.Key.Value);

                if (coinRacksWithMoney.Count() == 0)
                {
                    availableFunds = changeNeeded; // this is what's left as available funds
                }

                var biggestCoinRackCoinKind = coinRacksWithMoney.First().Key;
                var biggestCoinRackIndex    = coinRacksWithMoney.First().Value;
                var biggestCoinRack         = this.hardware.CoinRacks[biggestCoinRackIndex];

                changeNeeded = changeNeeded - biggestCoinRackCoinKind.Value;
                biggestCoinRack.ReleaseCoin();
            }
            credit = new Cents(availableFunds);
        }
Exemplo n.º 15
0
        private void coinSlot_AcceptCoin(object sender, CoinEventArgs e)
        {
            Cents cents = e.Coin.Value;

            funds += cents.Value;
        }
Exemplo n.º 16
0
 private void insertSuccessful(object sender, CoinEventArgs e)
 {
     this.availableFunds += e.Coin.Value;
 }
Exemplo n.º 17
0
 public void InsertCoin(Object sender, CoinEventArgs e)
 {
     credit += e.Coin.Value;
 }
 public Credit(Cents value) : base(value)
 {
 }
 public Debit(Cents value) : base(value)
 {
 }
Exemplo n.º 20
0
 public void InsertFunds(Cents ammount)
 {
     this.credit = this.credit + ammount;
 }
 //=========Add funds, maybe with a credit card? coins?, doesn't matter
 public void addFunds(Cents value)
 {
     this.funds += value;
 }
Exemplo n.º 22
0
	public Rate SetRate(int cent)
    {
        this.cent = (Cents)cent;
        return this;
    }