Valera 6fdb96c626 first commit | 7 months ago | |
---|---|---|
img | 7 months ago | |
.gitignore.txt | 7 months ago | |
readme.md | 7 months ago |
"Получение данных из внешних источников.CSV."
C#
Class1
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp1.model
{
public class cars
{
public string name { get; set; }
public int year { get; set; }
public double price { get; set; }
public string color { get; set; }
public bool defects { get; set; }
public DateOnly dateOfLastSTO { get; set; }
public string photo { get; set; }
}
}
Class2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp1.model
{
class Globals
{
public static IDataProvider dataProvider;
IEnumerable<Namecar> getNamecar()
{
return new Namecar[] {
new Namecar { title = "Toyota Trueno AE8"},
new Namecar { title = "Toyota Supra A80"},
new Namecar { title = "Nissal Skyline R34" },
new Namecar { title = "Nissan Silvia S15"},
new Namecar { title = "Toyota Camry 3.5"},
new Namecar { title = "Audi RS 6" },
new Namecar { title = "Трактор LOVOL TE354 HT"},
new Namecar { title = "BMW M5 F90"},
new Namecar { title = "BMW E36" },
new Namecar { title = "Daewoo Matiz" }
};
}
IEnumerable<Carsprice> getCarsprice()
{
return new Carsprice[] {
new Carsprice{title="Малая цена", priceFrom=0, priceTo=15001},
new Carsprice{title="Средняя цена", priceFrom = 20000, priceTo=35000},
new Carsprice{title="Высокая цена", priceFrom = 35001, priceTo=70000},
};
}
}
}
Class3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp1.model
{
public class Carsprice
{
public string title { get; set; }
public int priceFrom { get; set; }
public int priceTo { get; set; }
}
public class Namecar
{
public string title { get; set; }
}
}
Class4
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CsvHelper;
namespace WpfApp1.model
{
class Class4
{
public class CSVDataProvider : LocalDataProvider, IDataProvider
{
private IEnumerable<cars> carList;
public CSVDataProvider()
{
using (var reader = new StreamReader("./data.csv"))
{
using (var csv = new CsvReader(
reader,
CultureInfo.InvariantCulture))
{
carList = csv.GetRecords<cars>().ToList();
}
}
}
public IEnumerable<cars>getCars()
{
return carList;
}
}
}
}
data.csv
name,year,price,color,defects,dateOfLastSTO,photo,
Toyota Trueno AE8,1995,15000,white,false,4/30/2024,,
Toyota Supra A80,1996,30000,blue,false,5/1/2024,,
Nissal Skyline R34,1996,25000,red,true,5/2/2024,,
Nissan Silvia S15,1999,20000,green,false,5/3/2024,,
Toyota Camry 3.5,2020,20000,black,true,5/4/2024,,
Audi RS 6,2016,30000,purple,false,5/5/2024,,
Трактор LOVOL TE354 HT,2024,66666,red,false,5/6/2024,,
BMW M5 F90,2019,30000,white,true,5/7/2024,,
BMW E36,2006,35000,green,true,5/8/2024,,
Daewoo Matiz,2010,20000,blue,false,5/9/2024,,
MainWindow.xaml.cs
using CsvHelper;
using System;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WpfApp1.model;
using static WpfApp1.model.Class4;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string searchFilter = "";
private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
{
searchFilter = SearchFilterTextBox.Text;
Invalidate();
}
private bool sortAsc = true;
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
sortAsc = (sender as RadioButton).Tag.ToString() == "1";
Invalidate();
}
public event PropertyChangedEventHandler PropertyChanged;
private void Invalidate()
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("CarList"));
}
public string selectedname = "Все автомобили";
public Carsprice? selectedprice = null;
private IEnumerable<cars> _CarList;
public IEnumerable<cars> CarList
{
get
{
var res = _CarList;
res = res
.Where(c => (c.name == selectedname || selectedname == "Все автомобили"))
.Where(c => (selectedprice == null || (c.price >= selectedprice.priceFrom && c.price < selectedprice.priceTo)));
if (searchFilter != "")
res = res.Where(c => c.name.IndexOf(
searchFilter,
StringComparison.OrdinalIgnoreCase) >= 0);
if (sortAsc) res = res.OrderBy(c => c.year);
else res = res.OrderByDescending(c => c.year);
return res;
}
set
{
_CarList = value;
}
}
public List<cars> Cars { get; set; }
public List<Namecar> CarinfoList { get; set; }
public List<Carsprice> CarpriceList { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
Globals.dataProvider = new CSVDataProvider();
CarList = Globals.dataProvider.getCars();
CarinfoList = Globals.dataProvider.getName().ToList();
CarinfoList.Insert(0, new Namecar { title = "Все автомобили" });
CarpriceList = Globals.dataProvider.getPrice().ToList();
}
private void ExitButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void carnameFilterComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
selectedname = (carnameFilterComboBox.SelectedItem as Namecar).title;
Invalidate();
}
private void carpriceFilterComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
selectedprice = carpriceFilterComboBox.SelectedItem as Carsprice;
Invalidate();
}
}
interface IDataProvider
{
IEnumerable<cars> getCars();
IEnumerable<Namecar> getName();
IEnumerable<Carsprice> getPrice();
}
public class LocalDataProvider : IDataProvider
{
public IEnumerable<Namecar> getName()
{
return new Namecar[]
{
new Namecar()
{
title="Toyota Trueno AE86"
},
new Namecar()
{
title="Toyota Supra A80"
},
new Namecar()
{
title="Nissal Skyline R34"
},
new Namecar()
{
title="Nissan Silvia S15"
},
new Namecar()
{
title="Toyota Camry 3.5"
},
new Namecar()
{
title="Audi RS 6"
},
new Namecar()
{
title="Трактор LOVOL TE354 HT"
},
new Namecar()
{
title="BMW M5 F90"
},
new Namecar()
{
title="BMW E36"
},
new Namecar()
{
title="Daewoo Matiz"
}
};
}
public IEnumerable<Carsprice> getPrice()
{
return new Carsprice[]
{
new Carsprice()
{
title="Все цены",
priceFrom=0,
priceTo=70000
},
new Carsprice()
{
title="Малая цена",
priceFrom=0,
priceTo=15001
},
new Carsprice()
{
title="Средняя цена",
priceFrom=20000,
priceTo=35000
},
new Carsprice()
{
title="Высокая цена",
priceFrom=35001,
priceTo=70000
}
};
}
public IEnumerable<cars> getCars()
{
return new cars[] {
new cars {
price = 15000,
name = "Toyota Trueno AE86",
year = 1995,
},
new cars {
price = 30000,
name = "Toyota Supra A80",
year = 1996,
},
new cars {
price = 25000,
name = "Nissal Skyline R34",
year = 1996
},
new cars {
price = 20000,
name = "Nissan Silvia S15",
year = 1999,
},
new cars {
price = 20000,
name = "Toyota Camry 3.5",
year = 2020,
},
new cars {
price = 30000,
name = "Audi RS 6",
year = 2016,
},
new cars {
price = 66666,
name = "Трактор LOVOL TE354 HT",
year = 2024,
},
new cars {
price = 30000,
name = "BMW M5 F90",
year = 2019,
},
new cars {
price = 35000,
name = "BMW E36",
year = 2006,
},
new cars {
price = 20000,
name = "Daewoo Matiz",
year = 2010,
},
};
}
}
}
MainWindow.xaml
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d"
Title="Иванов Валера И-21" Height="450" Width="908">
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition />
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- лого -->
<Image
Source="./img/da.jpg"
Grid.RowSpan="2" HorizontalAlignment="Right"/>
<DataGrid
Grid.Row="1"
Grid.Column="1"
CanUserAddRows="False"
AutoGenerateColumns="False"
ItemsSource="{Binding CarList}">
<DataGrid.Columns>
<DataGridTextColumn
Header="Название"
Binding="{Binding name}"/>
<DataGridTextColumn
Header="Цена"
Binding="{Binding price}"/>
<DataGridTextColumn
Header="Год"
Binding="{Binding year}"/>
<DataGridTextColumn
Header="Дефекты"
Binding="{Binding defects}"/>
<DataGridTextColumn
Header="Дата последнего СТО"
Binding="{Binding dateOfLastSTO}"/>
</DataGrid.Columns>
</DataGrid>
<StackPanel
Orientation="Vertical"
Grid.RowSpan="3"
VerticalAlignment="Bottom">
<Button
x:Name="ExitButton"
Content="Выход"
Click="ExitButton_Click"
Height="50"/>
</StackPanel>
<WrapPanel
Orientation="Horizontal"
Grid.Column="1"
MinHeight="50">
<Label
Content="Поиск"
VerticalAlignment="Center"/>
<TextBox
Width="200"
VerticalAlignment="Center"
x:Name="SearchFilterTextBox"
KeyUp="SearchFilter_KeyUp"/>
<Label
Content="Название:"
VerticalAlignment="Center"/>
<ComboBox
Name="carnameFilterComboBox"
SelectionChanged="carnameFilterComboBox_SelectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding CarinfoList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label
Content="Цена:"
VerticalAlignment="Center"/>
<ComboBox
Name="carpriceFilterComboBox"
SelectionChanged="carpriceFilterComboBox_SelectionChanged"
VerticalAlignment="Center"
MinWidth="100"
SelectedIndex="0"
ItemsSource="{Binding CarpriceList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<Label
Content="{Binding title}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<Label
Content="Возраст:"
VerticalAlignment="Center"/>
<RadioButton
GroupName="Age"
Tag="1"
Content="по возрастанию"
IsChecked="True"
Checked="RadioButton_Checked"
VerticalAlignment="Center"/>
<RadioButton
GroupName="Age"
Tag="2"
Content="по убыванию"
Checked="RadioButton_Checked"
VerticalAlignment="Center"/>
</WrapPanel>
</Grid>
</Window>
Вот что получилось