Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AV5;
- public class Student implements Comparable<Student>{
- private String lastName;
- private String firstName;
- private int exam1;
- private int exam2;
- private int exam3;
- private char grade;
- public Student(String lastName, String firstName, int exam1 , int exam2 , int exam3) {
- this.lastName = lastName;
- this.firstName = firstName;
- this.exam1 = exam1;
- this.exam2 = exam2;
- this.exam3 = exam3;
- setGrade();
- }
- public static Student createStudent(String line)
- {
- String[] parts=line.split("\\s+");
- return new Student(parts[0],parts[1], Integer.parseInt(parts[2]) , Integer.parseInt(parts[3]) , Integer.parseInt(parts[4]));
- }
- public char getGrade() {
- return grade;
- }
- public double totalPoints() {
- return 0.25 * exam1 + 0.3 * exam2 + 0.45 * exam3;
- }
- public void setGrade() {
- double points = totalPoints();
- if (points >= 90) {
- this.grade = 'A';
- } else if (points >= 80) {
- this.grade = 'B';
- } else if (points >= 70) {
- this.grade = 'C';
- } else if (points >= 60) {
- this.grade = 'D';
- } else if (points >= 50) {
- this.grade = 'E';
- } else
- this.grade = 'F';
- }
- @Override
- public String toString() {
- return lastName + " " + firstName + " " + getGrade();
- }
- public String printFullInfo() {
- return String.format("%s %s %d %d %d %.2f %c", lastName, firstName, exam1, exam2, exam3, totalPoints(),grade);
- }
- @Override
- public int compareTo(Student other) {
- return Character.compare(this.grade, other.grade);
- }
- }
- package AV5;
- import java.io.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.stream.Collectors;
- public class Course {
- private List<Student> students;
- public Course() {
- students = new ArrayList<>();
- }
- public void readData(InputStream inputStream) {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- this.students = bufferedReader.lines()
- .map(line -> Student.createStudent(line))
- .collect(Collectors.toList());
- }
- public void printSortedData(OutputStream outputStream) {
- PrintWriter printWriter = new PrintWriter(outputStream);
- this.students.stream().sorted().forEach(s -> printWriter.println(s));
- printWriter.flush();
- }
- public void printDetailsData(OutputStream outputStream) {
- PrintWriter printWriter = new PrintWriter(outputStream);
- this.students.stream().sorted().forEach(s -> printWriter.println(s.printFullInfo()));
- printWriter.flush();
- }
- public void printDistributedData(OutputStream outputStream) {
- PrintWriter printWriter = new PrintWriter(outputStream);
- int[] gradeDistribution = new int[6];
- for (Student s : students) {
- gradeDistribution[s.getGrade() - 'A']++;
- for (int i = 0; i < 6; i++) {
- printWriter.printf("%c->%d\n", i + 'A', gradeDistribution[i]);
- printWriter.flush();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement