Answer on Question #42798, Programming, C#
Problem:
Write an application that retrieves a student name and three scores per line from a text file. Process the values by calculating the average of the scores per student. Write the name and average to a different text file. Test your application with a minimum of eight records in the original file. Open and Save FileDialog. Visual Studio Express 2012 Desktop.
Answer:
I created a Windows Form Application on Visual Studio Express 2012 Desktop.
There we have only one form with two buttons: one for load data from file (OpenFileDialog) and other to save data to text file (SaveFileDialog).
When we load data from input file they must be in next format:
- Bodeychuck|5|4|3
- Rotar|3|4|4
- Tsalin|4,5|3|4
- Slobodyan|5|5|5
- Shevaha|2|3|4
- Yashan|5|6,7|5
- Yurchushun|4|5|3
- Yuzva|3|3|23,1
else we would have a message: “Not correct data in input file”. If all good we can write result to file. For our example we will have:
- Bodeychuck|4,000
- Rotar|3,667
- Tsalin|3,833
- Slobodyan|5,000
- Shevaha|3,000
- Yashan|5,567
- Yurchushun|4,000
- Yuzva|9,700
Code on c#:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace Student {
public partial class Form1 : Form {
//Class for Student as object
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();
}
// Parse data to objects
private bool ParseData(string str) {
bool isGood = true;
try {
studentList = new List<Student>();
var items = str.Split('\n');
if (items != null && items.Any()) {
for (int i = 0; i < items.Length; i++) {
Student st = new Student();
var stSplit = items[i].Split('|', '\r');
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;
}
// Load data from file to inputTextData
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();
}
}
// Save data to file
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();
}
}
}
}