No Description

ababin a7e561c92e Добавить 'README.md' 1 month ago
.vs 69c2fd7537 lab 1 month ago
WpfApp1 69c2fd7537 lab 1 month ago
img eba4ddcb3b Загрузить файлы 'img' 1 month ago
README.md a7e561c92e Добавить 'README.md' 1 month ago
WpfApp1.sln 69c2fd7537 lab 1 month ago

README.md

MusicalInstrument.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace WpfApp3.Class
{
    [DataContract]
    public class MusicalInstrument
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Type { get; set; }
        [DataMember]
        public string Brand { get; set; }
        [DataMember]
        public double Price { get; set; }

        [DataMember(Name = "deliveryDate")]
        private string? stringDate { get; set; }

        [IgnoreDataMember]
        public DateTime? deliveryDate
        {
            get
            {
                return stringDate == null ? null : DateTime.Parse(stringDate);
            }
            set
            {
                stringDate = value.ToString();
            }
        }
    }
}

Manager.cs

using System.Collections.Generic;
using WpfApp3.Class;
using System.IO;
using System.Linq;
using System.Globalization;
using System.Runtime.Serialization.Json;
namespace WpfApp3
{
        public class JSONDataProvider
        {
            private List<MusicalInstrument> _instruments;
            public JSONDataProvider()
            {
                var serializer = new DataContractJsonSerializer(typeof(MusicalInstrument[]));
                using (var sr = new StreamReader("data.json"))
                {
                    _instruments = ((MusicalInstrument[])serializer.ReadObject(sr.BaseStream)).ToList();
                }
            }
            public List<MusicalInstrument> GetInstruments()
            {
                return _instruments;
            }
        }
}

Filter.cs

using System.Collections.Generic;
using System.Linq;
using WpfApp3.Class;

namespace WpfApp2
{
    class InstrumentFilter
    {
        public List<MusicalInstrument> FilterByType(List<MusicalInstrument> instruments, string type)
        {
            return instruments.Where(i => i.Type == type).ToList();
        }

        public List<MusicalInstrument> FilterAll(List<MusicalInstrument> instruments)
        {
            return instruments;
        }
    }
}

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WpfApp2;
using WpfApp3.Class;

namespace WpfApp3
{
    public partial class MainWindow : Window
    {
        private JSONDataProvider dataProvider;
        private InstrumentFilter filter;
        private string searchFilter = "";
        private bool sortAsc = true;

        public MainWindow()
        {
            InitializeComponent();

            dataProvider = new JSONDataProvider();
            filter = new InstrumentFilter();

            FilterComboBox.Items.Add("Все");
            FilterComboBox.Items.Add("Клавишные");
            FilterComboBox.Items.Add("Струнные");
            FilterComboBox.Items.Add("Ударные");
            FilterComboBox.SelectedIndex = 0;

            UpdateInstrumentList(dataProvider.GetInstruments());
        }
        private void FilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selectedFilter = (string)FilterComboBox.SelectedItem;

            if (selectedFilter == "Все")
            {
                UpdateInstrumentList(filter.FilterAll(dataProvider.GetInstruments()));
            }
            else
            {
                UpdateInstrumentList(filter.FilterByType(dataProvider.GetInstruments(), selectedFilter));
            }
        }
        private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
        {
            searchFilter = SearchFilterTextBox.Text;
            UpdateInstrumentList(dataProvider.GetInstruments());
        }

        private void RadioButtonAsc_Checked(object sender, RoutedEventArgs e)
        {
            sortAsc = true;
            UpdateInstrumentList(dataProvider.GetInstruments());
        }
        private void RadioButtonDesc_Checked(object sender, RoutedEventArgs e)
        {
            sortAsc = false;
            UpdateInstrumentList(dataProvider.GetInstruments());
        }
        private void UpdateInstrumentList(List<MusicalInstrument> instrumentsToDisplay)
        {
            if (!string.IsNullOrEmpty(searchFilter))
            {
                instrumentsToDisplay = instrumentsToDisplay.Where(i => i.Name.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                i.Type.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                i.Brand.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0).ToList();
            }

            if (sortAsc)
            {
                instrumentsToDisplay = instrumentsToDisplay.OrderBy(i => i.Price).ToList();
            }
            else
            {
                instrumentsToDisplay = instrumentsToDisplay.OrderByDescending(i => i.Price).ToList();
            }

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

    }
}

MainWindow.xaml

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="Musical Store" Height="450" Width="800">
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <Image Source="./assets/Guitar.jpg" Grid.Column="0" Grid.RowSpan="3" HorizontalAlignment="Right"/>

        <StackPanel Grid.Column="1" Orientation="Horizontal" Margin="10">
            <Label Content="Фильтр:" VerticalAlignment="Center" />
            <ComboBox x:Name="FilterComboBox" SelectionChanged="FilterComboBox_SelectionChanged" Margin="10" />
            <Label Content="Поиск:" VerticalAlignment="Center" />
            <TextBox Width="200" VerticalAlignment="Center" x:Name="SearchFilterTextBox" KeyUp="SearchFilter_KeyUp"/>
        </StackPanel>

        <StackPanel Grid.Column="1" Orientation="Vertical" Grid.Row="2" VerticalAlignment="Bottom">
            <RadioButton GroupName="Sort" Tag="1" Content="по возрастанию" Checked="RadioButtonAsc_Checked" VerticalAlignment="Center"/>
            <RadioButton GroupName="Sort" Tag="2" Content="по убыванию" Checked="RadioButtonDesc_Checked" VerticalAlignment="Center"/>
            <Button x:Name="ExitButton" Content="Выход" Click="ExitButton_Click" Height="50"/>
        </StackPanel>

        <DataGrid x:Name="InstrumentListView"
    Grid.Row="1"
    Grid.Column="1"
    CanUserAddRows="False"
    AutoGenerateColumns="False"
    ItemsSource="{Binding InstrumentListView}">
            <DataGrid.Columns>
                <DataGridTextColumn
            Header="Название"
            Binding="{Binding Name}"/>
                <DataGridTextColumn
            Header="Тип"
            Binding="{Binding Type}"/>
                <DataGridTextColumn
            Header="Бренд"
            Binding="{Binding Brand}"/>
                <DataGridTextColumn
            Header="Цена"
            Binding="{Binding Price}"/>
                <DataGridTextColumn
            Header="Дата поступления"
            Binding="{Binding deliveryDate, StringFormat={}{0:dd.MM.yyyy}}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>