Browse Source

Добавить 'README.md'

ababin 1 month ago
parent
commit
324ef6480c
1 changed files with 255 additions and 0 deletions
  1. 255 0
      README.md

+ 255 - 0
README.md

@@ -0,0 +1,255 @@
+### MainWindow
+```
+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 InstrumentDataManager dataManager;
+        private InstrumentFilter filter;
+        private string searchFilter = "";
+        private bool sortAsc = true;
+
+        public MainWindow()
+        {
+            InitializeComponent();
+
+            dataManager = new InstrumentDataManager();
+            filter = new InstrumentFilter();
+
+            FilterComboBox.Items.Add("Все");
+            FilterComboBox.Items.Add("Клавишные");
+            FilterComboBox.Items.Add("Струнные");
+            FilterComboBox.Items.Add("Ударные");
+            FilterComboBox.SelectedIndex = 0;
+
+            UpdateInstrumentList(dataManager.GetInstruments());
+        }
+
+        private void FilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
+        {
+            string selectedFilter = (string)FilterComboBox.SelectedItem;
+
+            if (selectedFilter == "Все")
+            {
+                UpdateInstrumentList(filter.FilterAll(dataManager.GetInstruments()));
+            }
+            else
+            {
+                UpdateInstrumentList(filter.FilterByType(dataManager.GetInstruments(), selectedFilter));
+            }
+        }
+        private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
+        {
+            searchFilter = SearchFilterTextBox.Text;
+            UpdateInstrumentList(dataManager.GetInstruments());
+        }
+
+        private void RadioButtonAsc_Checked(object sender, RoutedEventArgs e)
+        {
+            sortAsc = true;
+            UpdateInstrumentList(dataManager.GetInstruments());
+        }
+        private void RadioButtonDesc_Checked(object sender, RoutedEventArgs e)
+        {
+            sortAsc = false;
+            UpdateInstrumentList(dataManager.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();
+        }
+
+    }
+}
+### Filter
+```
+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;
+        }
+    }
+}
+```
+### Manager
+```
+using System.Collections.Generic;
+using WpfApp3.Class;
+using System.IO;
+using CsvHelper;
+using CsvHelper.Configuration;
+using System.Linq;
+using System.Globalization;
+namespace WpfApp3
+{
+    class InstrumentDataManager
+    {
+        private List<MusicalInstrument> instruments;
+
+        public InstrumentDataManager()
+        {
+            instruments = new List<MusicalInstrument>
+            {
+                new MusicalInstrument { Name = "Гитара", Type = "Струнные", Price = 300 },
+                new MusicalInstrument { Name = "Барабаны", Type = "Ударные", Price = 500 },
+                new MusicalInstrument { Name = "Фортепиано", Type = "Клавишные", Price = 1000 },
+                new MusicalInstrument { Name = "Скрипка", Type = "Струнные", Price = 400 },
+                new MusicalInstrument { Name = "Труба", Type = "Медные", Price = 600 },
+            };
+            CsvConfiguration configuration = new CsvConfiguration(CultureInfo.InvariantCulture)
+            {
+                Delimiter = ",",
+                HasHeaderRecord = true,
+                IgnoreBlankLines = true
+            };
+
+            using (var reader = new StreamReader("./data.csv"))
+            using (var csv = new CsvReader(reader, configuration))
+            {
+                instruments = csv.GetRecords<MusicalInstrument>().ToList();
+            }
+        }
+
+        public List<MusicalInstrument> GetInstruments()
+        {
+            return instruments;
+        }
+    }
+}
+```
+### MusicalInsrument
+```
+using System.Collections.Generic;
+using WpfApp3.Class;
+using System.IO;
+using CsvHelper;
+using CsvHelper.Configuration;
+using System.Linq;
+using System.Globalization;
+namespace WpfApp3
+{
+    class InstrumentDataManager
+    {
+        private List<MusicalInstrument> instruments;
+
+        public InstrumentDataManager()
+        {
+            instruments = new List<MusicalInstrument>
+            {
+                new MusicalInstrument { Name = "Гитара", Type = "Струнные", Price = 300 },
+                new MusicalInstrument { Name = "Барабаны", Type = "Ударные", Price = 500 },
+                new MusicalInstrument { Name = "Фортепиано", Type = "Клавишные", Price = 1000 },
+                new MusicalInstrument { Name = "Скрипка", Type = "Струнные", Price = 400 },
+                new MusicalInstrument { Name = "Труба", Type = "Медные", Price = 600 },
+            };
+            CsvConfiguration configuration = new CsvConfiguration(CultureInfo.InvariantCulture)
+            {
+                Delimiter = ",",
+                HasHeaderRecord = true,
+                IgnoreBlankLines = true
+            };
+
+            using (var reader = new StreamReader("./data.csv"))
+            using (var csv = new CsvReader(reader, configuration))
+            {
+                instruments = csv.GetRecords<MusicalInstrument>().ToList();
+            }
+        }
+
+        public List<MusicalInstrument> GetInstruments()
+        {
+            return instruments;
+        }
+    }
+}
+```
+```
+<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>
+
+        <ListView x:Name="InstrumentListView" Grid.Column="1" Grid.Row="1" Margin="10">
+            <ListView.View>
+                <GridView>
+                    <GridViewColumn Header="Название" DisplayMemberBinding="{Binding Name}" />
+                    <GridViewColumn Header="Тип" DisplayMemberBinding="{Binding Type}" />
+                    <GridViewColumn Header="Бренд" DisplayMemberBinding="{Binding Brand}" />
+                    <GridViewColumn Header="Цена" DisplayMemberBinding="{Binding Price}" />
+                </GridView>
+            </ListView.View>
+        </ListView>
+    </Grid>
+</Window>
+```
+![](./img/изображение_2024-05-07_030422699.png)