Guest User

Untitled

a guest
Apr 22nd, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace TeamLabAssignment
  11. {
  12. public partial class frmCGPACalculator : Form
  13. {
  14. const int MAX_COURSES = 40;
  15. const double GPA_90_100 = 5.0;
  16. const double GPA_85_89 = 4.5;
  17. const double GPA_80_84 = 4.0;
  18. const double GPA_75_79 = 3.5;
  19. const double GPA_70_74 = 3.0;
  20. const double GPA_65_69 = 2.5;
  21. const double GPA_60_64 = 2.0;
  22. const double GPA_55_59 = 1.5;
  23. const double GPA_50_54 = 1.0;
  24. const double GPA_BELOW_50 = 0;
  25. int arrayIndex = 0;
  26. //define the arrays in the class. cant be stored in click method, else data loss
  27. double[] coursesPercentages = new double[MAX_COURSES];
  28. double[] coursesGPA = new double[MAX_COURSES];
  29.  
  30. public frmCGPACalculator()
  31. {
  32. InitializeComponent();
  33. }
  34.  
  35. private void btnCalculate_Click(object sender, EventArgs e)
  36. {
  37. double coursePercentage;
  38. double courseHours;
  39. double hourTotal=0;
  40. double QualityPointValuesTotal=0;
  41.  
  42. try
  43. {
  44. if (isValidData())
  45. {
  46. coursePercentage = Convert.ToDouble(txtPercent.Text);
  47. courseHours = Convert.ToDouble(txtHours.Text);
  48.  
  49. hourTotal += courseHours; //add all the hours as user enters them
  50.  
  51. QualityPointValuesTotal = QualityPointValues(courseHours, coursePercentage);
  52. //Totals the Quality Point Value as user enters grades
  53.  
  54. //add the data to the array
  55. coursesPercentages[arrayIndex] = coursePercentage;
  56. coursesGPA[arrayIndex] = calculateGPA(coursePercentage);
  57. //coursesGPA[arrayIndex] = courseHours;
  58.  
  59. lbGPAOutput.Items.Add(coursePercentage + "% - GPA: " + coursesGPA[arrayIndex]);
  60. //calculate
  61.  
  62. txtHours.Clear();
  63. txtPercent.Clear();
  64. txtPercent.Focus();
  65.  
  66.  
  67. arrayIndex++;
  68. }
  69. }
  70. //not a number
  71. catch (FormatException myFormatEx)
  72. {
  73. MessageBox.Show(myFormatEx.Message + "\nInvalid data type entered. Please check all entries.", "Entry Error");
  74. }
  75. //too large for decimal
  76. catch (OverflowException myOverflowEx)
  77. {
  78. MessageBox.Show(myOverflowEx.Message + "\nOverflow error. Please enter smaller values.", "Entry Error");
  79. }
  80. //other exception
  81. catch (Exception myEx)
  82. {
  83. MessageBox.Show(myEx.Message + "\n\n" + myEx.GetType().ToString() + "\n" + myEx.StackTrace, "Exception");
  84. }
  85.  
  86.  
  87. //Print to CGPA lable after calling and convering method
  88. // lblCGPAOutput.Text= Convert.ToString(CGPACalculate(hourTotal, QualityPointValuesTotal));
  89.  
  90.  
  91. }
  92.  
  93. private void btnClear_Click(object sender, EventArgs e)
  94. {
  95. txtHours.Clear();
  96. txtPercent.Clear();
  97. lbGPAOutput.Items.Clear();
  98. arrayIndex = 0;
  99. }
  100.  
  101. private void btnExit_Click(object sender, EventArgs e)
  102. {
  103. this.Close();
  104. }
  105.  
  106. private bool isValidData()
  107. {
  108. const int MAX_PERCENTAGE = 100;
  109. const int MIN_PERCENTAGE = 0;
  110.  
  111. return
  112. //validate hours
  113. isPresent(txtHours, "Hours") &&
  114. isDouble(txtHours, "Hours") &&
  115.  
  116. //validate percentage
  117. isPresent(txtPercent, "Percentage") &&
  118. isDouble(txtPercent, "Percentage") &&
  119.  
  120. isWithinRange(txtPercent, "Percent", MIN_PERCENTAGE, MAX_PERCENTAGE);
  121. }
  122. private double calculateGPA(double gradePercentage)
  123. {
  124.  
  125.  
  126.  
  127. double calculatedResult = 0;
  128.  
  129. if (gradePercentage >= 90 && gradePercentage <= 100)
  130. {
  131. calculatedResult = GPA_90_100;
  132. }
  133. else if (gradePercentage >= 85 && gradePercentage <= 89)
  134. {
  135. calculatedResult = GPA_85_89;
  136. }
  137. else if (gradePercentage >= 80 && gradePercentage <= 84)
  138. {
  139. calculatedResult = GPA_80_84;
  140. }
  141. else if (gradePercentage >= 75 && gradePercentage <= 79)
  142. {
  143. calculatedResult = GPA_75_79;
  144. }
  145. else if (gradePercentage >= 70 && gradePercentage <= 74)
  146. {
  147. calculatedResult = GPA_70_74;
  148. }
  149. else if (gradePercentage >= 65 && gradePercentage <= 69)
  150. {
  151. calculatedResult = GPA_65_69;
  152. }
  153. else if (gradePercentage >= 60 && gradePercentage <= 64)
  154. {
  155. calculatedResult = GPA_60_64;
  156. }
  157. else if (gradePercentage >= 55 && gradePercentage <= 59)
  158. {
  159. calculatedResult = GPA_55_59;
  160. }
  161. else if (gradePercentage >= 50 && gradePercentage <= 54)
  162. {
  163. calculatedResult = GPA_50_54;
  164. }
  165. else//gradePercentage < 50
  166. {
  167. calculatedResult = GPA_BELOW_50;
  168. }
  169.  
  170. return calculatedResult;
  171. }
  172.  
  173. private bool isDouble(TextBox aTextBox, string name)
  174. {
  175. //check if the textbox is a double, assume true
  176. bool doubleValue = true;
  177.  
  178. try
  179. {
  180. //try to convert it, catch otherwise
  181. Convert.ToDouble(aTextBox.Text);
  182. }
  183. catch (FormatException)
  184. {
  185. MessageBox.Show(name + " must be a decimal value.", "Entry Error");
  186. //sets the textBox with a problem as the focus and highlight
  187. aTextBox.Focus();
  188. aTextBox.SelectAll();
  189.  
  190. //textbox is not a double
  191. doubleValue = false;
  192. }
  193. catch (OverflowException myOverflowEx)
  194. {
  195. throw myOverflowEx; // throw to the calling method to handle
  196. }
  197. catch (Exception myEx)
  198. {
  199. throw myEx; // throw to the calling method to handle
  200. }
  201. return doubleValue;
  202. }
  203.  
  204. private bool isPresent(TextBox aTextBox, string name)
  205. {
  206. //check if the textbox has an entry, assume true
  207. bool doesExist = true;
  208.  
  209. //empty textBox
  210. if (aTextBox.Text == "")
  211. {
  212. MessageBox.Show(name + " is a required field.", "Entry Error");
  213. //sets the textBox with a problem as the focus
  214. aTextBox.Focus();
  215. aTextBox.SelectAll();
  216. doesExist = false;
  217. }
  218.  
  219. return doesExist;
  220. }
  221.  
  222. private bool isWithinRange(TextBox aTextBox, string name, decimal RANGE_MIN, decimal RANGE_MAX)
  223. {
  224. bool withinRange = true;
  225. decimal aNumber;
  226.  
  227. try
  228. {
  229. aNumber = Convert.ToDecimal(aTextBox.Text);
  230.  
  231. //outside of range
  232. if (aNumber > RANGE_MAX || aNumber < RANGE_MIN)
  233. {
  234. MessageBox.Show(name + " must be between " + RANGE_MIN + " and " + RANGE_MAX, "Entry Error");
  235. //select the text of the error textBox for user friendliness
  236. aTextBox.Focus();
  237. aTextBox.SelectAll();
  238.  
  239. //not within range
  240. withinRange = false;
  241. }
  242. }
  243. catch (FormatException myFormatEx)
  244. {
  245. throw myFormatEx;
  246. }
  247. catch (Exception myEx)
  248. {
  249. throw myEx;
  250. }
  251.  
  252. return withinRange;
  253. }
  254.  
  255.  
  256. private double QualityPointValues(double courseHours, double coursePercentage )
  257. {
  258. double QualityPointValue;
  259.  
  260. if (coursePercentage >= 90 && coursePercentage <= 100)
  261. {
  262. QualityPointValue=courseHours*GPA_90_100;
  263. }
  264. else if (coursePercentage >= 85 && coursePercentage <= 89)
  265. {
  266. QualityPointValue=courseHours*GPA_85_89;
  267. }
  268. else if (coursePercentage >= 80 && coursePercentage <= 84)
  269. {
  270. QualityPointValue=courseHours*GPA_80_84;
  271. }
  272. else if (coursePercentage >= 75 && coursePercentage <= 79)
  273. {
  274. QualityPointValue=courseHours*GPA_75_79;
  275. }
  276. else if (coursePercentage >= 70 && coursePercentage <= 74)
  277. {
  278. QualityPointValue=courseHours*GPA_70_74;
  279. }
  280. else if (coursePercentage >= 65 && coursePercentage <= 69)
  281. {
  282. QualityPointValue=courseHours*GPA_65_69;
  283. }
  284. else if (coursePercentage >= 60 && coursePercentage <= 64)
  285. {
  286. QualityPointValue=courseHours*GPA_60_64;
  287. }
  288. else if (coursePercentage >= 55 && coursePercentage <= 59)
  289. {
  290. QualityPointValue=courseHours*GPA_55_59;
  291. }
  292. else if (coursePercentage >= 50 && coursePercentage <= 54)
  293. {
  294. QualityPointValue=courseHours*GPA_50_54;
  295. }
  296. else//gradePercentage < 50
  297. {
  298. QualityPointValue=courseHours*GPA_BELOW_50;
  299. }
  300.  
  301. return QualityPointValue;
  302. }
  303.  
  304. private double CGPACalculate(double hourTotal, double QualityPointValueTotal)
  305. {
  306. double CGPA;
  307.  
  308. CGPA = QualityPointValueTotal / hourTotal;
  309.  
  310. return CGPA;
  311. }
  312.  
  313.  
  314.  
  315.  
  316. }
  317. }
Add Comment
Please, Sign In to add comment