
package gpa_calculator;
import java.util.Scanner;

public class Student {
private long studNumber;
private String firstName;
private String lastName;
private double marks [];
private double bonus [];
private double totalMarks;
private double totalBonus;
private double gpa;
private Scanner input;
private static final int NUMBER_MARKS = 3;


public Student() { //Constructor
marks = new double [NUMBER_MARKS];
this.input = new Scanner(System.in);
}

//This method will read student details from the user
public void readStudentDetails() {
	System.out.print("Enter Student Number: ");
		studNumber = input.nextLong();
	System.out.print("Enter first name: ");
		firstName = input.next();
	System.out.print("Enter last name: ");
		lastName= input.next();
		this.readMarks();
		this.readBonuses(0);
}
//This method will read marks from the student
private void readMarks() {

 for(int i=0; i < marks.length; i++) {
 System.out.print("Enter mark "+ (i + 1)+": ");
 marks[i] = input.nextDouble();

 while  (marks[i] < 0 || marks[i] > 100) {  
 
 System.out.println("Marks should be within 0 and 100. Please reenter.");
 System.out.print("Enter mark "+(i+1)+": ");  
 marks[i] = input.nextDouble();
 
}
 this.totalMarks += marks[i];
}
}
//This method reads number of bonuses from the user
private void readBonuses(int numberOfBonuses) {
	System.out.print("Enter number of bonuses (should not be greater than 5): ");
	numberOfBonuses = input.nextInt();
	while (numberOfBonuses>5 || numberOfBonuses<0) {
		System.out.println("Number of bonuses should not be greater than 5 !!!\n");
		System.out.print("Enter number of bonuses (should not be greater than 5):");
		numberOfBonuses=input.nextInt();}
	bonus = new double [numberOfBonuses];
	for(int i=0; i<bonus.length; i ++) {
		System.out.print("Enter bonus " + (i+1) + ": ");
		bonus[i] = input.nextDouble(); 
		while  (bonus[i] < 0 || bonus[i] > 100) {  
			 
			 System.out.println("Bonus should be within 0 and 100. Please reenter.");
			 System.out.print("Enter bonus "+(i+1)+": ");  
			 bonus[i] = input.nextDouble();	
		}
		this.totalBonus +=bonus[i];
		}		
	
	}
//This method calculates total GPA based on total marks and total bonus
public void calculateGpa() {
	if (!checkEligibility()) { 
		this.convertBonus();
		this.totalMarks += totalBonus; 
		gpa = 0;		
	}
	else {
this.convertBonus();
this.totalMarks += totalBonus; 
gpa = (this.totalMarks / (NUMBER_MARKS * 100)) * 4;
while (gpa >4) {
	this.gpa = 4;	
}

}
}	
//This method converts bonus from the user into a formula needed to calculate final percentage
private double convertBonus() {
if(this.totalBonus>15) {
this.totalBonus=15;
this.totalBonus=(totalBonus/15) * 2;        
return totalBonus;
}
else
this.totalBonus=(totalBonus/15) * 2;        
return totalBonus;
}
//This method will calculate percentage based on number of marks
private double calculatePercentage() {
return totalMarks/ (NUMBER_MARKS * 100) * 100;
}
private String findGradeLetter() {
if (!checkEligibility()) { //if (checkEligibility() == false)
return "F";
}else if(calculatePercentage() > 89 && calculatePercentage() <101) {
return  "A+";
}else if (calculatePercentage() > 84 ){
return "A";
}else if (calculatePercentage() > 79 ){
return "A-";
}else if (calculatePercentage() > 76 ){
return "B+";
}else if (calculatePercentage() > 72 ){
return "B";
}else if (calculatePercentage() > 69 ){
return "B-";
}else if (calculatePercentage() > 66 ){
return "C+";
}else if (calculatePercentage() > 62 ){
return "C";
}else if (calculatePercentage() > 59 ){
return "C-";
}else if (calculatePercentage() > 56 ){
return "D+";
}else if (calculatePercentage() > 52 ){
return "D";
}else return "D-";
}
private boolean checkEligibility() {
if(marks[0] <50 ) {
return false;
}
else if(marks[1] <50 ) {
return false;
}
else if(marks[2] <50 ) {
return false;
}else return true;
}
//This method is used to print student details
public void displayStudentDetails() {


	System.out.printf("%9d      |", studNumber);
	System.out.printf("%21s       |",firstName +" "+lastName);
	System.out.printf("%13.2f      |",totalMarks);
	System.out.printf("%8.2f   |",gpa);
	findGradeLetter();
	System.out.printf("         %-11s|",findGradeLetter());
	
this.checkEligibility();
if(findGradeLetter() == "F") {
System.out.print("     Note: failed one or more courses");
}
System.out.println();
}
//This method is used to display title on the top of the console 
public static void displayTitle() {
System.out.println("GPA CALCULATOR");
System.out.print("=================\n");
}
//This method is used to display header
public static void displayHeader() {
System.out.println("****************************************************************************************************");
System.out.println("                                          MARK LIST");
System.out.println("****************************************************************************************************");
System.out.println("Student Number |" +"            Name            |" +  "    Total Marks    |" + "    GPA    |" + "    Grade Letter    |");
System.out.println("-----------------------------------------------------------------------------------------------------");
}

}