Skip to content

Commit

Permalink
Syareel Week 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Syareel-Sukeri committed Aug 13, 2024
1 parent efedcdb commit 962ffe1
Show file tree
Hide file tree
Showing 42 changed files with 6,733 additions and 0 deletions.
1 change: 1 addition & 0 deletions week4/Syareel/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vs/
175 changes: 175 additions & 0 deletions week4/Syareel/Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

152 changes: 152 additions & 0 deletions week4/Syareel/Form1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Syareel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeListViews();
}

private void InitializeListViews()
{
listViewTotalScores.Columns.Add("Name", 100);
listViewTotalScores.Columns.Add("Total Score", 100);
listViewTotalScores.View = View.Details;

listViewPassingStudents.Columns.Add("Name", 100);
listViewPassingStudents.View = View.Details;

listViewTopStudents.Columns.Add("Name", 100);
listViewTopStudents.Columns.Add("Total Score", 100);
listViewTopStudents.View = View.Details;

listViewCsv.View = View.Details;
}

private void buttonBrowse_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = openFileDialog.FileName;
var students = ReadCsv(filePath);

DisplayCsvContent(filePath);

var studentScores = students.Select(s => new
{
s.Name,
TotalScore = s.MathScore + s.EnglishScore + s.ProgrammingScore
}).ToList();

var passingStudents = students.Where(s => s.MathScore > 60 && s.EnglishScore > 60 && s.ProgrammingScore > 60)
.Select(s => s.Name)
.ToList();

var topStudents = studentScores.OrderByDescending(s => s.TotalScore)
.Take(3)
.ToList();

listViewTotalScores.Items.Clear();
foreach (var student in studentScores)
{
var item = new ListViewItem(student.Name);
item.SubItems.Add(student.TotalScore.ToString());
listViewTotalScores.Items.Add(item);
}

listViewPassingStudents.Items.Clear();
foreach (var student in passingStudents)
{
var item = new ListViewItem(student);
listViewPassingStudents.Items.Add(item);
}

listViewTopStudents.Items.Clear();
foreach (var student in topStudents)
{
var item = new ListViewItem(student.Name);
item.SubItems.Add(student.TotalScore.ToString());
listViewTopStudents.Items.Add(item);
}
}
}
}

private void buttonReset_Click(object sender, EventArgs e)
{
listViewTotalScores.Items.Clear();
listViewPassingStudents.Items.Clear();
listViewTopStudents.Items.Clear();
listViewCsv.Items.Clear();
listViewCsv.Columns.Clear();
}

private List<Student> ReadCsv(string filePath)
{
var students = new List<Student>();
var lines = File.ReadAllLines(filePath);

foreach (var line in lines.Skip(1))
{
var values = line.Split(',');
var student = new Student
{
StudentId = int.Parse(values[0]),
Name = values[1],
MathScore = int.Parse(values[2]),
EnglishScore = int.Parse(values[3]),
ProgrammingScore = int.Parse(values[4])
};
students.Add(student);
}

return students;
}

private void DisplayCsvContent(string filePath)
{
var lines = File.ReadAllLines(filePath);

if (lines.Length > 0)
{
var header = lines[0].Split(',');
foreach (var column in header)
{
listViewCsv.Columns.Add(column);
}

for (int i = 1; i < lines.Length; i++)
{
var values = lines[i].Split(',');
var item = new ListViewItem(values[0]);
for (int j = 1; j < values.Length; j++)
{
item.SubItems.Add(values[j]);
}
listViewCsv.Items.Add(item);
}
}
}
}

public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public int MathScore { get; set; }
public int EnglishScore { get; set; }
public int ProgrammingScore { get; set; }
}
}
Loading

0 comments on commit 962ffe1

Please sign in to comment.