public void TestEmptyTableWeight() { var calc = new AverageCalc(); var emptyTable = new SubjectDetailsViewModel[1]; var result = calc.WeightedAverage(emptyTable); }
public void TestWeightedMean() { List<SubjectDetailsViewModel> grades = new List<SubjectDetailsViewModel>(); var rnd = new Random(); var sum = 0.0; var num = 0.0; for (var i = 0; i < 5; i++) { var val = rnd.Next(2, 6); var weight = rnd.Next(1, 10); var grade = new SubjectDetailsViewModel() { grade_value = val, grade_weight = weight }; grades.Add(grade); sum += grade.grade_weight * grade.grade_value; num += grade.grade_weight; } var mean =Math.Round(sum/num, 2); var calc = new AverageCalc(); var result = calc.WeightedAverage(grades.ToArray()); Assert.AreEqual(mean, result); }
/// <summary> /// Calculates average from given grades. /// </summary> /// <param name="grades">Array of grades as SubjectDetailsViewModel.</param> /// <returns></returns> public double Average(SubjectDetailsViewModel[] grades) { double sum = 0; if (grades != null) { if (grades.Length != 0) { foreach (var grade in grades) { if(grade == null) throw new Exception("There is no grade! (null)"); sum = sum + grade.grade_value; } return Math.Round((sum/grades.Length),2); } } return 0; }
/// <summary> /// Calculates a weighted average from given grades. /// </summary> /// <param name="grades">Array of grades as SubjectDetailsViewModel.</param> /// <returns></returns> public double WeightedAverage(SubjectDetailsViewModel[] grades) { if (grades != null) { if (grades.Length != 0) { double sumTop = 0; double sumBot = 0; foreach (var grade in grades) { if (grade == null) throw new Exception("There is no grade! (null)"); sumTop = sumTop + (grade.grade_value*grade.grade_weight); sumBot = sumBot + (grade.grade_weight); } return Math.Round((sumTop/sumBot), 2); } } return 0; }
public async Task<IHttpActionResult> PutSubjectDetails(int id, SubjectDetailsViewModel subjectDetails) { SubjectDetails local= await db.SubjectDetails.FindAsync(id); local.grade_date = subjectDetails.grade_date; local.grade_desc = subjectDetails.grade_desc; local.grade_value = subjectDetails.grade_value; local.grade_weight = subjectDetails.grade_weight; local.user_id = subjectDetails.user_id; if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != subjectDetails.id) { return BadRequest(); } db.Entry(local).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SubjectDetailsExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostSubjectDetails(SubjectDetailsViewModel subjectDetails) { if (!ModelState.IsValid) { return BadRequest(ModelState); } SubjectDetails newSubjectDetail = new SubjectDetails() { grade_date = subjectDetails.grade_date, grade_desc = subjectDetails.grade_desc, grade_value = subjectDetails.grade_value, grade_weight = subjectDetails.grade_weight, sub_id = subjectDetails.sub_id, user_id = subjectDetails.user_id }; db.SubjectDetails.Add(newSubjectDetail); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = subjectDetails.id }, subjectDetails); }
/// <summary> /// Calls edit form for grade /// </summary> /// <param name="sub">Subject to which grade will be added</param> private void AddGradeToSubject(SubjectsViewModel sub) { SubjectDetailsViewModel grade = new SubjectDetailsViewModel() { sub_id = sub.id, grade_weight = 1 }; var editForm = new EditGradeForm(grade, true); editForm.ShowDialog(); UpdateMainForm(); }