using Avalonia.Controls; using Avalonia.Controls.Shapes; using Avalonia.Interactivity; using Avalonia.Markup.Xaml.MarkupExtensions; using Avalonia.Media; using System; using System.Collections; using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Xml.Linq; using Tmds.DBus.Protocol; using static System.Net.Mime.MediaTypeNames; namespace AvaloniaApplication2 { // структура пользователей public struct Users { public string lastname; public string firstname; public string patronymic; public string phoneNumber; public string birthday; public string gender; public string birthmonth; public string birthyear; public string weekday; public string horoscope; public void GetUser(string[] splittedLine) { lastname = splittedLine[0]; firstname = splittedLine[1]; patronymic = splittedLine[2]; phoneNumber = splittedLine[3]; gender = splittedLine[4]; birthday = splittedLine[5]; birthmonth = splittedLine[6]; birthyear = splittedLine[7]; weekday = splittedLine[8]; horoscope = splittedLine[9]; } } public partial class MainWindow : Window { private string[] months = {"Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"}; private string[] weekdays = { "Воскресенье", "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота" }; private string basepath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName; public MainWindow() { InitializeComponent(); } private void WriteFile(object sender, RoutedEventArgs e) { bool flag = true; // Показывает, есть ли несоответствия паттернам среди введенных данных string errmessage = ""; // Переменная, которая хранит несоответствия регулярным выражениям tErrMsg.Text = " "; Regex[] patterns = { new Regex(@"^[А-Я][а-я]+$"), // Паттерн для проверки фамилии new Regex(@"^(2|1)\d{3}$"), // Паттерн для проверки года рождения new Regex(@"^\d{1,2}$"), // Паттерн для проверки дня рождения new Regex(@"^(7|8)\d{10}$"), // Паттерн для проверки телефона без форматирования (напр., 79000000000) new Regex(@"(7|8)\(\d{3}\)\d{3}-\d{2}-\d{2}$"), // с форматированием (напр., 8(900)000-00-00)) new Regex(@"^[А-Я][а-я]*$") // Паттерн для проверки имени }; Users user = new Users(); foreach(TextBox item in new TextBox[]{tbLastName, tbFirstName, tbPatronymic, tbPhone, tbBirthDay, tbBirthYear }) // Изменяет значения невведенных полей с Null на " " { if (item.Text is not string) item.Text = " "; } // заполнение полей структуры user.birthmonth = months[cbBirthMonth.SelectedIndex]; if (GenderM.IsChecked == true) user.gender = "Мужской"; else user.gender = "Женский"; if (patterns[0].IsMatch(tbLastName.Text)) user.lastname = tbLastName.Text; else { errmessage += "Фамилия, "; flag = false; } if (patterns[5].IsMatch(tbFirstName.Text)) user.firstname = tbFirstName.Text; else { errmessage += "Имя, "; flag = false; } if (patterns[0].IsMatch(tbPatronymic.Text)) user.patronymic = tbPatronymic.Text; else { errmessage += "Отчество, "; flag = false; } if (patterns[3].IsMatch(tbPhone.Text)) user.phoneNumber = $"8({tbPhone.Text.Substring(1,3)}){tbPhone.Text.Substring(4, 3)}-{tbPhone.Text.Substring(7, 2)}-{tbPhone.Text.Substring(9, 2)}"; else if (patterns[4].IsMatch(tbPhone.Text)) user.phoneNumber = $"8{tbPhone.Text.Substring(1, tbPhone.Text.Length-1)}"; else { errmessage += "Телефон, "; flag = false; } if (patterns[1].IsMatch(tbBirthYear.Text)) user.birthyear = tbBirthYear.Text; else { errmessage += "Год, "; flag = false; } if (patterns[2].IsMatch(tbBirthDay.Text)) user.birthday = tbBirthDay.Text; else { errmessage += "День, "; flag = false; } if (!flag) { tErrMsg.Text = $"{errmessage.Substring(0,errmessage.Length-2)} не соответствуют формату"; } else { DateOnly birthdate = new DateOnly(Convert.ToInt32(user.birthyear), cbBirthMonth.SelectedIndex+1, Convert.ToInt32(user.birthday)); user.weekday = weekdays[Convert.ToInt32(birthdate.DayOfWeek)]; DateOnly[,] hrsRanges = { {new DateOnly(Convert.ToInt32(user.birthyear), 12, 24), new DateOnly(Convert.ToInt32(user.birthyear), 1, 30) }, {new DateOnly(Convert.ToInt32(user.birthyear), 1, 31), new DateOnly(Convert.ToInt32(user.birthyear), 2, 28) }, {new DateOnly(Convert.ToInt32(user.birthyear), 3, 1), new DateOnly(Convert.ToInt32(user.birthyear), 3, 31) }, {new DateOnly(Convert.ToInt32(user.birthyear), 4, 1), new DateOnly(Convert.ToInt32(user.birthyear), 4, 30) }, {new DateOnly(Convert.ToInt32(user.birthyear), 5, 1), new DateOnly(Convert.ToInt32(user.birthyear), 5, 14) }, {new DateOnly(Convert.ToInt32(user.birthyear), 5, 15), new DateOnly(Convert.ToInt32(user.birthyear), 6, 1) }, {new DateOnly(Convert.ToInt32(user.birthyear), 6, 2), new DateOnly(Convert.ToInt32(user.birthyear), 6, 12) }, {new DateOnly(Convert.ToInt32(user.birthyear), 6, 13), new DateOnly(Convert.ToInt32(user.birthyear), 7, 6) }, {new DateOnly(Convert.ToInt32(user.birthyear), 6, 24), new DateOnly(Convert.ToInt32(user.birthyear), 6, 24) }, {new DateOnly(Convert.ToInt32(user.birthyear), 7, 7), new DateOnly(Convert.ToInt32(user.birthyear), 7, 31) }, {new DateOnly(Convert.ToInt32(user.birthyear), 8, 1), new DateOnly(Convert.ToInt32(user.birthyear), 8, 28) }, {new DateOnly(Convert.ToInt32(user.birthyear), 8, 29), new DateOnly(Convert.ToInt32(user.birthyear), 9, 13) }, {new DateOnly(Convert.ToInt32(user.birthyear), 9, 14), new DateOnly(Convert.ToInt32(user.birthyear), 9, 27) }, {new DateOnly(Convert.ToInt32(user.birthyear), 9, 28), new DateOnly(Convert.ToInt32(user.birthyear), 10, 15) }, {new DateOnly(Convert.ToInt32(user.birthyear), 10, 16), new DateOnly(Convert.ToInt32(user.birthyear), 11, 8) }, {new DateOnly(Convert.ToInt32(user.birthyear), 11, 9), new DateOnly(Convert.ToInt32(user.birthyear), 11, 28) }, {new DateOnly(Convert.ToInt32(user.birthyear), 11, 29), new DateOnly(Convert.ToInt32(user.birthyear), 12, 23) },}; if (birthdate >= hrsRanges[0, 0] & birthdate <= hrsRanges[0, 1]) user.horoscope = "Мороз"; else if (birthdate >= hrsRanges[1, 0] & birthdate <= hrsRanges[1, 1]) user.horoscope = "Велес"; else if (birthdate >= hrsRanges[2, 0] & birthdate <= hrsRanges[2, 1]) user.horoscope = "Макошь"; else if (birthdate >= hrsRanges[3, 0] & birthdate <= hrsRanges[3, 1]) user.horoscope = "Жива"; else if (birthdate >= hrsRanges[4, 0] & birthdate <= hrsRanges[4, 1]) user.horoscope = "Ярило"; else if (birthdate >= hrsRanges[5, 0] & birthdate <= hrsRanges[5, 1]) user.horoscope = "Леля"; else if (birthdate >= hrsRanges[6, 0] & birthdate <= hrsRanges[6, 1]) user.horoscope = "Кострома"; else if (birthdate >= hrsRanges[8, 0] & birthdate <= hrsRanges[8, 1]) user.horoscope = "Иван Купала"; else if (birthdate >= hrsRanges[7, 0] & birthdate <= hrsRanges[7, 1]) user.horoscope = "Додола"; else if (birthdate >= hrsRanges[9, 0] & birthdate <= hrsRanges[9, 1]) user.horoscope = "Лада"; else if (birthdate >= hrsRanges[10, 0] & birthdate <= hrsRanges[10, 1]) user.horoscope = "Перун"; else if (birthdate >= hrsRanges[11, 0] & birthdate <= hrsRanges[11, 1]) user.horoscope = "Сева"; else if (birthdate >= hrsRanges[12, 0] & birthdate <= hrsRanges[12, 1]) user.horoscope = "Рожаница"; else if (birthdate >= hrsRanges[13, 0] & birthdate <= hrsRanges[13, 1]) user.horoscope = "Сварожичи"; else if (birthdate >= hrsRanges[14, 0] & birthdate <= hrsRanges[14, 1]) user.horoscope = "Морена"; else if (birthdate >= hrsRanges[15, 0] & birthdate <= hrsRanges[15, 1]) user.horoscope = "Зима"; else if (birthdate >= hrsRanges[16, 0] & birthdate <= hrsRanges[16, 1]) user.horoscope = "Карачун"; else user.horoscope = "Никто"; try { using (StreamWriter writer = new StreamWriter(basepath + @"\users.csv", true, System.Text.Encoding.UTF8)) { writer.WriteLine($"{user.lastname};{user.firstname};{user.patronymic};{user.phoneNumber};{user.gender};{user.birthday};{user.birthmonth};{user.birthyear};{user.weekday};{user.horoscope}"); tErrMsg.Text = "Запись добавлена"; } } catch (Exception) { tErrMsg.Text = "Возникла ошибка при добавлении записи"; } } } private void ShowUsers(object sender, RoutedEventArgs e) { UsersPanel.IsVisible = true; WritePanel.IsVisible = false; ArrayList allUsers = new ArrayList(); using (StreamReader reader = new StreamReader(basepath + @"\users.csv")) { reader.ReadLine(); string line; string[] splittedLine; Users user = new Users(); while ((line = reader.ReadLine()) != null) { splittedLine = line.Split(";"); user.GetUser(splittedLine); allUsers.Add(user); } } Users[] users = (Users[])allUsers.ToArray(typeof(Users)); // Преобразование ArrayList в массив структур users = users.OrderBy(a => a.lastname).ThenBy(b => b.firstname).ToArray(); // Сортировка пользователей по имени и фамилии foreach(Users user in users) { TextBlock FIO = new TextBlock() { Text = $"ФИО: {user.lastname} {user.firstname} {user.patronymic}", }; TextBlock Phone = new TextBlock() { Text = $"Номер телефона: {user.phoneNumber}", }; string textbirthday; string textbirthmonth; if (Convert.ToInt32(user.birthday) < 10) textbirthday = $"0{user.birthday}"; else textbirthday = user.birthday; if (Array.IndexOf(months, user.birthmonth) + 1 < 10) textbirthmonth = $"0{Array.IndexOf(months, user.birthmonth) + 1}"; else textbirthmonth = $"{Array.IndexOf(months, user.birthmonth) + 1}"; TextBlock Birth = new TextBlock() { Text = $"День рождения: {textbirthday}.{textbirthmonth}.{user.birthyear}", }; TextBlock WeekDay = new TextBlock() { Text = $"День недели рождения: {user.weekday}", }; TextBlock Male = new TextBlock() { Text = "Пол: Мужской", Foreground = new SolidColorBrush(Color.FromRgb(145, 210, 255)), }; TextBlock Female = new TextBlock() { Text = "Пол: Женский", Foreground = new SolidColorBrush(Color.FromRgb(229, 128, 255)), }; TextBlock HoroScope = new TextBlock() { Text = $"Гороскоп: {user.horoscope}", }; StackPanel spUser = new StackPanel(); spUser.HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center; spUser.Children.Add(FIO); spUser.Children.Add(Phone); spUser.Children.Add(Birth); spUser.Children.Add(WeekDay); if (user.gender == "Мужской") spUser.Children.Add(Male); else spUser.Children.Add(Female); spUser.Children.Add(HoroScope); Border border = new Border(); border.Background = new SolidColorBrush(Color.FromRgb(255, 255, 255)); border.BorderBrush = Brushes.BlueViolet; border.BorderThickness = new Avalonia.Thickness(2); border.CornerRadius = new Avalonia.CornerRadius(50); border.Margin = new Avalonia.Thickness(10); border.Padding = new Avalonia.Thickness(20); border.Child = spUser; UsersPanel.Children.Add(border); } } private void ClickBack(object sender, RoutedEventArgs e) { UsersPanel.IsVisible = false; WritePanel.IsVisible = true; for (int i = UsersPanel.Children.Count - 1; i > 0; i--) // Убирает блоки с пользователями { UsersPanel.Children.Remove(UsersPanel.Children[i]); } } } }