Garey

Java Kontrolno

Mar 19th, 2019
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.79 KB | None | 0 0
  1. package com.company;
  2.  
  3. public class Student implements Comparable {
  4.     private String name;
  5.     private String faculty;
  6.     private int points;
  7.  
  8.     public Student(String name, String faculty, int points) {
  9.         this.name = name;
  10.         this.faculty = faculty;
  11.         this.points = points;
  12.     }
  13.  
  14.     /*! Get Methods */
  15.     public int getPoints() {
  16.         return points;
  17.     }
  18.  
  19.     public String getFaculty() {
  20.         return faculty;
  21.     }
  22.  
  23.     public String getName() {
  24.         return name;
  25.     }
  26.     /*! Set Methods */
  27.     public void setFaculty(String faculty) {
  28.         this.faculty = faculty;
  29.     }
  30.  
  31.     public void setName(String name) {
  32.         this.name = name;
  33.     }
  34.  
  35.     public void setPoints(int points) {
  36.         this.points = points;
  37.     }
  38.  
  39.     /*! Comparable */
  40.  
  41.     @Override
  42.     public String toString() {
  43.         return "Name: " + name + " - Faculty number: " + faculty + " - Points: " + points;
  44.     }
  45.  
  46.     @Override
  47.     public int compareTo(Object obj) {
  48.         if(obj instanceof Student) {
  49.            return ((Student) obj).getPoints() == this.points ? 0 : ((Student) obj).getPoints() < this.points ? 1 : -1;
  50.         }
  51.  
  52.         return -2;
  53.     }
  54. }
  55.  
  56. /*! ============================================================================= */
  57.  
  58. package com.company;
  59.  
  60. import java.io.File;
  61. import java.nio.file.Files;
  62. import java.util.Arrays;
  63. import java.util.Scanner;
  64. import java.util.stream.Stream;
  65.  
  66. public class StudentGroup {
  67.     private Student students[];
  68.  
  69.     /*! Constructor */
  70.     StudentGroup(String fileName) {
  71.         try {
  72.             var linesCount = 0;
  73.  
  74.             var file = new File(fileName);
  75.             var scanner = new Scanner(file);
  76.  
  77.             try(Stream<String> lines = Files.lines(file.toPath())) {
  78.                 linesCount = (int)lines.count();
  79.             }
  80.  
  81.             this.students = new Student[linesCount];
  82.  
  83.            while(scanner.hasNextLine())
  84.                 this.students[--linesCount] = new Student((scanner.next() + " " + scanner.next()), scanner.next(), scanner.nextInt());
  85.  
  86.         } catch(Exception err) {
  87.             System.out.println(err);
  88.         }
  89.     }
  90.  
  91.     /*! Print function */
  92.     public void print() {
  93.         for(var i = 0; i < this.students.length; i++)
  94.             System.out.println(this.students[i].toString());
  95.     }
  96.  
  97.     /*! Sorting */
  98.     public void sort() {
  99.        Arrays.sort(this.students);
  100.     }
  101.  
  102.     /*! Average */
  103.     public int average() {
  104.         var sum = 0;
  105.  
  106.         for(var i = 0; i < this.students.length; i++)
  107.             sum += this.students[i].getPoints();
  108.  
  109.         return (sum / this.students.length);
  110.     }
  111. }
  112.  
  113. /*! ============================================================================== */
  114. Georgi Petrov 17621707 104
  115. Petur Ivanov 17621708 99
Add Comment
Please, Sign In to add comment