/// <summary> /// Parses the arguments of command line (if the number of command line arguments equals to 2) /// </summary> /// <param name="args">The arguments of command line</param> /// <param name="luckyTicketAlgorithm">The lucky ticket algorithm</param> /// <param name="numberOfDigits">The number of digits in tickets</param> /// <exception cref="ArgumentException"> /// Invalid path to the file! /// or /// Can`t find valid algorithm in the file! /// or /// Your second argument is not a number! /// or /// Number of digits can`t be less or equal zero! /// </exception> public void Parse(string[] args, out ILuckyTicketAlgorithm luckyTicketAlgorithm, out byte numberOfDigits) { if (!File.Exists(args[0])) { throw new ArgumentException("Invalid path to the file!"); } if (!this.IsValidAlgorithm(args[0])) { throw new ArgumentException("Can`t find valid algorithm in the file!"); } luckyTicketAlgorithm = this.luckyTicketAlgorithm; if (!byte.TryParse(args[1], out numberOfDigits)) { throw new ArgumentException("Your second argument is not a number from 0 to 18!"); } if (numberOfDigits <= 0) { throw new ArgumentException("The number of ticket digits can`t be less or equal zero!"); } }
/// <summary> /// Counts lucky tickets with specefied digits using specified algorithm /// </summary> /// <param name="luckyTicketAlgorithm">The lucky ticket algorithm.</param> /// <param name="digitsOfTicket">The digits of ticket.</param> /// <returns></returns> public int Count(ILuckyTicketAlgorithm luckyTicketAlgorithm, byte digitsOfTicket) { Ticket startValue = (Ticket)"0".PadLeft((int)digitsOfTicket, '0'); Ticket boundaryValue = (Ticket)"9".PadLeft((int)digitsOfTicket, '9'); return(this.Count(luckyTicketAlgorithm, startValue, boundaryValue)); }
/// <summary> /// Determines whether lucky ticket counting algorithm is valid /// </summary> /// <param name="path">The path to the file with algorithm</param> /// <returns> /// <c>true</c> if algorithm is valid; otherwise, <c>false</c>. /// </returns> private bool IsValidAlgorithm(string path) { try { using (StreamReader streamReader = new StreamReader(path)) { string stringFromFile; stringFromFile = streamReader.ReadLine(); if (stringFromFile == null) { return(false); } stringFromFile = stringFromFile.ToLower(); if (stringFromFile.Equals(LuckyTicketsAlgorithms.moscow.ToString())) { this.luckyTicketAlgorithm = new MoscowLuckyTicketAlgorithm(); return(true); } if (stringFromFile.Equals(LuckyTicketsAlgorithms.piter.ToString())) { this.luckyTicketAlgorithm = new PiterLuckyTicketAlgorithm(); return(true); } return(false); } } catch (IOException) { return(false); } }
public void Run(string[] commandArguments) { if (!ValidateArguments(commandArguments)) { return; } var algorithmType = GetAlgorithmType(commandArguments[1]); using (StreamReader reader = new StreamReader(commandArguments[0])) { string line; while ((line = reader.ReadLine()) != null) { ticketList.AddRange(luckyTicketParser.CheckTicketsInLine(line)); } } switch (algorithmType) { case AlgorithmType.MoskowAlgirithm: luckyTicketAlgorithm = new MoskowAlgorithm(); logger.Info(StringConstants.MOSKOW_ALGORITHM_USED); break; case AlgorithmType.PiterAlgirithm: luckyTicketAlgorithm = new PiterAlgorithm(); logger.Info(StringConstants.PITER_ALGORITHM_USED); break; } UI.ConsoleOutPut(string.Format(StringConstants.OUTPUT_RESULT, luckyTicketAlgorithm.Algorithm(ticketList))); }
/// <summary> /// Counts lucky tickets in specified range using specified algorithm /// </summary> /// <param name="luckyTicketAlgorithm">The lucky ticket algorithm</param> /// <param name="startValue">The start value from which generator wil be searching for tickets</param> /// <param name="boundaryValue">The boundary value until which generator wil be searching for tickets</param> /// <returns></returns> public int Count(ILuckyTicketAlgorithm luckyTicketAlgorithm, Ticket startValue, Ticket boundaryValue) { LuckyTicketsGenerator generator = LuckyTicketsGenerator.Initialize(startValue, boundaryValue, luckyTicketAlgorithm); int luckyTicketsCount = 0; foreach (Ticket luckyTicket in generator) { luckyTicketsCount++; } return(luckyTicketsCount); }
/// <summary> /// Parses the arguments of command line (if the number of command line arguments equals to 3) /// </summary> /// <param name="args">The arguments of command line</param> /// <param name="luckyTicketAlgorithm">The lucky ticket algorithm</param> /// <param name="startTicket">The start ticket</param> /// <param name="boundaryTicket">The last ticket</param> /// <exception cref="ArgumentException"> /// Invalid path to the file! /// or /// Can`t find valid algorithm in the file! /// or /// Your second argument is not a number! /// </exception> public void Parse(string[] args, out ILuckyTicketAlgorithm luckyTicketAlgorithm, out Ticket startTicket, out Ticket boundaryTicket) { if (!File.Exists(args[0])) { throw new ArgumentException("Invalid path to the file!"); } if (!this.IsValidAlgorithm(args[0])) { throw new ArgumentException("Can`t find valid algorithm in the file!"); } luckyTicketAlgorithm = this.luckyTicketAlgorithm; startTicket = (Ticket)args[1]; boundaryTicket = (Ticket)args[2]; }
/// <summary> /// Initializes the generator of lucky tickets /// <param name="startValue">The start value from which generator wil be searching for tickets</param> /// <param name="boundaryValue">The boundary value until which generator wil be searching for tickets</param> /// <param name="luckyTicketAlgorithm">The lucky ticket algorithm</param> /// <returns>The instance of LuckyTicketsSequenceGenerator type</returns> /// <exception cref="ArgumentException"> /// Start ticket and last ticket can not have different number of digits! /// or /// The value of the last ticket can not be less than the value of the start ticket! /// </exception> public static LuckyTicketsGenerator Initialize(Ticket startValue, Ticket boundaryValue, ILuckyTicketAlgorithm luckyTicketAlgorithm) { if (startValue.NumberOfDigits != boundaryValue.NumberOfDigits) { throw new ArgumentException("Start ticket and last ticket can not have different number of digits!"); } if (boundaryValue.Value < startValue.Value) { throw new ArgumentException("The value of the last ticket can not be less than " + "the value of the start ticket!"); } return(new LuckyTicketsGenerator(startValue, boundaryValue, luckyTicketAlgorithm)); }
/// <summary> /// Initializes a new instance of the <see cref="LuckyTicketsGenerator"/> class /// </summary> /// <param name="startValue">The start value of ticket from which generator wil be searching for tickets</param> /// <param name="boundaryValue">The boundary value of ticket until which generator wil be searching for tickets</param> /// <param name="luckyTicketAlgorithm">The lucky ticket algorithm of <see cref="ILuckyTicketAlgorithm"/> type</param> private LuckyTicketsGenerator(Ticket startValue, Ticket boundaryValue, ILuckyTicketAlgorithm luckyTicketAlgorithm) { this.StartValue = startValue; this.BoundaryValue = boundaryValue; this.LuckyTicketAlgorithm = luckyTicketAlgorithm; }
public PiterAlgorithmTests(PiterAlgorithm algorithm) { ticketAlgorithm = algorithm; }
public MoskowAlgorithmTests(MoskowAlgorithm algorithm) { ticketAlgorithm = algorithm; }