Ejemplo n.º 1
0
        public bool TakeMoney(Coin coin)
        {
            if (!coins.ContainsKey(coin.Amount)) return false;

            coins[coin.Amount]++;
            return true;
        }
Ejemplo n.º 2
0
        private bool GiveMoney(Coin coin)
        {
            if (!coins.ContainsKey(coin.Amount)) return false;

            coins[coin.Amount]--;
            return true;
        }
Ejemplo n.º 3
0
        public void TakeMoney(Coin coin)
        {
            if (!Coin.Validate(coin))
            {
                Console.WriteLine("The coin {0} is not valid! Unacceptablle!!!", (int)coin);
                return;
            }

            if (coins.ContainsKey(coin))
            {
                coins[coin]++;
            }
            else
            {
                coins.Add(coin, 1);
            }

            total += coin.Value / 100.0;
        }
Ejemplo n.º 4
0
 public BatchCoin(Coin[] coins)
     : this(coins.ToList())
 {
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds a Coin object to the CashDesk object.
        /// </summary>
        /// <param name="singleCoin">Single Coin object to add if valid.</param>
        public void TakeMoney(Coin singleCoin)
        {
            // Adds single coin to CashDesk if coin is valid
            if (this.validCoinValues.Contains(singleCoin.Value))
            {
                this.CoinTotalProp += singleCoin.Value;
                if (!this.coins.ContainsKey(singleCoin.Value))
                {
                    this.coins.Add(singleCoin.Value, 1);
                }
                else
                {
                    this.coins[singleCoin.Value]++;
                }

                Console.WriteLine("SUCCESS: added {0} coin !", new Coin(singleCoin.Value));
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Removes a single Coin object from the CashDesk object.
 /// </summary>
 /// <param name="singleCoin">Single Coin object to remove.</param>
 public void RemoveMoney(Coin singleCoin)
 {
     // If coin exists in CashDesk, removes it
     if (this.coins.Keys.Contains(singleCoin.Value))
     {
         this.coins.Remove(singleCoin.Value);
         this.CoinTotalProp -= singleCoin.Value;
         Console.WriteLine("SUCCES: removed {0} coin !", singleCoin.ToString());
     }
     else
     {
         Console.WriteLine("ERROR: coins list does not contain a {0} coin !", singleCoin.ToString());
     }
 }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hi! Input a command:");

            bool exit = false;
            CashDesk.CashDesk desk = new CashDesk.CashDesk();

            do
            {
                string[] splited = Console.ReadLine().Split(' ');

                switch (splited[0])
                {
                    case "takebill":
                        desk.TakeMoney(new Bill(int.Parse(splited[1])));
                        break;

                    case "takebatch":
                        Bill[] batch = new Bill[splited.Length - 1];

                        for (int i = 0; i < batch.Length; i++)
                        {
                            batch[i] = new Bill(int.Parse(splited[i + 1]));
                        }

                        desk.TakeMoney(new BatchBill(batch));
                        break;

                    case "takecoin":
                        desk.TakeMoney(new Coin(int.Parse(splited[1])));
                        break;

                    case "takecoinbatch":
                        Coin[] coinBatch = new Coin[splited.Length - 1];

                        for (int i = 0; i < coinBatch.Length; i++)
                        {
                            coinBatch[i] = new Coin(int.Parse(splited[i + 1]));
                        }

                        desk.TakeMoney(new BatchCoin(coinBatch));
                        break;

                    case "total":
                        Console.WriteLine(desk.Total);
                        break;

                    case "inspect":
                        desk.Inspect();
                        break;

                    case "getchange":
                        List<Coin> change = new List<Coin>();

                        for (int i = 1; i < splited.Length; i++)
                        {
                            int value;

                            if(int.TryParse(splited[i], out value))
                            {
                                change.Add(new Coin(value));
                            }
                        }

                        var bestChange = desk.GiveChange(change);

                        break;

                    case "exit":
                        exit = true;
                        break;

                    default:
                        Console.WriteLine("Unknown command.");
                        break;
                }

            } while (!exit);
        }