Question: Develop an application that let the user maintain a list of student scores. However, youll use a text file to store the student data. I

Develop an application that let the user maintain a list of student scores. However, youll use a text file to store the student data.

I believe I have most of the code correct but I am having trouble when reading the input file it only reads the name and the first score and doesnt continue after the break '|' . I am still trying to work through the update form as well but my main concern right now is why it isn't reading and storing all the scores in the list. The code I have is as follows:

Student Class

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

namespace Student_Scores_Lab { public class Student {

private string name; private List scoreList;

public Student() { }

public Student(string name, List scoreList) { this.Name = name; this.ScoreList = scoreList; }

public string Name { get { return name; } set { name = value; } }

public List ScoreList { get { return scoreList; } set { scoreList = value; } }

public string GetDisplayText() { string total = ""; foreach (int score in scoreList) { total += "|" + score; } return name + total; } } }

StudentDB class

--------------------------------------------------------------

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO;

namespace Student_Scores_Lab { public static class StudentDB { private const string dir = @"C:\C# 2017\For Lab 4\"; private const string path = dir + "StudentScores.txt";

public static void SaveStudents(List students) { StreamWriter textOut = new StreamWriter( new FileStream(path, FileMode.Create, FileAccess.Write));

foreach (Student student in students) { string scoresList = "";

for (int i = 0; i < student.ScoreList.Count(); i++) { int value = student.ScoreList[i];

scoresList += (value + "|"); }

textOut.Write(student.Name + "|"); textOut.WriteLine(scoresList.TrimEnd('|')); } textOut.Close(); }

public static List GetStudents() { if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);

StreamReader textIn = new StreamReader( new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));

List students = new List();

while (textIn.Peek() != -1) { string row = textIn.ReadLine(); string[] columns = row.Split('|'); Student student = new Student();

List scoreList = new List();

if (columns[1] != "") { int[] scoreArray = columns[1].Split('|').Select(n => Convert.ToInt32(n)).ToArray();

scoreList = scoreArray.ToList(); student.ScoreList = scoreList; } else { student.ScoreList = new List(); }

student.Name = columns[0]; students.Add(student);

}

textIn.Close();

return students; }

public static void LoadSampleStudents() {

if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); }

if (!File.Exists(path)) {

StreamWriter textOut = new StreamWriter( new FileStream(path, FileMode.Create, FileAccess.Write));

textOut.WriteLine("Elle Hamilton|100|97|68"); textOut.WriteLine("Paul Wolf|95|76|87"); textOut.WriteLine("Hannah Berry|95|89|94");

textOut.Close(); } } } }

------------------------------------

Validator Class

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace Student_Scores_Lab { public static class Validator { public static string title = "Entry Error";

public static bool IsPresent(TextBox textBox, string name) { if (textBox.Text == String.Empty) { MessageBox.Show(name + " is a required field.", title); textBox.Focus(); return false; } return true; }

public static bool IsInt32(TextBox textBox, string name) { int number = 0; if (Int32.TryParse(textBox.Text, out number)) { return true; } else { MessageBox.Show(name + " must be an integer.", title); textBox.Focus(); return false; } }

public static bool IsWithinRange(TextBox textBox, string name, int min, int max) { int number = Convert.ToInt32(textBox.Text); if (number < min || number > max) { MessageBox.Show(name + " must be between " + min + " and " + max + ".", title); textBox.Focus(); return false; } return true; } } }

Student_Scores.cs - main form

--------------------

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

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

private List students = null;

private void Form1_Load(object sender, EventArgs e) { StudentDB.LoadSampleStudents(); students = StudentDB.GetStudents(); FillStudentListBox(); txtBoxTotal.Text = String.Empty; txtBoxCount.Text = String.Empty; txtBoxAverage.Text = String.Empty; }

private void FillStudentListBox() { lstStudents.Items.Clear(); foreach (Student s in students) { lstStudents.Items.Add(s.GetDisplayText()); } }

private void btnExit_Click_1(object sender, EventArgs e) { this.Close(); }

private void btnAddNew_Click(object sender, EventArgs e) { Add_New_Student addStudentForm = new Add_New_Student(); Student student = addStudentForm.GetNewStudent();

if (student != null) { students.Add(student); StudentDB.SaveStudents(students); FillStudentListBox(); int i = students.Count - 1; lstStudents.SetSelected(i, true); } }

/* private void btnUpdate_Click(object sender, EventArgs e) { int i = lstStudents.SelectedIndex; if (i != -1) { Student selectedStudent = students[i]; Update_Student_Scores updateStudentScoresForm = new Update_Student_Scores(selectedStudent); updateStudentScoresForm.GetUpdatedStudent(); StudentDB.SaveStudents(students); FillStudentListBox(); lstStudents.SetSelected(i, true); } } */

private void btnDelete_Click(object sender, EventArgs e) { int i = lstStudents.SelectedIndex;

if (i != -1) { Student student = students[i]; students.Remove(student); StudentDB.SaveStudents(students); FillStudentListBox(); txtBoxTotal.Text = String.Empty; txtBoxCount.Text = String.Empty; txtBoxAverage.Text = String.Empty; } }

private void lstStudents_SelectedIndexChanged(object sender, EventArgs e) { Calculations(); }

private void Calculations() { int i = lstStudents.SelectedIndex;

if (i != -1) { Student selectedStudent = students[i]; if (selectedStudent.ScoreList.Count() == 0) { txtBoxTotal.Text = "n/a"; txtBoxCount.Text = "0"; txtBoxAverage.Text = "n/a"; } else { int total = 0; foreach (int score in selectedStudent.ScoreList) { total += score; } int count = selectedStudent.ScoreList.Count(); decimal average = Convert.ToDecimal(total) / Convert.ToDecimal(count);

txtBoxTotal.Text = Convert.ToString(total); txtBoxCount.Text = Convert.ToString(count); txtBoxAverage.Text = average.ToString("#.##");

} } }

}//end form class }

-------------

Add_New_Student.cs

-------------------------------

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace Student_Scores_Lab { public partial class Add_New_Student : Form { public Add_New_Student() { InitializeComponent(); }

private Student student = null;

public Student GetNewStudent() { this.ShowDialog(); return student; }

private void btnOk_Click(object sender, EventArgs e) { if (Validator.IsPresent(txtBoxName, "Name")) { student = new Student(txtBoxName.Text, tempList); this.Close(); } }

private void btnCancel_Click(object sender, EventArgs e) { this.Close(); }

List tempList = new List();

private void btnAddScore_Click(object sender, EventArgs e) { if (IsValidData()) { tempList.Add(Convert.ToInt32(txtBoxScore.Text)); string total = ""; foreach (int scores in tempList) { total += Convert.ToString(scores) + " "; } lblScores.Text = total; } }

private void Add_New_Student_Load(object sender, EventArgs e) {

}

private void btnClear_Click(object sender, EventArgs e) { tempList.Clear(); txtBoxScores.Text = String.Empty; }

private bool IsValidData() { return Validator.IsPresent(txtBoxScores, "Score") && Validator.IsInt32(txtBoxScores, "Score") && Validator.IsWithinRange(txtBoxScores, "Score", 0, 100); }

private void btnAddScore_Click_1(object sender, EventArgs e) {

} } }

Add_Score.cs

-------------------

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace Student_Scores_Lab { public partial class Add_Score : Form { private int score = -1;

public Add_Score() { InitializeComponent(); }

private void Add_Score_Load(object sender, EventArgs e) {

}

private void btnAdd_Click(object sender, EventArgs e) { if (IsValidData()) { score = Convert.ToInt32(txtBoxScore.Text); this.Close(); } }

private void btnCancel_Click(object sender, EventArgs e) { this.Close(); }

public int GetNewScore() { this.ShowDialog(); return score; }

private bool IsValidData() { return Validator.IsPresent(txtBoxScore, "Score") && Validator.IsInt32(txtBoxScore, "Score") && Validator.IsWithinRange(txtBoxScore, "Score", 0, 100); }

} }

Any help you can provide would be much appreciated.

This is my second post on the same question because last answer was Java and didn't seem to help. Again it needs to be C#. My main issue is that when the form loads/writes it only lets 1 of the scores... I need it to read how many ever scores that person has... and seperated by '|'

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!