Пример #1
0
        static void Main(string[] args)
        {
            // 自動販売機を生成する
            VendingController controller = null;

            if (VendingController.createInstance(out controller) != 0)
            {
                return;
            }
            if (controller == null)
            {
                return;
            }

            // 自動販売機を整備する
            if (maintainMachine(ref controller) != 0)
            {
                return;
            }

            // 自動販売機を運用する
            if (controller.mainLoop() != 0)
            {
                return;
            }
        }
Пример #2
0
        // インスタンスの生成
        public static int createInstance(out VendingController controller)
        {
            int result = -1;

            // 返却値の初期化
            controller = null;

            try
            {
                controller = new VendingController();
                if (controller == null)
                {
                    return(result);
                }

                controller.m_dictDrinkStorage = new Dictionary <String, DrinkStorage>();
                if (controller.m_dictDrinkStorage == null)
                {
                    return(result);
                }

                controller.m_dictDrinkPrice = new Dictionary <String, int>();
                if (controller.m_dictDrinkPrice == null)
                {
                    return(result);
                }

                controller.m_supportCoin = new List <int>();
                if (controller.m_supportCoin == null)
                {
                    return(result);
                }

                controller.m_supportBill = new List <int>();
                if (controller.m_supportBill == null)
                {
                    return(result);
                }

                // ここまでくれば正常終了
                result = 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                if (result != 0)
                {
                    controller = null;
                }
            }
            return(result);
        }
        /// <summary>
        /// method purchases products from VendingController.
        /// If there is a problem with the purchase, there will be messages shown
        /// Otherwise the product and it usage will be shown
        /// </summary>
        /// <param name="vm">the VendingController instance</param>
        private static void Buy(VendingController vm)
        {
            Console.WriteLine(vm.ShowAll());
            Console.WriteLine("Choose product to buy: ");
            int wantedProduct = -1;
            var parseOk       = int.TryParse(Console.ReadLine(), out wantedProduct);

            if (parseOk)
            {
                Console.WriteLine(vm.Purchase(wantedProduct));
            }
            else
            {
                Console.WriteLine("Problem with choice, try again!");
            }

            Console.WriteLine("Your balance is: " + vm.Payment + " kr.\n");
        }
        /// <summary>
        /// Main method with a loop for menu choices
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            VendingController vm = new VendingController(true);

            Console.WriteLine("Welcome to Vending Machine!");
            bool finished = false;

            while (!finished)
            {
                ShowMenu();

                Console.WriteLine("Your current balance is " + vm.Payment + " kr.");
                Console.Write("My Choice: ");
                char choice = Console.ReadKey(true).KeyChar;
                Console.WriteLine("\n--------------------");

                switch (choice)
                {
                /*case '0':
                 *  //menu automatically shows... default will do
                 *  break;*/
                case '1':
                    Console.WriteLine(vm.ShowAll());
                    break;

                case '2':
                    InsertCash(vm);
                    break;

                case '3':
                    Buy(vm);
                    break;

                case '4':
                    Console.WriteLine(vm.EndTransaction());
                    finished = true;
                    break;

                default:
                    break;
                }
            }
        }
        /// <summary>
        /// method inserts money and add it to the VendingController.
        /// If the amount is not allowed, there will be messages shown
        /// </summary>
        /// <param name="vm">the VendingController instance</param>
        private static void InsertCash(VendingController vm)
        {
            Console.WriteLine("Money allowed SEK: 1000, 500, 100, 50, 20, 10, 5, 1");
            int amount = 0;

            Console.WriteLine("Amount to insert: ");
            var parseOk = int.TryParse(Console.ReadLine(), out amount);

            if (parseOk)
            {
                bool allowedMoney = vm.InsertMoney(amount);
                if (allowedMoney == false)
                {
                    Console.WriteLine("Try again, only Swedish money of" +
                                      " the denominations 1000, 500, 100, 50, 20, 10, 5, 1 are allowed.");
                }
            }
            else
            {
                Console.WriteLine("Try again, only allowed 0-9");
            }

            Console.WriteLine("Your balance is: " + vm.Payment + " kr.\n");
        }
Пример #6
0
        // 自動販売機を整備する
        private static int maintainMachine(ref VendingController controller)
        {
            int result = -1;

            try
            {
                // 入力値チェック
                if (controller == null)
                {
                    return(result);
                }

                // ドリンク入れる本数
                const int stock_num = 3;

                // 製品ラインナップ (ドリンク種類,価格)
                Tuple <string, int>[] lineup =
                {
                    new Tuple <string, int>("水",   100),
                    new Tuple <string, int>("コーラ", 150),
                    new Tuple <string, int>("お茶", 130)
                };

                // サポートする硬貨を
                int[] supportCoin = { 500, 100, 10, 50 };

                // サポートする紙幣を設定
                int[] supportBill = {};

                // サポートする硬貨を設定
                foreach (int coin in supportCoin)
                {
                    controller.m_supportCoin.Add(coin);
                }

                // サポートする紙幣を設定
                foreach (int bill in supportBill)
                {
                    controller.m_supportBill.Add(bill);
                }

                foreach (Tuple <string, int> element in lineup)
                {
                    // 価格テーブルにドリンク名と金額を追加
                    controller.m_dictDrinkPrice[element.Item1] = element.Item2;

                    // ドリンクストレージをメンテナンスのために取り出し
                    DrinkStorage drinkStorage = null;
                    if (controller.maintainStorage(out drinkStorage, element.Item1) != 0)
                    {
                        return(result);
                    }
                    if (drinkStorage == null)
                    {
                        return(result);
                    }

                    //ドリンクをストレージに追加
                    for (int i = 0; i < stock_num; i++)
                    {
                        Drink drink = null;
                        if (Drink.createInstance(out drink, element.Item1) != 0)
                        {
                            return(result);
                        }

                        if (drinkStorage.pushDrink(ref drink) != 0)
                        {
                            return(result);
                        }
                    }
                }

                // ここまでくれば正常終了
                result = 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                if (result != 0)
                {
                }
            }
            return(result);
        }