-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgpaApp.java
More file actions
120 lines (117 loc) · 4.15 KB
/
Copy pathcgpaApp.java
File metadata and controls
120 lines (117 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package org.example;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.time.format.DateTimeFormatter;
public class Main{
static Scanner Stan = new Scanner(System.in);
static FileWriter Stanf;
static int option,n, i,credits[];
static String name, regNo, reply;
static char grades;
static double gradepoints[], weighted[],weightedSum,CGPA,totalCredits;
static boolean hasData = false;
public static void main(String[] args) {
System.out.printf("Welcome to Stanley's CGPA calculator%n=======************======%nSelect from one of the options below%n");
do {
System.out.println("=======************======");
System.out.println("1. Calculate CGPA");
System.out.println("2. Export CGPA to file");
System.out.println("3. Exit");
System.out.print("Select an option: ");
option = Stan.nextInt();
switch (option) {
case 1:
CalculateCGPA();
break;
case 2:
exportFile();
break;
case 3:
System.out.println("Goodbye from Stanley");
break;
default:
System.out.println("Invalid input pls try again");
}
} while (option != 3);
}
static void CalculateCGPA(){
System.out.print("Enter Your name: ");
name = Stan.next();
System.out.print("Enter your matric number: ");
regNo = Stan.next();
System.out.print("Enter number of courses: ");
n = Stan.nextInt();
weighted = new double[n];
credits = new int[n];
gradepoints = new double[n];
loopInput();
for(double weight: weighted){
weightedSum += weight;
}
for(int credit:credits){
totalCredits += credit;
}
CGPA = weightedSum/totalCredits;
System.out.println("============================");
System.out.printf("Your CGPA is %.2f%n",CGPA);
System.out.println("============================");
hasData = true;
}
static void loopInput() {
for (i = 0; i < n; i++) {
System.out.println("====================");
System.out.printf("<===Course %d===> %n", i + 1);
System.out.print("Enter credit: ");
credits[i] = Stan.nextInt();
System.out.print("Enter grade: ");
grades = Character.toUpperCase(Stan.next().charAt(0));
calGrades();
weighted[i] = gradepoints[i] * credits[i];
}
}
static void calGrades(){
switch (grades) {
case 'A':
gradepoints[i] = 5;
break;
case 'B':
gradepoints[i] = 4;
break;
case 'C':
gradepoints[i] = 3;
break;
case 'D':
gradepoints[i] = 2;
break;
case 'E':
gradepoints[i] = 1;
break;
case 'F':
gradepoints[i] = 0;
break;
default:
System.out.printf("Invalid grade%nPlease input details on course %d", i + 1);
i--;
break;
}
}
static void exportFile(){
if(hasData){
try{
Stanf = new FileWriter(name+".txt");
Stanf.write(String.format("==========CGPA REPORT=========%nName: %s%nMatric No.: %s%nCGPA: %.2f%n============== ", name,regNo,CGPA));
Stanf.write(String.format("This result was calculated by Stanley Obimma at:%n%s", Stanf ));
Stanf.close();
System.out.printf("Result saved successfully at %s.txt",name);
}
catch (IOException e){
System.out.print("File not saved");
}
}
else
{
System.out.println("No data to export!!!");
}
}
}