Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.math.RoundingMode;
- import java.text.DecimalFormat;
- import java.util.*;
- class UtilMath {
- static public double roundDown(double nr, int prec) {
- final String format = "." + "#".repeat(prec);
- var decimalFormat = new DecimalFormat(format);
- decimalFormat.setRoundingMode(RoundingMode.DOWN);
- return Double.parseDouble(decimalFormat.format(nr));
- }
- }
- class Student implements Comparable<Student> {
- private int id;
- private String name;
- private List<Double> grades;
- public ArrayList<Double> getGrades() {
- return (ArrayList<Double>) grades;
- }
- public Student(int id, String name) {
- this.id = id;
- this.name = name;
- grades = new ArrayList<>();
- }
- public void addGrade(double grade) {
- grades.add(grade);
- }
- public double getGPA() {
- double[] primDoubles = grades.stream().mapToDouble(d -> d).toArray();
- double mean = Arrays.stream(primDoubles).average().orElse(Double.NaN);
- return UtilMath.roundDown(mean, 1);
- }
- public String getName() {
- return name;
- }
- public int getId() {
- return id;
- }
- @Override
- public String toString() {
- return "Student{" +
- "id=" + id +
- ", name='" + name + '\'' +
- ", grades=" + grades +
- '}';
- }
- @Override
- public int compareTo(Student other) {
- return name.compareTo(other.name);
- }
- static class GpaComp implements Comparator<Student> {
- @Override
- public int compare(Student student1, Student student2) {
- if (student1.getGPA() > student2.getGPA()) {
- return -1;
- }
- return 1;
- }
- }
- }
- class University implements Comparable<University> {
- final static int SCHOOLARSHIP_NR = 2;
- private String name;
- private List<Student> students;
- public String getName() {
- return name;
- }
- public University(String name) {
- this.name = name;
- students = new ArrayList<>();
- }
- void addStud(final Student student) {
- students.add(student);
- }
- public double getGPA() {
- double mean = 0;
- int nr = 0;
- for (var student : students) {
- for (double grade : student.getGrades()) {
- mean += grade;
- }
- nr += student.getGrades().size();
- }
- mean /= 1.0 * nr;
- return UtilMath.roundDown(mean, 1);
- }
- public List<Student> getStudents() {
- return students;
- }
- public Student findStudById(int id) {
- for (var student : students) {
- if (student.getId() == id) {
- return student;
- }
- }
- return null;
- }
- @Override
- public String toString() {
- return "University{" +
- "name='" + name + '\'' +
- ", students=" + students +
- '}';
- }
- public void sortStudents() {
- Collections.sort(students);
- }
- @Override
- public int compareTo(University other) {
- if (getGPA() > other.getGPA()) {
- return -1;
- }
- return 1;
- }
- List<String> findStudentsNameWithSchoolarship() {
- Set<Student> set = new TreeSet<>(new Student.GpaComp());
- for (var student : students) {
- set.add(student);
- }
- List<String> ans = new ArrayList<>();
- Iterator<Student> iterator = set.iterator();
- while (iterator.hasNext()) {
- if (ans.size() == SCHOOLARSHIP_NR) {
- break;
- }
- ans.add(iterator.next().getName());
- }
- return ans;
- }
- public double getTOpGPA() {
- double ans = Double.NEGATIVE_INFINITY;
- for (var student : students) {
- ans = Math.max(ans, student.getGPA());
- }
- return ans;
- }
- }
- class UniversitiesManager {
- private static UniversitiesManager instance = null;
- List<University> universities;
- private UniversitiesManager() {
- universities = new ArrayList<>();
- }
- public static synchronized UniversitiesManager getInstance() {
- if (instance == null) {
- instance = new UniversitiesManager();
- }
- return instance;
- }
- private void readStudents(Scanner scanner, List<University> universities) {
- int n = scanner.nextInt();
- for (int i = 0; i < n; i++) {
- int studId = scanner.nextInt();;
- String name = scanner.next();
- String uniName = scanner.next();
- int idx = -1;
- for (int j = 0; j < universities.size() && idx == -1; j++) {
- if (universities.get(j).getName().equals(uniName)) {
- idx = j;
- }
- }
- var student = new Student(studId, name);
- if (idx != -1) {
- universities.get(idx).addStud(student);
- } else {
- var university = new University(uniName);
- university.addStud(student);
- universities.add(university);
- }
- }
- }
- private void readGrades(Scanner scanner, List<University> universities) {
- int m = scanner.nextInt();
- for (int i = 0; i < m; i++) {
- double grade = scanner.nextDouble();
- int studId = scanner.nextInt();
- for (var university : universities) {
- var student = university.findStudById(studId);
- if (student != null) {
- student.addGrade(grade);
- break;
- }
- }
- }
- }
- public void readData(final String FILE_PATH) throws FileNotFoundException {
- var file = new File(FILE_PATH);
- universities = new ArrayList<>();
- var scanner = new Scanner(file);
- readStudents(scanner, universities);
- readGrades(scanner, universities);
- scanner.close();
- }
- public void task1() {
- System.out.println("TASK1: ");
- for (var university : universities) {
- university.sortStudents();
- }
- Collections.sort(universities);
- for (var university : universities) {
- System.out.print(university.getName() + ": ");
- for (var student : university.getStudents()) {
- System.out.print(student.getName() + " ");
- }
- System.out.println();
- }
- System.out.println();
- }
- public void task2() {
- System.out.println("TASK2: ");
- for (var university : universities) {
- String schoolarshipsStr = String.join(" ", university.findStudentsNameWithSchoolarship());
- System.out.println(university.getName() + ": " + schoolarshipsStr);
- }
- System.out.println();
- }
- public void task3() {
- System.out.println("TASK3: ");
- List<Student> students = new ArrayList<>();
- for (var university : universities) {
- students.addAll(university.getStudents());
- university.getStudents().clear();
- }
- Collections.sort(students, new Student.GpaComp());
- for (int i = universities.size() - 1, j = 0; i >= 0; i--, j++) {
- universities.get(i).addStud(students.get(j));
- }
- for (int i = students.size() - 1; i >= universities.size(); i--) {
- universities.get(0).addStud(students.get(i));
- }
- for (var university : universities) {
- System.out.print(university.getName() + ": ");
- for (var student : university.getStudents()) {
- System.out.print(student.getName() + " ");
- }
- System.out.println();
- }
- //if you want to check the difference uncomment the next line
- //System.out.println(universities.get(universities.size() - 1).getTOpGPA() - universities.get(0).getTOpGPA());
- }
- }
- public class Main{
- final static String FILE_PATH = "data.txt";
- public static void main(String args[]) throws FileNotFoundException {
- var universitiesManager = UniversitiesManager.getInstance();
- universitiesManager.readData(FILE_PATH);
- universitiesManager.task1();
- universitiesManager.task2();
- universitiesManager.task3();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement