vendredi 28 septembre 2018

GPA that updates when an item in the Listbox is Removed

I've created a listbox that displays a checked grade and selected credit plus is respective GPA. Each row of listbox items calculates the cumulative GPA. For instance, the listbox shows:

  1. A - 1 4.000
  2. C - 1 3.000
  3. D - 3 1.800

If I remove the last item, the cumulative GPA will be 3.000. However, if I remove the 2nd item, the GPA should be 2.500. However, it isn't how it should be when I tested my code.

These are the codes that I used:

//global declarations
List<double> allGrades = new List<double>();
        List<double> allHours = new List<double>();
        List<double> allGPA = new List<double>();
        List<int> gradeValue = new List<int>();
        private RadioButton currentRadioButton;
        Dictionary<char, double> gradeValuesMap;

        public string StudentName { get; set; }

 public DataEntry()//switch back to pass value: string input
        {
            InitializeComponent();
            gradeValuesMap = new Dictionary<char, double>();
            gradeValuesMap.Add('A', 4.0);
            gradeValuesMap.Add('B', 3.0);
            gradeValuesMap.Add('C', 2.0);
            gradeValuesMap.Add('D', 1.0);
            gradeValuesMap.Add('F', 0.0);            
        }


    enter code here

   private void btnEnter_Click(object sender, EventArgs e)
            {
                /*if grades and credits are not selected, Error Message:
                    * MessageBox.Show("A grade must be selected.");
                    * MessageBox.Show("Credit hours must be selected.");
                */
                if (!(radA.Checked || radB.Checked || radC.Checked ||
                    radD.Checked || radF.Checked))
                {
                    MessageBox.Show("A grade must be selected.");
                }

                if (cboCreditHrs.SelectedIndex == -1)//if" no credit is selected
                {
                    MessageBox.Show("Credit hours must be selected.");
                }
                else
                {
                    // do gpa math here
                    // sum(grades * credits) / sum(credits)


              var grade = getGradeValue(currentRadioButton.Text);
              var credit = Convert.ToDouble(cboCreditHrs.SelectedItem.ToString());


                    // sum(4 * 4) / sum(4)
                    var result = (grade * credit) / credit; //GPA calculation

                    allGrades.Add(grade);
                    allHours.Add(credit);

                    double sum = 0;

                    for (int i = 0; i < allGrades.Count; i++)
                    {
                        sum += allGrades[i] * allHours[i];
                    }


                    result = (sum)/ allHours.Sum();

                    string listboxItem = string.Format("{0}-{1} {2}", currentRadioButton.Text, cboCreditHrs.SelectedItem, result.ToString("n3"));
                    lstDetail.Items.Add(listboxItem);

                    // uncheck the currently checked radio button
                    currentRadioButton.Checked = false;

                    // reset the combox selection
                    cboCreditHrs.SelectedIndex = -1;
                }
            }

To remove the selected item. I used the following Even Handler:



     private void btnRmvSelect_Click(object sender, EventArgs e)
        {
            ///*foreach (DataEntry i in lstDetail.*/SelectedItems)
                //lstDetail.Items.Remove(i);

            for (int i = 0; i < lstDetail.SelectedItems.Count; i++)
                lstDetailhttps://stackoverflow.com/questions/20277193/how-to-change-the-selecteditem-foreground-text-of-listbox-item.Items.Remove(lstDetail.SelectedItems[i]);

        }

Most likely, my code above is missing something. Thus why the expected result is incorrect. Can anyone correct my code?




Aucun commentaire:

Enregistrer un commentaire