Esempio n. 1
0
        private LuckyTicketsConfig.Status InitLuckyTicket(string path)
        {
            string mode = this.ReadFile(path);

            LuckyTicketsConfig.Status status;

            switch (mode)
            {
            case "Moscow":
                this.ticketCounter = new LuckyTicketCounter(new LuckyTicketMoscow());
                status             = LuckyTicketsConfig.Status.Success;
                break;

            case "Piter":
                this.ticketCounter = new LuckyTicketCounter(new LuckyTicketPeter());
                status             = LuckyTicketsConfig.Status.Success;
                break;

            case "File not found":
                status = LuckyTicketsConfig.Status.InvalidPath;
                break;

            default:
                status = LuckyTicketsConfig.Status.InvalidFileContent;
                break;
            }

            return(status);
        }
Esempio n. 2
0
        /// <summary>
        /// The starting point of application for getting the number of lucky tickets
        /// </summary>
        /// <param name="args">The arguments from command line</param>
        /// <returns>Return Code</returns>
        /// <exception cref="ArgumentException">
        /// You don't have enough arguments!
        /// or
        /// You have too many arguments!
        /// </exception>
        public int Run(string[] args)
        {
            Logger.InitLogger();
            try
            {
                LuckyTicketsArgumentParser parser  = new LuckyTicketsArgumentParser();
                LuckyTicketCounter         counter = new LuckyTicketCounter();
                this.countOfLuckyTickets = 0;
                switch (args.Length)
                {
                case NULL_ARGUMENTS_COUNT:
                    this.ShowAbout();
                    Console.ReadKey();
                    Logger.Log.Info("The program started with null parameters, showed Instructions "
                                    + "and ended with return code: Success.");
                    return((int)ReturnCode.Success);

                case NOT_ENOUGH_ARGUMENTS_COUNT:
                    throw new ArgumentException("You don't have enough arguments!");

                case MIN_ARGUMENTS_COUNT:
                    parser.Parse(args, out this.luckyTicketAlgorithm, out this.numberOfDigits);
                    countOfLuckyTickets = counter.Count(this.luckyTicketAlgorithm, this.numberOfDigits);
                    break;

                case MAX_ARGUMENTS_COUNT:
                    parser.Parse(args, out this.luckyTicketAlgorithm, out this.startTicket, out this.boundaryTicket);
                    countOfLuckyTickets = counter.Count(this.luckyTicketAlgorithm, this.startTicket, this.boundaryTicket);
                    break;

                default:
                    if (args.Length > MAX_ARGUMENTS_COUNT)
                    {
                        throw new ArgumentException("You have too many arguments!");
                    }

                    break;
                }

                Console.WriteLine($"There are {countOfLuckyTickets} lucky tickets in your diapason.");
            }
            catch (ArgumentException exception)
            {
                string logMessage = exception + Environment.NewLine +
                                    "The command line arguments: " + this.GetArgumentsAsString(args) + Environment.NewLine;
                Logger.Log.Error(logMessage);
                Console.WriteLine(exception.Message);
                return((int)ReturnCode.Error);
            }

            Logger.Log.Info("The program ended with return code: Success. "
                            + "The command line arguments: " + this.GetArgumentsAsString(args));
            return((int)ReturnCode.Success);
        }
Esempio n. 3
0
        private LuckyTicketsConfig.Status InitLuckyTicket(string path, string digits)
        {
            string mode = this.ReadFile(path);

            LuckyTicketsConfig.Status status;

            int parsedDigits = 0;

            if (int.TryParse(digits, out parsedDigits) && this.IsValidNumberOfDigits(parsedDigits))
            {
                switch (mode)
                {
                case "Moscow":
                    this._ticketCounter = new LuckyTicketCounter(new LuckyTicketMoscow(), parsedDigits);
                    status = LuckyTicketsConfig.Status.Success;
                    break;

                case "Piter":
                    this._ticketCounter = new LuckyTicketCounter(new LuckyTicketPeter(), parsedDigits);
                    status = LuckyTicketsConfig.Status.Success;
                    break;

                case "File not found":
                    status = LuckyTicketsConfig.Status.InvalidPath;
                    break;

                default:
                    status = LuckyTicketsConfig.Status.InvalidFileContent;
                    break;
                }
            }
            else
            {
                status = LuckyTicketsConfig.Status.InvalidArgs;
            }

            return(status);
        }