Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. namespace Assignment_2_module_marks
  2. {
  3. public partial class Form1 : Form
  4. {
  5. public static List<string> studentInfo = new List<string>(); // Declaring a list structure and making it public so that it can be accessed across forms
  6.  
  7. public Form1()
  8. {
  9. InitializeComponent();
  10. StartPosition = FormStartPosition.CenterScreen;
  11. }
  12.  
  13. Stats myStats = new Stats(); // Calls the Stats class
  14.  
  15. private void Form1_Load(object sender, EventArgs e)
  16. {
  17. string lines, studentNumber, studentMark; // Declaring string variables
  18.  
  19. StreamReader inputFile = File.OpenText("COM122.csv"); // Declaring a streamReader called inputfile and setting it to the data file
  20.  
  21. while (!inputFile.EndOfStream) // While not at the end of the file execute the while loop
  22. {
  23. lines = inputFile.ReadLine(); // Read all the lines in the file and store it to the lines variable
  24. studentInfo = lines.Split(',').ToList(); // Split all the lines after the ',' and coverts it to a list
  25. studentNumber = studentInfo[0]; // Store the value of the first index of studentInfo to the variable studentNumber
  26. studentMark = studentInfo[1]; // Store the value of the second index of studentInfo to the variable studentMark
  27. lstMarks.Items.Add(studentNumber + " : " + studentMark); // Add the studentNumber and studentMark together and store it in the listbox
  28. }
  29. inputFile.Close(); // closes the file
  30. }
  31.  
  32. private void btnAddMark_Click(object sender, EventArgs e)
  33. {
  34. addRecord myAddRecordForm = new addRecord(); // Opens a form called addRecord
  35. if (myAddRecordForm.ShowDialog() == DialogResult.OK) // If statement will run if the dialogResult is set to OK in the addRecord form
  36. {
  37. studentInfo.Add(myAddRecordForm.StudentID); // Adds the student ID to the studentInfo List
  38. studentInfo.Add(myAddRecordForm.Mark); // Adds the Mark to the studentInfo List
  39. lstMarks.Items.Add(myAddRecordForm.StudentID + " : " + myAddRecordForm.Mark); // Adds the StudentID and Mark to the listbox
  40. }
  41. }
  42.  
  43. private void btnEditMark_Click(object sender, EventArgs e)
  44. {
  45. editMark myEditRecordForm = new editMark(); // Opens a form called editMark
  46. this.Hide(); // Hides the previous form
  47. myEditRecordForm.ShowDialog(); // Shows the form
  48. }
  49.  
  50. private void btnDeleteMark_Click(object sender, EventArgs e)
  51. {
  52. deleteRecord myDeleteRecordForm = new deleteRecord(); // Opens a form called deleteRecord
  53. this.Hide(); // Hides the previous form
  54. myDeleteRecordForm.ShowDialog(); // Shows the form
  55. }
  56.  
  57. private void btnMax_Click(object sender, EventArgs e)
  58. {
  59.  
  60.  
  61. }
  62.  
  63. private void btnMin_Click(object sender, EventArgs e)
  64. {
  65.  
  66. }
  67.  
  68. private void btnAverage_Click(object sender, EventArgs e)
  69. {
  70. double average;
  71.  
  72. average = myStats.AverageMark(studentInfo);
  73.  
  74. lblOutput.Text = average.ToString();
  75. }
  76.  
  77. private void btnSaveToFile_Click(object sender, EventArgs e)
  78. {
  79. StreamWriter outputFile = File.AppendText("MODULEMARKS.csv"); // Declaring a streamwriter and naming it outputfile and sets the output location
  80.  
  81. outputFile.WriteLine(); //Writes the average to the file
  82.  
  83. outputFile.Close(); // Closes the outputfile
  84.  
  85. MessageBox.Show("Student Information inserted successfully"); // Displays a message to the user
  86. }
  87.  
  88. private void btnExit_Click(object sender, EventArgs e)
  89. {
  90. this.Close(); // Closes the program
  91. }
  92. }
  93. }
  94.  
  95.  
  96.  
  97.  
  98. Class :
  99.  
  100. namespace Assignment_2_module_marks
  101. {
  102. class Stats
  103. {
  104. //public int maxMarks()
  105. //{
  106. // int Max;
  107.  
  108. // //return Max;
  109. //}
  110. //public int minMarks()
  111. //{
  112. // int Min;
  113.  
  114. // return Min;
  115. //}
  116. public double AverageMark(List<string> studentInfo)
  117. {
  118. int total = 0;
  119.  
  120. foreach(int x in studentInfo[1])
  121. {
  122. total += x;
  123. MessageBox.Show(x.ToString()); // outputs 55 and 57 for some reason
  124. }
  125. return total / studentInfo.Count;
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement