Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Assignment_2_module_marks
- {
- public partial class Form1 : Form
- {
- public static List<string> studentInfo = new List<string>(); // Declaring a list structure and making it public so that it can be accessed across forms
- public Form1()
- {
- InitializeComponent();
- StartPosition = FormStartPosition.CenterScreen;
- }
- Stats myStats = new Stats(); // Calls the Stats class
- private void Form1_Load(object sender, EventArgs e)
- {
- string lines, studentNumber, studentMark; // Declaring string variables
- StreamReader inputFile = File.OpenText("COM122.csv"); // Declaring a streamReader called inputfile and setting it to the data file
- while (!inputFile.EndOfStream) // While not at the end of the file execute the while loop
- {
- lines = inputFile.ReadLine(); // Read all the lines in the file and store it to the lines variable
- studentInfo = lines.Split(',').ToList(); // Split all the lines after the ',' and coverts it to a list
- studentNumber = studentInfo[0]; // Store the value of the first index of studentInfo to the variable studentNumber
- studentMark = studentInfo[1]; // Store the value of the second index of studentInfo to the variable studentMark
- lstMarks.Items.Add(studentNumber + " : " + studentMark); // Add the studentNumber and studentMark together and store it in the listbox
- }
- inputFile.Close(); // closes the file
- }
- private void btnAddMark_Click(object sender, EventArgs e)
- {
- addRecord myAddRecordForm = new addRecord(); // Opens a form called addRecord
- if (myAddRecordForm.ShowDialog() == DialogResult.OK) // If statement will run if the dialogResult is set to OK in the addRecord form
- {
- studentInfo.Add(myAddRecordForm.StudentID); // Adds the student ID to the studentInfo List
- studentInfo.Add(myAddRecordForm.Mark); // Adds the Mark to the studentInfo List
- lstMarks.Items.Add(myAddRecordForm.StudentID + " : " + myAddRecordForm.Mark); // Adds the StudentID and Mark to the listbox
- }
- }
- private void btnEditMark_Click(object sender, EventArgs e)
- {
- editMark myEditRecordForm = new editMark(); // Opens a form called editMark
- this.Hide(); // Hides the previous form
- myEditRecordForm.ShowDialog(); // Shows the form
- }
- private void btnDeleteMark_Click(object sender, EventArgs e)
- {
- deleteRecord myDeleteRecordForm = new deleteRecord(); // Opens a form called deleteRecord
- this.Hide(); // Hides the previous form
- myDeleteRecordForm.ShowDialog(); // Shows the form
- }
- private void btnMax_Click(object sender, EventArgs e)
- {
- }
- private void btnMin_Click(object sender, EventArgs e)
- {
- }
- private void btnAverage_Click(object sender, EventArgs e)
- {
- double average;
- average = myStats.AverageMark(studentInfo);
- lblOutput.Text = average.ToString();
- }
- private void btnSaveToFile_Click(object sender, EventArgs e)
- {
- StreamWriter outputFile = File.AppendText("MODULEMARKS.csv"); // Declaring a streamwriter and naming it outputfile and sets the output location
- outputFile.WriteLine(); //Writes the average to the file
- outputFile.Close(); // Closes the outputfile
- MessageBox.Show("Student Information inserted successfully"); // Displays a message to the user
- }
- private void btnExit_Click(object sender, EventArgs e)
- {
- this.Close(); // Closes the program
- }
- }
- }
- Class :
- namespace Assignment_2_module_marks
- {
- class Stats
- {
- //public int maxMarks()
- //{
- // int Max;
- // //return Max;
- //}
- //public int minMarks()
- //{
- // int Min;
- // return Min;
- //}
- public double AverageMark(List<string> studentInfo)
- {
- int total = 0;
- foreach(int x in studentInfo[1])
- {
- total += x;
- MessageBox.Show(x.ToString()); // outputs 55 and 57 for some reason
- }
- return total / studentInfo.Count;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement