Class1.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7. namespace Dll
  8. {
  9. public class Class1
  10. {
  11. public static List<int> regions = new List<int> { 102, 103, 111, 113, 116, 118, 121, 122, 123, 124, 125, 126, 130, 134, 136, 138, 142, 147, 150, 152, 154, 155, 156, 158, 159, 161, 163, 164, 172, 173, 174, 177, 178, 180, 181, 186, 190, 193, 196, 197, 198, 199, 252, 277, 299, 702, 716, 725, 750, 754, 761, 763, 774, 777, 790, 797, 799, 977 };
  12. public static List<char> letters = new List<char> { 'A', 'B', 'E', 'K', 'M', 'H', 'O', 'P', 'C', 'T', 'Y', 'X' };
  13. public static bool CheckMark(string mark)
  14. {
  15. if (string.IsNullOrEmpty(mark) || mark.Length != 9)
  16. {
  17. throw new ArgumentException("Строка не соответствует формату");
  18. }
  19. Regex regex = new Regex(@"^[ABEKMHOPCTYX]{1}\d{3}[ABEKMHOPCTYX]{2}\d{2}(\d{1}|[0-9]{2}|[0-7][0-9]|[8][0-8])$");
  20. if (regex.IsMatch(mark))
  21. {
  22. string regionCode = mark.Substring(6, 3).ToUpper();
  23. int regionCodeInt = Convert.ToInt32(regionCode);
  24. if (regions.Contains(regionCodeInt) || (regionCodeInt >= 1 && regionCodeInt <= 99))
  25. {
  26. return true;
  27. }
  28. }
  29. return false;
  30. }
  31. public static string GetNextMarkAfter(string mark)
  32. {
  33. if (string.IsNullOrEmpty(mark) || mark.Length != 9)
  34. {
  35. throw new ArgumentException("Строка не соответствует формату");
  36. }
  37. string newMark = mark;
  38. int number = Convert.ToInt32(mark.Substring(1, 3));
  39. if(number != 999)
  40. {
  41. number++;
  42. newMark = newMark.Remove(1, 3);
  43. newMark = newMark.Insert(1,Convert.ToString(number));
  44. }
  45. else
  46. {
  47. if (newMark[5] != 'X')
  48. {
  49. int index = letters.IndexOf(newMark[5]);
  50. newMark = newMark.Remove(5, 1);
  51. newMark = newMark.Insert(5, Convert.ToString(letters[index + 1]));
  52. }
  53. else if(newMark[4]!= 'X')
  54. {
  55. int index = letters.IndexOf(newMark[4]);
  56. newMark = newMark.Remove(4, 1);
  57. newMark = newMark.Insert(4, Convert.ToString(letters[index + 1]));
  58. }
  59. else if(newMark[0] != 'X')
  60. {
  61. int index = letters.IndexOf(newMark[0]);
  62. newMark = newMark.Remove(0, 1);
  63. newMark = newMark.Insert(0, Convert.ToString(letters[index + 1]));
  64. }
  65. else
  66. {
  67. return "Это конец...";
  68. }
  69. }
  70. return newMark;
  71. }
  72. public static string GetNextMarkAfterInRange(string prevMark, string rangeStart, string rangeEnd)
  73. {
  74. if (string.IsNullOrEmpty(prevMark) || string.IsNullOrEmpty(rangeStart) || string.IsNullOrEmpty(rangeEnd))
  75. {
  76. throw new ArgumentException("Строка не соответствует формату");
  77. }
  78. string newMark = prevMark;
  79. int number = Convert.ToInt32(prevMark.Substring(1, 3));
  80. int startNumber = Convert.ToInt32(rangeStart.Substring(1, 3));
  81. int endNumber = Convert.ToInt32(rangeEnd.Substring(1, 3));
  82. if (number < endNumber && number >= startNumber)
  83. {
  84. number++;
  85. newMark = newMark.Remove(1, 3);
  86. newMark = newMark.Insert(1, Convert.ToString(number));
  87. return newMark;
  88. }
  89. else
  90. {
  91. return "out of stock";
  92. }
  93. }
  94. public static int GetCombinationsCountInRange(string mark1, string mark2)
  95. {
  96. if (string.IsNullOrEmpty(mark1) || string.IsNullOrEmpty(mark2))
  97. {
  98. throw new ArgumentException("Строка не соответствует формату");
  99. }
  100. int startNumber = Convert.ToInt32(mark1.Substring(1, 3));
  101. int endNumber = Convert.ToInt32(mark2.Substring(1, 3));
  102. int count = 0;
  103. while (startNumber <= endNumber)
  104. {
  105. count++;
  106. startNumber++;
  107. }
  108. return count;
  109. }
  110. }
  111. }