Created
June 20, 2014 09:27
-
-
Save dlfigueira/24260b423a1a418a0d48 to your computer and use it in GitHub Desktop.
Challenge #167 [Intermediate] Final Grades
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.Scanner; | |
public class CS101Class { | |
private class StudentComparator implements Comparator<Student> { | |
@Override | |
public int compare(Student o1, Student o2) { | |
if(o1.getFinalGrade() > o2.getFinalGrade()) { | |
return -1; | |
} | |
else if(o1.getFinalGrade() < o2.getFinalGrade()) { | |
return 1; | |
} | |
return 0; | |
} | |
} | |
private ArrayList<Student> mStudents; | |
public CS101Class() { | |
mStudents = new ArrayList<Student>(); | |
} | |
public void addStudent(String firstName, String lastName, ArrayList<Integer> scores) { | |
Student student = new Student(firstName, lastName, scores); | |
setStudentGrade(student); | |
mStudents.add(student); | |
} | |
private void setStudentGrade(Student student) { | |
int finalScore = 0; | |
for (Integer grade : student.getScores()) { | |
finalScore += grade; | |
} | |
finalScore /= student.getScores().size(); | |
student.setFinalGrade(finalScore); | |
String letterGrade = new String(); | |
switch(finalScore/10) { | |
case 10: | |
case 9: | |
letterGrade = "A"; break; | |
case 8: | |
letterGrade = "B"; break; | |
case 7: | |
letterGrade = "C"; break; | |
case 6: | |
letterGrade = "D"; break; | |
case 5: | |
case 4: | |
case 3: | |
case 2: | |
case 1: | |
case 0: | |
letterGrade = "F"; break; | |
} | |
if(!letterGrade.equals("A") && !letterGrade.equals("F")) { | |
switch(finalScore%10) { | |
case 10: | |
case 9: | |
case 8: | |
letterGrade += "+"; break; | |
case 3: | |
case 2: | |
case 1: | |
letterGrade += "-"; break; | |
} | |
} | |
student.setLetterGrade(letterGrade); | |
} | |
@Override | |
public String toString() { | |
String output = new String(); | |
Collections.sort(mStudents, new StudentComparator()); | |
for (Student student : mStudents) { | |
output += student.toString() + "\n"; | |
} | |
return output; | |
} | |
public static void main(String[] args) { | |
CS101Class cs101Class = new CS101Class(); | |
try { | |
BufferedReader bufferedReader = new BufferedReader(new FileReader("student_scores.txt")); | |
String line; | |
while ((line = bufferedReader.readLine()) != null) { | |
Scanner scanner = new Scanner(line); | |
String firstName = scanner.next(); | |
String nextString = scanner.next(); | |
while (!nextString.contains(",")) { | |
firstName += " " + nextString; | |
nextString = scanner.next(); | |
} | |
firstName = firstName.replaceAll("[\\[\\],]", ""); | |
String lastName = nextString; | |
nextString = scanner.next(); | |
while (!nextString.matches("\\d+")) { | |
lastName += " " + nextString; | |
nextString = scanner.next(); | |
} | |
lastName = lastName.replaceAll("[,]", ""); | |
ArrayList<Integer> scores = new ArrayList<Integer>(); | |
scores.add(Integer.valueOf(nextString)); | |
while (scanner.hasNext()) { | |
scores.add(scanner.nextInt()); | |
} | |
cs101Class.addStudent(firstName, lastName, scores); | |
} | |
bufferedReader.close(); | |
} catch (NumberFormatException | IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println(cs101Class.toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Collections; | |
public class Student { | |
private final String FORMATED_STUDENT_STRING = "%-12s, %-12s (%2d%%) (%-2s):\t %s"; | |
private String mFirstName = null; | |
private String mLastName = null; | |
private ArrayList<Integer> mScores = null; | |
private String mLetterGrade = null; | |
private int mFinalGrade = 0; | |
public Student(String firstName, String lastName, ArrayList<Integer> scores) { | |
mFirstName = firstName; | |
mLastName = lastName; | |
mScores = scores; | |
Collections.sort(mScores); | |
Collections.reverse(mScores); | |
} | |
public int getFinalGrade() { | |
return mFinalGrade; | |
} | |
public ArrayList<Integer> getScores() { | |
return mScores; | |
} | |
public void setFinalGrade(int grade) { | |
mFinalGrade = grade; | |
} | |
public void setLetterGrade(String letterGrade) { | |
mLetterGrade = letterGrade; | |
} | |
@Override | |
public String toString() { | |
String scoresString = new String(); | |
for (Integer score : mScores) { | |
scoresString += String.format("%3d ", score); | |
} | |
return String.format(FORMATED_STUDENT_STRING, mLastName, mFirstName, mFinalGrade, mLetterGrade, scoresString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment