Пример #1
0
        public void startDialog(Client client)
        {
            var cash = 0;
            showMenu ();
            Console.WriteLine ("Balance: 0 rub.\n*** \"help\" - print all commands");

            //Initialize method for cashBack
            Action<int, int> cashBack = (weight, index) => {
                while (cash >= weight && _cash[index] > 0) {
                    client.Cash += weight;
                    _cash [index]--;
                    cash -= weight;
                    Console.Write(" " + weight);
                }
            };

            //Method for show balance
            Action printBalance = () => Console.WriteLine ("Balance: " + cash + " rub.");

            while(true)
            {
                Console.Write ("$ ");
                var strs = Console.ReadLine().Split(new char[]{'\t', ' '}, StringSplitOptions.RemoveEmptyEntries);
                if (strs.Length == 0)
                    continue;

                switch(strs[0].ToLower())
                {
                case "balance":
                    printBalance ();
                    break;
                case "get":
                    if (strs.Length != 2)
                        Console.WriteLine ("Please, use this notation:\n\tget 'name', where point from menu called \"name\"");
                    else {
                        var item = _menu.Find (x => x.name.ToLower() == strs [1].ToLower ());
                        if (item.Equals(default(Menu))) {
                            Console.WriteLine ("Unknown menu item " + strs [1]);
                            showMenu ();
                        } else if (cash < item.cost) {
                            Console.WriteLine ("Too little money. Please, insert more " + (item.cost - cash) + " rub.");
                        } else {
                            cash -= item.cost;
                            Console.WriteLine ("Take " + item.name);
                            printBalance ();
                        }
                    }
                    break;
                case "exit":
                    var buf = cash;
                    Console.Write ("Returned");
                    cashBack (10, 0);
                    cashBack (5, 1);
                    cashBack (2, 2);
                    cashBack (1, 3);
                    Console.WriteLine (" rub. (" + (buf - cash) + ")");
                    printBalance ();
                    if (cash > 0) {
                        Console.WriteLine ("Machine cannot give U " + cash + " rubles. Please, insert more money!");
                        break;
                    }
                    return;
                case "help":
                    help ();
                    break;
                case "insert":
                    cash += insert (client, strs);
                    printBalance ();
                    break;
                case "show":
                    showMenu ();
                    printBalance ();
                    break;
                default:
                    Console.WriteLine ("Unknown command: " + strs [0]);
                    break;
                }
            }
        }
Пример #2
0
        int insert(Client client, string[] strs)
        {
            int result = 0, index = 1, buf = -1;

            Func<int, int, bool> tran = (weight, i) => {
                if (client.Cash >= weight)	{
                    client.Cash -= weight;
                    _cash[i]++;
                    result += weight;
                    return true;
                } else {
                    Console.WriteLine("U have no money!");
                    return false;
                }
            };

            if (strs.Length < 2)
                Console.WriteLine ("Take some coins");
            else
                while (index < strs.Length) {
                    if (int.TryParse (strs [index], out buf)) {
                        var change = result;
                        var correct =
                            buf == 10 ? tran (10, 0) :
                            buf == 5 ? tran (5, 1) :
                            buf == 2 ? tran (2, 2) :
                            buf == 1 ? tran (1, 0) :
                            true;
                        if (!correct)
                            return result;
                        if (change == result)
                            Console.WriteLine ("Returned " + strs [index]);
                    } else
                        Console.WriteLine ("Returned " + strs [index]);
                    index++;
                }

            return result;
        }