the .csv file: School Year Won Loss Tie Alabama 1995 3 0 0 Alabama 1996 10 3 0 A
ID: 3916970 • Letter: T
Question
the .csv file:
School Year Won Loss Tie Alabama 1995 3 0 0 Alabama 1996 10 3 0 Alabama 1997 7 4 0 Alabama 1998 4 7 0 Alabama 1999 7 5 0 Alabama 2000 10 3 0 Alabama 2001 3 8 0 Alabama 2002 7 5 0 Alabama 2003 10 3 0 Alabama 2004 6 6 0 Alabama 2005 10 1 0 Alabama 2006 6 7 0 Alabama 2007 2 6 0 Alabama 2008 12 2 0 Alabama 2009 14 0 0 Alabama 2010 10 3 0 Alabama 2011 12 1 0 Alabama 2012 13 1 0 Alabama 2013 11 2 0 Alabama 2014 12 2 0 Alabama 2015 14 1 0 Alabama 2016 14 1 0 Auburn 1995 8 4 0 Auburn 1996 8 4 0 Auburn 1997 10 3 0 Auburn 1998 3 8 0 Auburn 1999 5 6 0 Auburn 2000 9 4 0 Auburn 2001 7 5 0 Auburn 2002 9 4 0 Auburn 2003 8 5 0 Auburn 2004 13 0 0 Auburn 2005 9 3 0 Auburn 2006 11 2 0 Auburn 2007 9 4 0 Auburn 2008 5 7 0 Auburn 2009 8 5 0 Auburn 2010 14 0 0 Auburn 2011 8 5 0 Auburn 2012 3 9 0 Auburn 2013 12 2 0 Auburn 2014 8 5 0 Auburn 2015 7 6 0 Auburn 2016 8 5 0Explanation / Answer
// You have to create a WPF application
// It will Create MainWindow.xaml and MainWindow.xaml.cs automatically
// MainWindow.xaml.cs constructor is start point of application by default
// Then you have to create 2 classes in the project
// Class Names are : Record.cs & GroupedRecord.cs
// In MainWindow.xaml.cs, I have created a variable name _FilePath
// this is csv file path and hardcoded to E drive, you have to update it to path of csv file you keep at.
// Please find code in bold
// 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"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button Content="Exit"
Margin="5" Padding="5"
Width="100" Height="40"
Click="Exit_Click"/>
<Button Content="Compare"
Margin="5" Padding="5"
Width="100" Height="40"
Click="Compare_Click"/>
</StackPanel>
<ListView Name="MainListView" Grid.Row="1">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="BorderThickness" Value="1" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
<GridViewColumn Header="Year" Width="100"
DisplayMemberBinding="{Binding Year}"/>
<GridViewColumn Header="Alabama" Width="100"
DisplayMemberBinding="{Binding Alabama}"/>
<GridViewColumn Header="Auburn" Width="100"
DisplayMemberBinding="{Binding Auburn}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
// MainWindow.xaml.cs
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Collections.Generic;
namespace WpfApp1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string _FilePath = @"E:Test.csv";
List<Record> _Records;
List<GroupedRecord> _GroupedRecords;
public MainWindow()
{
InitializeComponent();
Initialize();
}
private void Initialize()
{
try
{
_Records = new List<Record>();
_GroupedRecords = new List<GroupedRecord>();
// Load data from csv file
LoadFromFile(_FilePath);
// Group records by year
var groupedRecords = _Records.GroupBy(r => r.Year);
foreach(var rec in groupedRecords)
{
var gRecord = new GroupedRecord();
gRecord.Year = rec.Key;
gRecord.Alabama = rec.First(r => r.School == "Alabama").Result;
gRecord.Auburn = rec.First(r => r.School == "Auburn").Result;
_GroupedRecords.Add(gRecord);
}
MainListView.ItemsSource = _GroupedRecords;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Initialize", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void LoadFromFile(string filePath)
{
try
{
var data = File.ReadAllLines(filePath);
for (int index = 0; index < data.Length; index++)
{
if (index > 0) // Skip header in file
{
var values = data[index].Split(',');
Record record = new Record();
record.School = values[0];
record.Year = Convert.ToInt32(values[1]);
record.Won = Convert.ToInt32(values[2]);
record.Loss = Convert.ToInt32(values[3]);
record.Tie = Convert.ToInt32(values[4]);
_Records.Add(record);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "LoadFromFile", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Exit_Click(object sender, RoutedEventArgs e)
{
try
{
Application.Current.Shutdown();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exit", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void Compare_Click(object sender, RoutedEventArgs e)
{
try
{
var selectedItem = MainListView.SelectedItem;
if(selectedItem != null)
{
var item = selectedItem as GroupedRecord;
var alab = item.Alabama.Split('-');
var aubu = item.Auburn.Split('-');
if(Convert.ToInt32(alab[0]) > Convert.ToInt32(aubu[0]))
{
MessageBox.Show($"Alabama was better in year {item.Year}", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
}
else if (Convert.ToInt32(alab[0]) < Convert.ToInt32(aubu[0]))
{
MessageBox.Show($"Auburn was better in year {item.Year}", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
}
else if (Convert.ToInt32(alab[1]) < Convert.ToInt32(aubu[1]))
{
MessageBox.Show($"Alabama was better in year {item.Year}", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
}
else if (Convert.ToInt32(alab[1]) > Convert.ToInt32(aubu[1]))
{
MessageBox.Show($"Auburn was better in year {item.Year}", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Compare", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
// Record.cs
namespace WpfApp1
{
public class Record
{
public string School { get; set; }
public int Year { get; set; }
public int Won { get; set; }
public int Loss { get; set; }
public int Tie { get; set; }
public string Result => $"{Won}-{Loss}-{Tie}";
}
}
// GroupedRecord.cs
namespace WpfApp1
{
public class GroupedRecord
{
public int Year { get; set; }
public string Alabama { get; set; }
public string Auburn { get; set; }
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.