Skip to content

Commit

Permalink
add average calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
leolost2605 committed Nov 29, 2023
1 parent 0dbae3f commit 8367087
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 54 deletions.
37 changes: 0 additions & 37 deletions src/subject-manager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -75,46 +75,9 @@ public class SubjectManager : Object {
}
}



// public void new_grade (int index, string grade, string note, int c) {
// bool worked = false;

// for (int i = 0; i < subjects[index].grades.length; i++) {
// if (subjects[index].grades[i] == null) {
// subjects[index].grades[i] = new Grade (grade, note, c);
// i = subjects[index].grades.length;
// worked = true;
// }
// }


// if (worked == false) {
// print ("no more grades available");
// } else {
// window_grade_rows_ui (index);
// }
// }




public void new_subject (string name, Category[] c) {
bool worked = false;

subjects[name] = new Subject (name);
}

// public void delete_grade (int sub_index, int gra_index) {
// subjects[sub_index].grades[gra_index] = null;


// for (int i = gra_index; i < subjects[sub_index].grades.length - 1; i++) {
// subjects[sub_index].grades[i] = subjects[sub_index].grades[i + 1];
// }

// subjects[sub_index].grades[subjects[sub_index].grades.length - 1] = null;

// window_grade_rows_ui (sub_index);
// }
}
49 changes: 32 additions & 17 deletions src/subject-page.vala
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,31 @@ public class SubjectPage : Gtk.Box {
edit_subject_dialog ();
});

void calculate_average () {
if (subject.grades_model.get_n_items () == 0) {
avg_label.label = "0.00";
return;
}

var table = new HashTable<string, double?> (str_hash, str_equal);
var table2 = new HashTable<string, int?> (str_hash, str_equal);

for (int i = 0; i < subject.grades_model.get_n_items (); i++) {
var grade = (Grade) subject.grades_model.get_item (i);
table[grade.category_name] = table[grade.category_name] == null ? double.parse (grade.grade) : table[grade.category_name] + double.parse (grade.grade);
table2[grade.category_name] += table2[grade.category_name] == null ? 1 : table2[grade.category_name] + 1;
}

double percentage_divider = 0;
double avg = 0;
foreach (var cat in table.get_keys ()) {
avg += (table[cat] / table2[cat]) * subject.categories_by_name[cat].percentage;
percentage_divider += subject.categories_by_name[cat].percentage;
}

avg_label.label = "%.2f".printf (avg / percentage_divider);
}

subject.grades_model.items_changed.connect (() => {
if (subject.grades_model.get_n_items () > 0 && nyttbox.get_last_child () == status_page) {
nyttbox.remove (status_page);
Expand All @@ -127,21 +152,11 @@ public class SubjectPage : Gtk.Box {
nyttbox.remove (list_box);
nyttbox.append (status_page);
}
// double percentage_divider = 0;

// int i = 0;
// for (; i < subject.grades_model.get_n_items (), i++) {
// if (number_of_grades[j] != 0) {
// avg_calculated[j] = average[j] / number_of_grades[j];
// final_avg += avg_calculated[j] * subjects[i].categories[j].percentage;
// percentage_divider += subjects[i].categories[j].percentage;
// }
// }
// if (percentage_divider != 0) {
// string average_string = "%.2f".printf (final_avg / percentage_divider);
// avg[i].set_label (average_string);
// }

calculate_average ();
});

calculate_average ();
}

public void new_grade_dialog () {
Expand All @@ -161,7 +176,7 @@ public class SubjectPage : Gtk.Box {
new EditSubjectDialog ((Window) get_root (), subject).present ();
}

private static Gtk.Widget widget_create_func (Object obj) {
private Gtk.Widget widget_create_func (Object obj) {
var grade = (Grade) obj;

var delete_button = new Gtk.Button () {
Expand All @@ -182,7 +197,7 @@ public class SubjectPage : Gtk.Box {

delete_button.clicked.connect (() => {
Adw.MessageDialog msg = new Adw.MessageDialog (
null, //TODO
(Window) get_root (),
_("Delete Grade?"),
_("If you delete this grade, its information will be deleted permanently.")
);
Expand All @@ -192,7 +207,7 @@ public class SubjectPage : Gtk.Box {
msg.set_close_response ("cancel");
msg.response.connect ((response) => {
if (response == "delete") {
// delete_grade (delete_button.subject_index, delete_button.grade_index);
subject.delete_grade (grade);
}
msg.destroy ();
});
Expand Down
7 changes: 7 additions & 0 deletions src/subject.vala
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ public class Subject : Object{
public void new_grade (string grade, string note, string c) {
grades_model.insert (0, new Grade (grade, note, c));
}

public void delete_grade (Grade grade) {
uint pos;
if (grades_model.find (grade, out pos)) {
grades_model.remove (pos);
}
}
}

0 comments on commit 8367087

Please sign in to comment.