Ejemplo n.º 1
0
        private int EnterNumberOfCounters()                                //this method does 2 things, doesnt it? (select type & enter number)
        {
            MyConverter.Operation converter = new MyConverter.Operation(); //create new Converter to convert user input (numeric or word) to integer
            converter.alertFromConverter += OnAlertFromConverter;          //subscribe to converter's alert msgs

            bool useNumbers = false;

            Console.Write("Press a key to select input type:\r\n" +
                          "1. Numeric\r\n" +
                          "2. Words\r\n" +
                          ">");
            ConsoleKeyInfo choice;

            do
            {
                choice = Console.ReadKey();

                switch (choice.KeyChar)
                {
                case '1':
                    useNumbers = true;
                    break;

                case '2':
                    useNumbers = false;
                    break;

                case (char)27:    //"esc" key
                    return(0);    //so here you exit the loop by 'ESC' key... but below you need to type 'exit'?

                default:
                    Console.Write("\r\nError. There is no such option. Try again or press \"Esc\" to exit program.\r\n>");
                    break;
                }
            } while ((choice.KeyChar != '1') && (choice.KeyChar != '2'));

            Console.Write("\r\n\r\nInput number of counters:\r\n>");
            int countersNumberParsed = 0;

            do
            {
                string countersNumberGivenByUser = Console.ReadLine();

                if (countersNumberGivenByUser.ToLower() == "exit") //see above for the 'escape loop' logic
                {
                    return(0);
                }
                //how about change it a bit to a 'if - else' logic?
                if (useNumbers)
                {
                    countersNumberParsed = converter.ReadDataAsNumbers(countersNumberGivenByUser);
                }
                else
                {
                    countersNumberParsed = converter.ReadDataAsWords(countersNumberGivenByUser.ToLower());
                }
            } while (countersNumberParsed == 0);
            //it's cool that you allow retry rather than kill the app if user makes a mistake:)
            return(countersNumberParsed);
        }