Write an application that retrieves both string data and num

Write an application that retrieves both string data and numbers from a text file. Test your solution by retrieveing names of students and three scores per linen from a text file. Process the values by calculating the average scores per student. Write the name and average to a different text file. Test your application with a mininum of eight records in the original file. Hint: You might consider adding delimiters between the data values in the original text file to simplify retrieving and processing the data. Include appropriate exception-handling techniques in your solution. When the application closes, locate the text file and verify its contenets. This is for C# using visual studio,

Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace Student
{
public partial class Form1 : Form
{
private class Student
{
public string Name
{
get;
set;
}
public double FirstBal
{
get;
set;
}
public double SecondBal
{
get;
set;
}
public double ThirdBal
{
get;
set;
}
}
private List<Student> studentList;
public Form1()
{
InitializeComponent();
}
private bool ParseData(string str)
{
bool isGood = true;
try
{
studentList = new List<Student>();
var items = str.Split(\'\ \');
if (items != null && items.Any())
{
for (int i = 0; i < items.Length; i++)
{
Student st = new Student();
var stSplit = items[i].Split(\'|\', \'\ \');
st.Name = stSplit[0];
st.FirstBal = double.Parse(stSplit[1]);
st.SecondBal = double.Parse(stSplit[2]);
st.ThirdBal = double.Parse(stSplit[3]);
studentList.Add(st);
}
}
}
catch
{
isGood = false;
}
finally
{
if (!isGood)
{
studentList = null;
}
}
return isGood;
}

private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
var inputTextData = sr.ReadToEnd();
if (!ParseData(inputTextData))
MessageBox.Show(\"Not correct data in input file\");
sr.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = \"txt files (*.txt)|*.txt|All files (*.*)|*.*\";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter writer = new StreamWriter(saveFileDialog1.OpenFile());
if (studentList != null && studentList.Any())
{
string str;
foreach (var item in studentList)
{
str = item.Name + \'|\' + ((item.FirstBal + item.SecondBal +item.ThirdBal) / 3.0).ToString(\"N3\");
writer.WriteLine(str);
}
}
writer.Close();
writer.Dispose();
}
}
}
}

Write an application that retrieves both string data and numbers from a text file. Test your solution by retrieveing names of students and three scores per line
Write an application that retrieves both string data and numbers from a text file. Test your solution by retrieveing names of students and three scores per line

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site