Загрузка данных


using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;

namespace WpfApp_Lab
{
    public partial class MainWindow : Window
    {
        // Храним строки напрямую
        private List<string> employees = new List<string>();
        private string filePath = "employees.txt";

        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Проверка пустых полей
                if (string.IsNullOrWhiteSpace(txtLastName.Text) ||
                    string.IsNullOrWhiteSpace(txtSalary.Text) ||
                    string.IsNullOrWhiteSpace(cmbPosition.Text) ||
                    string.IsNullOrWhiteSpace(cmbCity.Text) ||
                    string.IsNullOrWhiteSpace(cmbStreet.Text) ||
                    string.IsNullOrWhiteSpace(txtHouse.Text))
                {
                    MessageBox.Show("Все поля должны быть заполнены!");
                    return;
                }

                // Проверка зарплаты
                if (!decimal.TryParse(txtSalary.Text, out decimal salary) || salary < 0)
                {
                    MessageBox.Show("Зарплата должна быть положительным числом!");
                    return;
                }

                // Формируем строку для ListBox и файла
                string employeeLine = $"{txtLastName.Text.Trim()} | {salary} руб. | " +
                                      $"{cmbPosition.Text.Trim()} | {cmbCity.Text.Trim()}, " +
                                      $"{cmbStreet.Text.Trim()}, {txtHouse.Text.Trim()}";

                employees.Add(employeeLine);
                UpdateListBox();
                ClearInputs();

                MessageBox.Show("Работник добавлен!");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Ошибка: {ex.Message}");
            }
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (employees.Count == 0)
                {
                    MessageBox.Show("Список пуст!");
                    return;
                }

                // Сохраняем как есть — строки с разделителями
                File.WriteAllLines(filePath, employees);
                MessageBox.Show($"Сохранено в {filePath}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Ошибка сохранения: {ex.Message}");
            }
        }

        private void BtnLoad_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(filePath))
                {
                    MessageBox.Show("Файл не найден!");
                    return;
                }

                employees.Clear();
                employees.AddRange(File.ReadAllLines(filePath));
                UpdateListBox();

                MessageBox.Show($"Загружено {employees.Count} записей");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Ошибка загрузки: {ex.Message}");
            }
        }

        private void UpdateListBox()
        {
            listBoxEmployees.Items.Clear();
            foreach (var emp in employees)
            {
                listBoxEmployees.Items.Add(emp);
            }
        }

        private void ClearInputs()
        {
            txtLastName.Clear();
            txtSalary.Clear();
            cmbPosition.Text = "";
            cmbCity.Text = "";
            cmbStreet.Text = "";
            txtHouse.Clear();
        }
    }
}