Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace TeamLabAssignment
- {
- public partial class frmCGPACalculator : Form
- {
- const int MAX_COURSES = 40;
- const double GPA_90_100 = 5.0;
- const double GPA_85_89 = 4.5;
- const double GPA_80_84 = 4.0;
- const double GPA_75_79 = 3.5;
- const double GPA_70_74 = 3.0;
- const double GPA_65_69 = 2.5;
- const double GPA_60_64 = 2.0;
- const double GPA_55_59 = 1.5;
- const double GPA_50_54 = 1.0;
- const double GPA_BELOW_50 = 0;
- int arrayIndex = 0;
- //define the arrays in the class. cant be stored in click method, else data loss
- double[] coursesPercentages = new double[MAX_COURSES];
- double[] coursesGPA = new double[MAX_COURSES];
- public frmCGPACalculator()
- {
- InitializeComponent();
- }
- private void btnCalculate_Click(object sender, EventArgs e)
- {
- double coursePercentage;
- double courseHours;
- double hourTotal=0;
- double QualityPointValuesTotal=0;
- try
- {
- if (isValidData())
- {
- coursePercentage = Convert.ToDouble(txtPercent.Text);
- courseHours = Convert.ToDouble(txtHours.Text);
- hourTotal += courseHours; //add all the hours as user enters them
- QualityPointValuesTotal = QualityPointValues(courseHours, coursePercentage);
- //Totals the Quality Point Value as user enters grades
- //add the data to the array
- coursesPercentages[arrayIndex] = coursePercentage;
- coursesGPA[arrayIndex] = calculateGPA(coursePercentage);
- //coursesGPA[arrayIndex] = courseHours;
- lbGPAOutput.Items.Add(coursePercentage + "% - GPA: " + coursesGPA[arrayIndex]);
- //calculate
- txtHours.Clear();
- txtPercent.Clear();
- txtPercent.Focus();
- arrayIndex++;
- }
- }
- //not a number
- catch (FormatException myFormatEx)
- {
- MessageBox.Show(myFormatEx.Message + "\nInvalid data type entered. Please check all entries.", "Entry Error");
- }
- //too large for decimal
- catch (OverflowException myOverflowEx)
- {
- MessageBox.Show(myOverflowEx.Message + "\nOverflow error. Please enter smaller values.", "Entry Error");
- }
- //other exception
- catch (Exception myEx)
- {
- MessageBox.Show(myEx.Message + "\n\n" + myEx.GetType().ToString() + "\n" + myEx.StackTrace, "Exception");
- }
- //Print to CGPA lable after calling and convering method
- // lblCGPAOutput.Text= Convert.ToString(CGPACalculate(hourTotal, QualityPointValuesTotal));
- }
- private void btnClear_Click(object sender, EventArgs e)
- {
- txtHours.Clear();
- txtPercent.Clear();
- lbGPAOutput.Items.Clear();
- arrayIndex = 0;
- }
- private void btnExit_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private bool isValidData()
- {
- const int MAX_PERCENTAGE = 100;
- const int MIN_PERCENTAGE = 0;
- return
- //validate hours
- isPresent(txtHours, "Hours") &&
- isDouble(txtHours, "Hours") &&
- //validate percentage
- isPresent(txtPercent, "Percentage") &&
- isDouble(txtPercent, "Percentage") &&
- isWithinRange(txtPercent, "Percent", MIN_PERCENTAGE, MAX_PERCENTAGE);
- }
- private double calculateGPA(double gradePercentage)
- {
- double calculatedResult = 0;
- if (gradePercentage >= 90 && gradePercentage <= 100)
- {
- calculatedResult = GPA_90_100;
- }
- else if (gradePercentage >= 85 && gradePercentage <= 89)
- {
- calculatedResult = GPA_85_89;
- }
- else if (gradePercentage >= 80 && gradePercentage <= 84)
- {
- calculatedResult = GPA_80_84;
- }
- else if (gradePercentage >= 75 && gradePercentage <= 79)
- {
- calculatedResult = GPA_75_79;
- }
- else if (gradePercentage >= 70 && gradePercentage <= 74)
- {
- calculatedResult = GPA_70_74;
- }
- else if (gradePercentage >= 65 && gradePercentage <= 69)
- {
- calculatedResult = GPA_65_69;
- }
- else if (gradePercentage >= 60 && gradePercentage <= 64)
- {
- calculatedResult = GPA_60_64;
- }
- else if (gradePercentage >= 55 && gradePercentage <= 59)
- {
- calculatedResult = GPA_55_59;
- }
- else if (gradePercentage >= 50 && gradePercentage <= 54)
- {
- calculatedResult = GPA_50_54;
- }
- else//gradePercentage < 50
- {
- calculatedResult = GPA_BELOW_50;
- }
- return calculatedResult;
- }
- private bool isDouble(TextBox aTextBox, string name)
- {
- //check if the textbox is a double, assume true
- bool doubleValue = true;
- try
- {
- //try to convert it, catch otherwise
- Convert.ToDouble(aTextBox.Text);
- }
- catch (FormatException)
- {
- MessageBox.Show(name + " must be a decimal value.", "Entry Error");
- //sets the textBox with a problem as the focus and highlight
- aTextBox.Focus();
- aTextBox.SelectAll();
- //textbox is not a double
- doubleValue = false;
- }
- catch (OverflowException myOverflowEx)
- {
- throw myOverflowEx; // throw to the calling method to handle
- }
- catch (Exception myEx)
- {
- throw myEx; // throw to the calling method to handle
- }
- return doubleValue;
- }
- private bool isPresent(TextBox aTextBox, string name)
- {
- //check if the textbox has an entry, assume true
- bool doesExist = true;
- //empty textBox
- if (aTextBox.Text == "")
- {
- MessageBox.Show(name + " is a required field.", "Entry Error");
- //sets the textBox with a problem as the focus
- aTextBox.Focus();
- aTextBox.SelectAll();
- doesExist = false;
- }
- return doesExist;
- }
- private bool isWithinRange(TextBox aTextBox, string name, decimal RANGE_MIN, decimal RANGE_MAX)
- {
- bool withinRange = true;
- decimal aNumber;
- try
- {
- aNumber = Convert.ToDecimal(aTextBox.Text);
- //outside of range
- if (aNumber > RANGE_MAX || aNumber < RANGE_MIN)
- {
- MessageBox.Show(name + " must be between " + RANGE_MIN + " and " + RANGE_MAX, "Entry Error");
- //select the text of the error textBox for user friendliness
- aTextBox.Focus();
- aTextBox.SelectAll();
- //not within range
- withinRange = false;
- }
- }
- catch (FormatException myFormatEx)
- {
- throw myFormatEx;
- }
- catch (Exception myEx)
- {
- throw myEx;
- }
- return withinRange;
- }
- private double QualityPointValues(double courseHours, double coursePercentage )
- {
- double QualityPointValue;
- if (coursePercentage >= 90 && coursePercentage <= 100)
- {
- QualityPointValue=courseHours*GPA_90_100;
- }
- else if (coursePercentage >= 85 && coursePercentage <= 89)
- {
- QualityPointValue=courseHours*GPA_85_89;
- }
- else if (coursePercentage >= 80 && coursePercentage <= 84)
- {
- QualityPointValue=courseHours*GPA_80_84;
- }
- else if (coursePercentage >= 75 && coursePercentage <= 79)
- {
- QualityPointValue=courseHours*GPA_75_79;
- }
- else if (coursePercentage >= 70 && coursePercentage <= 74)
- {
- QualityPointValue=courseHours*GPA_70_74;
- }
- else if (coursePercentage >= 65 && coursePercentage <= 69)
- {
- QualityPointValue=courseHours*GPA_65_69;
- }
- else if (coursePercentage >= 60 && coursePercentage <= 64)
- {
- QualityPointValue=courseHours*GPA_60_64;
- }
- else if (coursePercentage >= 55 && coursePercentage <= 59)
- {
- QualityPointValue=courseHours*GPA_55_59;
- }
- else if (coursePercentage >= 50 && coursePercentage <= 54)
- {
- QualityPointValue=courseHours*GPA_50_54;
- }
- else//gradePercentage < 50
- {
- QualityPointValue=courseHours*GPA_BELOW_50;
- }
- return QualityPointValue;
- }
- private double CGPACalculate(double hourTotal, double QualityPointValueTotal)
- {
- double CGPA;
- CGPA = QualityPointValueTotal / hourTotal;
- return CGPA;
- }
- }
- }
Add Comment
Please, Sign In to add comment