示例#1
0
 public void TakeMoney(BatchCoin batch)
 {
     foreach (Coin coin in batch)
     {
         TakeMoney(coin);
     }
 }
        /// <summary>
        /// Main Method.
        /// </summary>
        public static void Main()
        {
            CashDesk obj = new CashDesk();
            while (true)
            {
                // Split input command
                string wholeCommand = Console.ReadLine();
                if (wholeCommand == null)
                {
                    continue;
                }

                string[] command = wholeCommand.Split(' ');
                switch (command[0])
                {
                    case "takebill":
                        if (IsInputLenghtLongerThan2(command))
                        {
                            if (command.Length < 3)
                            {
                                obj.TakeMoney(new Bill(int.Parse(command[1])));
                            }
                            else
                            {
                                Console.WriteLine("ERROR: takebill command takes only 1 parameter");
                            }
                        }
                        else
                        {
                            Console.WriteLine("Input Command lenght too short");
                        }

                        break;

                    case "takebatch":
                        if (IsInputLenghtLongerThan2(command))
                        {
                            List<Bill> billList = new List<Bill>();
                            for (int i = 1; i < command.Length; i++)
                            {
                                billList.Add(new Bill(int.Parse(command[i])));
                            }

                            BatchBill batch = new BatchBill(billList);
                            obj.TakeMoney(batch);
                        }

                        break;

                    case "takecoin":
                        if (IsInputLenghtLongerThan2(command))
                        {
                            if (command.Length < 3)
                            {
                                obj.TakeMoney(new Coin(int.Parse(command[1])));
                            }
                            else
                            {
                                Console.WriteLine("ERROR: Takecoin command takes only 1 parameter !");
                            }
                        }

                        break;

                    case "takebatchcoin":
                        if (IsInputLenghtLongerThan2(command))
                        {
                            List<Coin> coinlist = new List<Coin>();
                            for (int i = 1; i < command.Length; i++)
                            {
                                coinlist.Add(new Coin(int.Parse(command[i])));
                            }

                            BatchCoin coinBatch = new BatchCoin(coinlist);
                            obj.TakeMoney(coinBatch);
                            Console.WriteLine("SUCCESS: added new batch of coins !");
                        }

                        break;

                    case "removebill":
                        if (IsInputLenghtLongerThan2(command))
                        {
                            if (command.Length < 3)
                            {
                                obj.RemoveMoney(new Bill(int.Parse(command[1])));
                            }
                            else
                            {
                                Console.WriteLine("ERROR: removebill command takes only 1 parameter !");
                            }
                        }

                        break;

                    case "removebatch":
                        if (IsInputLenghtLongerThan2(command))
                        {
                            List<Bill> billList = new List<Bill>();
                            for (int i = 1; i < command.Length; i++)
                            {
                                billList.Add(new Bill(int.Parse(command[i])));
                            }

                            BatchBill batch = new BatchBill(billList);
                            obj.RemoveMoney(batch);
                        }

                        break;

                    case "removeallbills":
                        if (command.Length < 2)
                        {
                            obj.RemoveAllBills();
                            Console.WriteLine("SUCCES: you have removed all coins !");
                        }
                        else
                        {
                            Console.WriteLine("ERROR: removeallbills command takes no parameters !");
                        }

                        break;

                    case "removecoin":
                        if (IsInputLenghtLongerThan2(command))
                        {
                            if (command.Length < 3)
                            {
                                obj.RemoveMoney(new Coin(int.Parse(command[1])));
                            }
                            else
                            {
                                Console.WriteLine("ERROR: removecoin command takes only 1 parameter !");
                            }
                        }

                        break;

                    case "removebatchcoin":
                        if (IsInputLenghtLongerThan2(command))
                        {
                            List<Coin> coinList = new List<Coin>();
                            for (int i = 1; i < command.Length; i++)
                            {
                                coinList.Add(new Coin(int.Parse(command[i])));
                            }

                            BatchCoin batch = new BatchCoin(coinList);
                            obj.RemoveMoney(batch);
                        }

                        break;

                    case "removeallcoins":
                        if (command.Length < 2)
                        {
                            obj.RemoveAllCoins();
                            Console.WriteLine("SUCCES: you have removed all coins !");
                        }
                        else
                        {
                            Console.WriteLine("ERROR: removeallcoins command takes no parameters !");
                        }

                        break;

                    case "total":
                        if (command.Length < 2)
                        {
                            Console.WriteLine(obj.Total());
                        }
                        else
                        {
                            Console.WriteLine("ERROR: total command takes no parameters !");
                        }

                        break;

                    case "inspect":
                        if (command.Length < 2)
                        {
                            obj.Inspect();
                        }
                        else
                        {
                            Console.WriteLine("ERROR: inspect commands takes no parameters !");
                        }

                        break;

                    case "exit":
                        if (command.Length < 2)
                        {
                            Environment.Exit(0);
                        }
                        else
                        {
                            Console.WriteLine("did you mean \"exit\" ?");
                        }

                        break;

                    default:
                        Console.WriteLine("Unknown Command " + command);
                        break;
                }
            }
        }
示例#3
0
        public string GiveChange(BatchCoin batch, out List<int> rejected)
        {
            int total = batch.Total();

            TakeMoney(batch, out rejected);

            SortedList<int, int> availableAmounts = new SortedList<int, int>(Comparer<int>.Create((x, y) => y.CompareTo(x)));

            foreach (KeyValuePair<int, int> coin in coins)
            {
                if (coin.Key <= total && coin.Value > 0)
                {
                    availableAmounts.Add(coin.Key, coin.Value);
                }
            }

            foreach (KeyValuePair<int, int> bill in bills)
            {
                if (bill.Key * 100 <= total && bill.Value > 0)
                {
                    availableAmounts.Add(bill.Key * 100, bill.Value);
                }
            }

            LinkedList<int> changeAmounts = new LinkedList<int>();

            int lowestBill = bills.Keys.ElementAt(0);

            if (GreadyChange(total, ref availableAmounts, ref changeAmounts))
            {
                if (changeAmounts.Count >= batch.Count)
                {
                    GiveMoney(batch);
                    return "No better batch to return!";
                }
                else
                {
                    StringBuilder returnedBatch = new StringBuilder();
                    foreach (int amount in changeAmounts)
                    {
                        if (amount >= lowestBill * 100)
                        {
                            returnedBatch.Append(amount / 100);
                            returnedBatch.Append("$ bills, ");
                            bills[amount / 100]--;
                        }
                        else
                        {
                            returnedBatch.Append(amount);
                            returnedBatch.Append("¢ coins, ");
                            coins[amount]--;
                        }
                    }
                    returnedBatch.Remove(returnedBatch.Length - 2, 2);

                    return returnedBatch.ToString();
                }
            }
            else
            {
                GiveMoney(batch);
                return "Change is not possible!";
            }
        }
示例#4
0
 private void GiveMoney(BatchCoin batch)
 {
     foreach (Coin coin in batch)
     {
         GiveMoney(coin);
     }
 }
示例#5
0
 public bool TakeMoney(BatchCoin batch, out List<int> rejected)
 {
     rejected = new List<int>();
     bool allCoinsAreCorrect = true;
     foreach (Coin coin in batch)
     {
         if (!TakeMoney(coin))
         {
             allCoinsAreCorrect = false;
             rejected.Add(coin.Amount);
         }
     }
     return allCoinsAreCorrect;
 }
示例#6
0
        public string Sell(int dollarValue, int centValue, BatchBill batchOfBills, BatchCoin batchOfCoins, out List<int> rejectedBills, out List<int> rejectedCoins)
        {
            rejectedBills = new List<int>();
            rejectedCoins = new List<int>();

            List<Bill> validBills = new List<Bill>();
            int validBillsAmount = 0;

            foreach (Bill bill in batchOfBills)
            {
                if (bills.ContainsKey(bill.Amount))
                {
                    validBills.Add(bill);
                    validBillsAmount += bill.Amount;
                }
            }

            List<Coin> validCoins = new List<Coin>();
            int validCoinsAmount = 0;

            foreach (Coin coin in batchOfCoins)
            {
                if (coins.ContainsKey(coin.Amount))
                {
                    validCoins.Add(coin);
                    validCoinsAmount += coin.Amount;
                }
            }

            int validMoney = validBillsAmount * 100 + validCoinsAmount;
            int priceMoney = dollarValue * 100 + centValue;
            bool needToGiveChange = true;
            string output = String.Empty;

            if (validMoney < priceMoney) return "Not enough money!";
            else if (validMoney == priceMoney)
            {
                needToGiveChange = false;
                output = "Item sold successfully! No change needed!";
            }

            bool hasInvalidBills = !TakeMoney(batchOfCoins, out rejectedCoins);
            bool hasInvalidCoins = !TakeMoney(batchOfBills, out rejectedBills);

            if (needToGiveChange)
            {
                output = GiveChange(validMoney - priceMoney);
                bool changeIsGiven = output[0] == 'C' ? false : true;

                if (changeIsGiven) output = "Item sold successfully!\r\nGiven back change: " + output;
                else
                {
                    GiveMoney(batchOfBills);
                    GiveMoney(batchOfCoins);
                }
            }

            return output;
        }
示例#7
0
 public void TakeMoney(BatchCoin batch)
 {
     foreach (Coin coin in batch)
     {
         TakeMoney(coin);
     }
 }
        /// <summary>
        /// Adds a list of Coin objects to the CashDesk object.
        /// </summary>
        /// <param name="batchCoin">List of Coin objects to add if valid.</param>
        public void TakeMoney(BatchCoin batchCoin)
        {
            // Remove the following code to add all but the not legit bill
            bool isLegit = batchCoin.Cast<Coin>().All(coinToAdd => this.validCoinValues.Contains(coinToAdd.Value));

            // Adds all coins if valid
            foreach (Coin coin in batchCoin.Cast<Coin>().Where(coin => isLegit))
            {
                if (!this.coins.ContainsKey(coin.Value))
                {
                    this.coins.Add(coin.Value, 1);
                    this.CoinTotalProp += coin.Value;
                }
                else
                {
                    this.coins[coin.Value]++;
                    this.CoinTotalProp += coin.Value;
                }
            }
        }
 /// <summary>
 /// Removes a list of Coin objects from the CashDesk object if they exist.
 /// </summary>
 /// <param name="batchCoin">List of coin objects to remove.</param>
 public void RemoveMoney(BatchCoin batchCoin)
 {
     // Removes a list of Coins if all coins are present in the CashDesk
     foreach (Coin coin in batchCoin)
     {
         if (!this.coins.ContainsKey(coin.Value))
         {
             Console.WriteLine("ERROR: could not find a {0} coin to remove !", coin.ToString());
         }
         else
         {
             this.coins.Remove(coin.Value);
             this.CoinTotalProp -= coin.Value;
             Console.WriteLine("SUCCES: Removed {0} !", coin.Value);
         }
     }
 }