Skip to content

Commit

Permalink
Merge pull request #28 from NewtonMutugi/main
Browse files Browse the repository at this point in the history
GPA Calculator (Bug Fix)
  • Loading branch information
IamMuuo authored Oct 23, 2023
2 parents ac645a8 + 1bda2fd commit 133e946
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
27 changes: 21 additions & 6 deletions lib/controllers/gpacalculator_controller.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:academia/models/courses.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';

class GPACalculatorController extends GetxController {
Expand All @@ -7,9 +8,18 @@ class GPACalculatorController extends GetxController {
List<Unit> get courseList => _courses;
double get gpa => _calculateGPA();

void addCourse(String name, String code, double creditHours, String grade) {
void addCourse(String name, String code, String creditHours, String grade) {
double tcreditHours = 0;
try {
tcreditHours = double.parse(creditHours);
} catch (e) {
Get.defaultDialog(
title: "Incorrect credit Hours",
content: const Text("Credit hours must be a number"));
throw Exception('Credit hours must be a number');
}
final course =
Unit(name: name, code: code, creditHours: creditHours, grade: grade);
Unit(name: name, code: code, creditHours: tcreditHours, grade: grade);
_courses.add(course);
}

Expand All @@ -22,11 +32,18 @@ class GPACalculatorController extends GetxController {
{required int index,
required String newName,
required String newCode,
required double newCreditHours,
required String newCreditHours,
required String newGrade}) {
_courses[index].name = newName;
_courses[index].code = newCode;
_courses[index].creditHours = newCreditHours;
try {
_courses[index].creditHours = double.parse(newCreditHours);
} catch (e) {
Get.defaultDialog(
title: "Incorrect credit Hours",
content: const Text("Credit hours must be a number"));
throw Exception('Credit hours must be a number');
}
_courses[index].grade = newGrade;
_courses.refresh();
}
Expand All @@ -38,8 +55,6 @@ class GPACalculatorController extends GetxController {
totalPoints += getGradePoints(course.grade) * course.creditHours;
totalCreditHours += course.creditHours;
}
print(totalPoints.toString());
print(totalCreditHours.toString());
return (totalPoints / totalCreditHours).toPrecision(2);
}

Expand Down
26 changes: 19 additions & 7 deletions lib/pages/gpacalculator_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,14 @@ class GpaCalculator extends StatelessWidget {
const SizedBox(height: 20),
ElevatedButton(
onPressed: () {
if (unitController.courseList.length.isEqual(0)) {
Get.defaultDialog(
title: "List empty",
content: const Text("Input Grades!"));
}
Get.defaultDialog(
title: "Your GPA",
content: Text("Your GPA is ${unitController.gpa}"),
content: Text("Your GPA is ${unitController.gpa.toString()}"),
);
},
child: const Text("Calculate GPA"),
Expand Down Expand Up @@ -121,6 +126,8 @@ class GpaCalculator extends StatelessWidget {
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
keyboardType:
TextInputType.number,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Credit Hours',
Expand Down Expand Up @@ -161,9 +168,8 @@ class GpaCalculator extends StatelessWidget {
codeController
.text,
newCreditHours:
double.parse(
creditHoursController
.text),
creditHoursController
.text,
newGrade:
gradeController
.text);
Expand Down Expand Up @@ -262,6 +268,7 @@ class GpaCalculator extends StatelessWidget {
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
keyboardType: TextInputType.number,
decoration: const InputDecoration(
border: OutlineInputBorder(),
labelText: 'Credit Hours',
Expand Down Expand Up @@ -289,8 +296,13 @@ class GpaCalculator extends StatelessWidget {
onPressed: () {
final name = nameController.text;
final code = codeController.text;
final creditHours = double.parse(
creditHoursController.text);
final creditHours =
creditHoursController.text;

// if (creditHours is! double) {
// Get.snackbar("Invalid Credit hours",
// "Must be a number");
// }
final grade = gradeController.text;
if (nameController.text != '' &&
codeController.text != '' &&
Expand All @@ -304,7 +316,7 @@ class GpaCalculator extends StatelessWidget {
} else {
Get.defaultDialog(
title: "Invalid Grade",
content: Text(
content: const Text(
"Please enter a valid grade"),
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/tool_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ToolsPage extends StatelessWidget {
Get.to(
const WebviewPage(
title: "Daystar Elearning",
url: "https://student.daystar.ac.ke"),
url: "https://elearning.daystar.ac.ke"),
);
},
icon: const Icon(
Expand Down

0 comments on commit 133e946

Please sign in to comment.