readme.md 4.9 KB

Получение данных из внешних источников. JSON.

University

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfApp3
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<Student> StudentList { get; set; }
        private Dictionary<string, object> filters = new Dictionary<string, object>();

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Globals.dataProvider = new LocalDataProvider();
            StudentList = new ObservableCollection<Student>(Globals.dataProvider.GetStudents());
        }

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void NameFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            filters["Name"] = ((TextBox)sender).Text;
            ApplyFilter();
        }

        private void NationFilter_TextChanged(object sender, TextChangedEventArgs e)
        {
            filters["Nation"] = ((TextBox)sender).Text;
            ApplyFilter();
        }

        private void ApplyFilter()
        {
            var filteredStudents = Globals.dataProvider.GetStudents();

            // Фильтрация по словарю
            foreach (var filter in filters)
            {
                filteredStudents = filteredStudents.Where(s => s.GetType().GetProperty(filter.Key).GetValue(s, null).ToString().Contains(filter.Value.ToString()));
            }

            // Фильтрация по условию
            filteredStudents = filteredStudents.Where(s => s.Age >= 20 && s.Course > 2);

            StudentList = new ObservableCollection<Student>(filteredStudents);
        }
    }

    internal interface IDataProvider
    {
        IEnumerable<Student> GetStudents();
    }

    public class LocalDataProvider : IDataProvider
    {
        public IEnumerable<Student> GetStudents()
        {
            return new List<Student>
            {
                new Student { Id = 1, Name = "Хэй Вэнь", Age = 24, Country = "Китай", GPA = 5.0, Speciality = "Программирование", Grant = 25000.0, Course = 4, EnrollmentDate = new DateTime(2020, 9, 1), IsFullTime = true },
                new Student { Id = 2, Name = "Семен Хомяк", Age = 22, Country = "Россия", GPA = 3.7, Speciality = "Инженерия", Grant = 10000.0, Course = 2, EnrollmentDate = new DateTime(2022, 1, 15), IsFullTime = false },
                new Student { Id = 3, Name = "Джейкоб Ли", Age = 19, Country = "Америка", GPA = 4.4, Speciality = "Лингвистика", Grant = 20000.0, Course = 1, EnrollmentDate = new DateTime(2023, 8, 20), IsFullTime = true },
                new Student { Id = 4, Name = "Арамсунторнсук Пирапонг", Age = 18, Country = "Таиланд", GPA = 4.1, Speciality = "Логистика", Grant = 15000.0, Course = 1, EnrollmentDate = new DateTime(2023, 4, 6), IsFullTime = false },
                new Student { Id = 5, Name = "Рипка Дарина", Age = 18, Country = "Украина", GPA = 4.8, Speciality = "Дизайн", Grant = 23000.0, Course = 1, EnrollmentDate = new DateTime(2023, 5, 15), IsFullTime = true },
            };
        }
    }

    public class Student
    {

        public int Id { get; set; }
        public string Name { get; set; }
        public string Speciality { get; set; }
        public string Country { get; set; }
        public double GPA { get; set; }
        public double Grant { get; set; }
        public DateTime EnrollmentDate { get; set; }
        public bool IsFullTime { get; set; }
        public int Age { get; internal set; }
        public int Course { get; internal set; }
    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp3.model
{
    public class JSONDataProvider
    {
        private List<Student> _StudentList;

        public JSONDataProvider()
        {
            var serializer = new DataContractJsonSerializer(typeof(Student[]));
            using (var sr = new StreamReader("./test.json"))
            {
                _StudentList = ((Student[])serializer.ReadObject(sr.BaseStream)).ToList();
            }
        }

        public IEnumerable<Student> getStudents()
        {
            return _StudentList;
        }
    }

    internal class DataContractJsonSerializer
    {
        private Type type;

        public DataContractJsonSerializer(Type type)
        {
            this.type = type;
        }

        internal Student[] ReadObject(Stream baseStream)
        {
            throw new NotImplementedException();
        }
    }
}