MainWindow.axaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using Avalonia.Controls;
  2. using Avalonia.Controls.Shapes;
  3. using Avalonia.Interactivity;
  4. using Avalonia.Markup.Xaml.MarkupExtensions;
  5. using Avalonia.Media;
  6. using System;
  7. using System.Collections;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. using System.Xml.Linq;
  12. using Tmds.DBus.Protocol;
  13. using static System.Net.Mime.MediaTypeNames;
  14. namespace AvaloniaApplication2
  15. {
  16. // ñòðóêòóðà ïîëüçîâàòåëåé
  17. public struct Users
  18. {
  19. public string lastname;
  20. public string firstname;
  21. public string patronymic;
  22. public string phoneNumber;
  23. public string birthday;
  24. public string gender;
  25. public string birthmonth;
  26. public string birthyear;
  27. public string weekday;
  28. public string horoscope;
  29. public void GetUser(string[] splittedLine)
  30. {
  31. lastname = splittedLine[0];
  32. firstname = splittedLine[1];
  33. patronymic = splittedLine[2];
  34. phoneNumber = splittedLine[3];
  35. gender = splittedLine[4];
  36. birthday = splittedLine[5];
  37. birthmonth = splittedLine[6];
  38. birthyear = splittedLine[7];
  39. weekday = splittedLine[8];
  40. horoscope = splittedLine[9];
  41. }
  42. }
  43. public partial class MainWindow : Window
  44. {
  45. private string[] months = {"ßíâàðü", "Ôåâðàëü", "Ìàðò", "Àïðåëü", "Ìàé", "Èþíü", "Èþëü", "Àâãóñò", "Ñåíòÿáðü", "Îêòÿáðü", "Íîÿáðü", "Äåêàáðü"};
  46. private string[] weekdays = { "Âîñêðåñåíüå", "Ïîíåäåëüíèê", "Âòîðíèê", "Ñðåäà", "×åòâåðã", "Ïÿòíèöà", "Ñóááîòà" };
  47. private string basepath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
  48. public MainWindow()
  49. {
  50. InitializeComponent();
  51. }
  52. private void WriteFile(object sender, RoutedEventArgs e)
  53. {
  54. bool flag = true; // Ïîêàçûâàåò, åñòü ëè íåñîîòâåòñòâèÿ ïàòòåðíàì ñðåäè ââåäåííûõ äàííûõ
  55. string errmessage = ""; // Ïåðåìåííàÿ, êîòîðàÿ õðàíèò íåñîîòâåòñòâèÿ ðåãóëÿðíûì âûðàæåíèÿì
  56. tErrMsg.Text = " ";
  57. Regex[] patterns = { new Regex(@"^[À-ß][à-ÿ]+$"), // Ïàòòåðí äëÿ ïðîâåðêè ôàìèëèè
  58. new Regex(@"^(2|1)\d{3}$"), // Ïàòòåðí äëÿ ïðîâåðêè ãîäà ðîæäåíèÿ
  59. new Regex(@"^\d{1,2}$"), // Ïàòòåðí äëÿ ïðîâåðêè äíÿ ðîæäåíèÿ
  60. new Regex(@"^(7|8)\d{10}$"), // Ïàòòåðí äëÿ ïðîâåðêè òåëåôîíà áåç ôîðìàòèðîâàíèÿ (íàïð., 79000000000)
  61. new Regex(@"(7|8)\(\d{3}\)\d{3}-\d{2}-\d{2}$"), // ñ ôîðìàòèðîâàíèåì (íàïð., 8(900)000-00-00))
  62. new Regex(@"^[À-ß][à-ÿ]*$") // Ïàòòåðí äëÿ ïðîâåðêè èìåíè
  63. };
  64. Users user = new Users();
  65. foreach(TextBox item in new TextBox[]{tbLastName, tbFirstName, tbPatronymic, tbPhone, tbBirthDay, tbBirthYear }) // Èçìåíÿåò çíà÷åíèÿ íåââåäåííûõ ïîëåé ñ Null íà " "
  66. {
  67. if (item.Text is not string)
  68. item.Text = " ";
  69. }
  70. // çàïîëíåíèå ïîëåé ñòðóêòóðû
  71. user.birthmonth = months[cbBirthMonth.SelectedIndex];
  72. if (GenderM.IsChecked == true)
  73. user.gender = "Ìóæñêîé";
  74. else
  75. user.gender = "Æåíñêèé";
  76. if (patterns[0].IsMatch(tbLastName.Text))
  77. user.lastname = tbLastName.Text;
  78. else
  79. {
  80. errmessage += "Ôàìèëèÿ, ";
  81. flag = false;
  82. }
  83. if (patterns[5].IsMatch(tbFirstName.Text))
  84. user.firstname = tbFirstName.Text;
  85. else
  86. {
  87. errmessage += "Èìÿ, ";
  88. flag = false;
  89. }
  90. if (patterns[0].IsMatch(tbPatronymic.Text))
  91. user.patronymic = tbPatronymic.Text;
  92. else
  93. {
  94. errmessage += "Îò÷åñòâî, ";
  95. flag = false;
  96. }
  97. if (patterns[3].IsMatch(tbPhone.Text))
  98. user.phoneNumber = $"8({tbPhone.Text.Substring(1,3)}){tbPhone.Text.Substring(4, 3)}-{tbPhone.Text.Substring(7, 2)}-{tbPhone.Text.Substring(9, 2)}";
  99. else if (patterns[4].IsMatch(tbPhone.Text))
  100. user.phoneNumber = $"8{tbPhone.Text.Substring(1, tbPhone.Text.Length-1)}";
  101. else
  102. {
  103. errmessage += "Òåëåôîí, ";
  104. flag = false;
  105. }
  106. if (patterns[1].IsMatch(tbBirthYear.Text))
  107. user.birthyear = tbBirthYear.Text;
  108. else
  109. {
  110. errmessage += "Ãîä, ";
  111. flag = false;
  112. }
  113. if (patterns[2].IsMatch(tbBirthDay.Text))
  114. user.birthday = tbBirthDay.Text;
  115. else
  116. {
  117. errmessage += "Äåíü, ";
  118. flag = false;
  119. }
  120. if (!flag)
  121. {
  122. tErrMsg.Text = $"{errmessage.Substring(0,errmessage.Length-2)} íå ñîîòâåòñòâóþò ôîðìàòó";
  123. }
  124. else
  125. {
  126. DateOnly birthdate = new DateOnly(Convert.ToInt32(user.birthyear), cbBirthMonth.SelectedIndex+1, Convert.ToInt32(user.birthday));
  127. user.weekday = weekdays[Convert.ToInt32(birthdate.DayOfWeek)];
  128. DateOnly[,] hrsRanges = { {new DateOnly(Convert.ToInt32(user.birthyear), 12, 24), new DateOnly(Convert.ToInt32(user.birthyear), 1, 30) },
  129. {new DateOnly(Convert.ToInt32(user.birthyear), 1, 31), new DateOnly(Convert.ToInt32(user.birthyear), 2, 28) },
  130. {new DateOnly(Convert.ToInt32(user.birthyear), 3, 1), new DateOnly(Convert.ToInt32(user.birthyear), 3, 31) },
  131. {new DateOnly(Convert.ToInt32(user.birthyear), 4, 1), new DateOnly(Convert.ToInt32(user.birthyear), 4, 30) },
  132. {new DateOnly(Convert.ToInt32(user.birthyear), 5, 1), new DateOnly(Convert.ToInt32(user.birthyear), 5, 14) },
  133. {new DateOnly(Convert.ToInt32(user.birthyear), 5, 15), new DateOnly(Convert.ToInt32(user.birthyear), 6, 1) },
  134. {new DateOnly(Convert.ToInt32(user.birthyear), 6, 2), new DateOnly(Convert.ToInt32(user.birthyear), 6, 12) },
  135. {new DateOnly(Convert.ToInt32(user.birthyear), 6, 13), new DateOnly(Convert.ToInt32(user.birthyear), 7, 6) },
  136. {new DateOnly(Convert.ToInt32(user.birthyear), 6, 24), new DateOnly(Convert.ToInt32(user.birthyear), 6, 24) },
  137. {new DateOnly(Convert.ToInt32(user.birthyear), 7, 7), new DateOnly(Convert.ToInt32(user.birthyear), 7, 31) },
  138. {new DateOnly(Convert.ToInt32(user.birthyear), 8, 1), new DateOnly(Convert.ToInt32(user.birthyear), 8, 28) },
  139. {new DateOnly(Convert.ToInt32(user.birthyear), 8, 29), new DateOnly(Convert.ToInt32(user.birthyear), 9, 13) },
  140. {new DateOnly(Convert.ToInt32(user.birthyear), 9, 14), new DateOnly(Convert.ToInt32(user.birthyear), 9, 27) },
  141. {new DateOnly(Convert.ToInt32(user.birthyear), 9, 28), new DateOnly(Convert.ToInt32(user.birthyear), 10, 15) },
  142. {new DateOnly(Convert.ToInt32(user.birthyear), 10, 16), new DateOnly(Convert.ToInt32(user.birthyear), 11, 8) },
  143. {new DateOnly(Convert.ToInt32(user.birthyear), 11, 9), new DateOnly(Convert.ToInt32(user.birthyear), 11, 28) },
  144. {new DateOnly(Convert.ToInt32(user.birthyear), 11, 29), new DateOnly(Convert.ToInt32(user.birthyear), 12, 23) },};
  145. if (birthdate >= hrsRanges[0, 0] & birthdate <= hrsRanges[0, 1])
  146. user.horoscope = "Ìîðîç";
  147. else if (birthdate >= hrsRanges[1, 0] & birthdate <= hrsRanges[1, 1])
  148. user.horoscope = "Âåëåñ";
  149. else if (birthdate >= hrsRanges[2, 0] & birthdate <= hrsRanges[2, 1])
  150. user.horoscope = "Ìàêîøü";
  151. else if (birthdate >= hrsRanges[3, 0] & birthdate <= hrsRanges[3, 1])
  152. user.horoscope = "Æèâà";
  153. else if (birthdate >= hrsRanges[4, 0] & birthdate <= hrsRanges[4, 1])
  154. user.horoscope = "ßðèëî";
  155. else if (birthdate >= hrsRanges[5, 0] & birthdate <= hrsRanges[5, 1])
  156. user.horoscope = "Ëåëÿ";
  157. else if (birthdate >= hrsRanges[6, 0] & birthdate <= hrsRanges[6, 1])
  158. user.horoscope = "Êîñòðîìà";
  159. else if (birthdate >= hrsRanges[8, 0] & birthdate <= hrsRanges[8, 1])
  160. user.horoscope = "Èâàí Êóïàëà";
  161. else if (birthdate >= hrsRanges[7, 0] & birthdate <= hrsRanges[7, 1])
  162. user.horoscope = "Äîäîëà";
  163. else if (birthdate >= hrsRanges[9, 0] & birthdate <= hrsRanges[9, 1])
  164. user.horoscope = "Ëàäà";
  165. else if (birthdate >= hrsRanges[10, 0] & birthdate <= hrsRanges[10, 1])
  166. user.horoscope = "Ïåðóí";
  167. else if (birthdate >= hrsRanges[11, 0] & birthdate <= hrsRanges[11, 1])
  168. user.horoscope = "Ñåâà";
  169. else if (birthdate >= hrsRanges[12, 0] & birthdate <= hrsRanges[12, 1])
  170. user.horoscope = "Ðîæàíèöà";
  171. else if (birthdate >= hrsRanges[13, 0] & birthdate <= hrsRanges[13, 1])
  172. user.horoscope = "Ñâàðîæè÷è";
  173. else if (birthdate >= hrsRanges[14, 0] & birthdate <= hrsRanges[14, 1])
  174. user.horoscope = "Ìîðåíà";
  175. else if (birthdate >= hrsRanges[15, 0] & birthdate <= hrsRanges[15, 1])
  176. user.horoscope = "Çèìà";
  177. else if (birthdate >= hrsRanges[16, 0] & birthdate <= hrsRanges[16, 1])
  178. user.horoscope = "Êàðà÷óí";
  179. else
  180. user.horoscope = "Íèêòî";
  181. try
  182. {
  183. using (StreamWriter writer = new StreamWriter(basepath + @"\users.csv", true, System.Text.Encoding.UTF8))
  184. {
  185. writer.WriteLine($"{user.lastname};{user.firstname};{user.patronymic};{user.phoneNumber};{user.gender};{user.birthday};{user.birthmonth};{user.birthyear};{user.weekday};{user.horoscope}");
  186. tErrMsg.Text = "Çàïèñü äîáàâëåíà";
  187. }
  188. }
  189. catch (Exception)
  190. {
  191. tErrMsg.Text = "Âîçíèêëà îøèáêà ïðè äîáàâëåíèè çàïèñè";
  192. }
  193. }
  194. }
  195. private void ShowUsers(object sender, RoutedEventArgs e)
  196. {
  197. UsersPanel.IsVisible = true;
  198. WritePanel.IsVisible = false;
  199. ArrayList allUsers = new ArrayList();
  200. using (StreamReader reader = new StreamReader(basepath + @"\users.csv"))
  201. {
  202. reader.ReadLine();
  203. string line;
  204. string[] splittedLine;
  205. Users user = new Users();
  206. while ((line = reader.ReadLine()) != null)
  207. {
  208. splittedLine = line.Split(";");
  209. user.GetUser(splittedLine);
  210. allUsers.Add(user);
  211. }
  212. }
  213. Users[] users = (Users[])allUsers.ToArray(typeof(Users)); // Ïðåîáðàçîâàíèå ArrayList â ìàññèâ ñòðóêòóð
  214. users = users.OrderBy(a => a.lastname).ThenBy(b => b.firstname).ToArray(); // Ñîðòèðîâêà ïîëüçîâàòåëåé ïî èìåíè è ôàìèëèè
  215. foreach(Users user in users)
  216. {
  217. TextBlock FIO = new TextBlock()
  218. {
  219. Text = $"ÔÈÎ: {user.lastname} {user.firstname} {user.patronymic}",
  220. };
  221. TextBlock Phone = new TextBlock()
  222. {
  223. Text = $"Íîìåð òåëåôîíà: {user.phoneNumber}",
  224. };
  225. string textbirthday;
  226. string textbirthmonth;
  227. if (Convert.ToInt32(user.birthday) < 10)
  228. textbirthday = $"0{user.birthday}";
  229. else
  230. textbirthday = user.birthday;
  231. if (Array.IndexOf(months, user.birthmonth) + 1 < 10)
  232. textbirthmonth = $"0{Array.IndexOf(months, user.birthmonth) + 1}";
  233. else
  234. textbirthmonth = $"{Array.IndexOf(months, user.birthmonth) + 1}";
  235. TextBlock Birth = new TextBlock()
  236. {
  237. Text = $"Äåíü ðîæäåíèÿ: {textbirthday}.{textbirthmonth}.{user.birthyear}",
  238. };
  239. TextBlock WeekDay = new TextBlock()
  240. {
  241. Text = $"Äåíü íåäåëè ðîæäåíèÿ: {user.weekday}",
  242. };
  243. TextBlock Male = new TextBlock()
  244. {
  245. Text = "Ïîë: Ìóæñêîé",
  246. Foreground = new SolidColorBrush(Color.FromRgb(145, 210, 255)),
  247. };
  248. TextBlock Female = new TextBlock()
  249. {
  250. Text = "Ïîë: Æåíñêèé",
  251. Foreground = new SolidColorBrush(Color.FromRgb(229, 128, 255)),
  252. };
  253. TextBlock HoroScope = new TextBlock()
  254. {
  255. Text = $"Ãîðîñêîï: {user.horoscope}",
  256. };
  257. StackPanel spUser = new StackPanel();
  258. spUser.HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center;
  259. spUser.Children.Add(FIO);
  260. spUser.Children.Add(Phone);
  261. spUser.Children.Add(Birth);
  262. spUser.Children.Add(WeekDay);
  263. if (user.gender == "Ìóæñêîé")
  264. spUser.Children.Add(Male);
  265. else
  266. spUser.Children.Add(Female);
  267. spUser.Children.Add(HoroScope);
  268. Border border = new Border();
  269. border.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255));
  270. border.BorderBrush = Brushes.BlueViolet;
  271. border.BorderThickness = new Avalonia.Thickness(2);
  272. border.CornerRadius = new Avalonia.CornerRadius(50);
  273. border.Margin = new Avalonia.Thickness(10);
  274. border.Padding = new Avalonia.Thickness(20);
  275. border.Child = spUser;
  276. UsersPanel.Children.Add(border);
  277. }
  278. }
  279. private void ClickBack(object sender, RoutedEventArgs e)
  280. {
  281. UsersPanel.IsVisible = false;
  282. WritePanel.IsVisible = true;
  283. for (int i = UsersPanel.Children.Count - 1; i > 0; i--) // Óáèðàåò áëîêè ñ ïîëüçîâàòåëÿìè
  284. {
  285. UsersPanel.Children.Remove(UsersPanel.Children[i]);
  286. }
  287. }
  288. }
  289. }