Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- public class Student implements Comparable {
- private String name;
- private String faculty;
- private int points;
- public Student(String name, String faculty, int points) {
- this.name = name;
- this.faculty = faculty;
- this.points = points;
- }
- /*! Get Methods */
- public int getPoints() {
- return points;
- }
- public String getFaculty() {
- return faculty;
- }
- public String getName() {
- return name;
- }
- /*! Set Methods */
- public void setFaculty(String faculty) {
- this.faculty = faculty;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setPoints(int points) {
- this.points = points;
- }
- /*! Comparable */
- @Override
- public String toString() {
- return "Name: " + name + " - Faculty number: " + faculty + " - Points: " + points;
- }
- @Override
- public int compareTo(Object obj) {
- if(obj instanceof Student) {
- return ((Student) obj).getPoints() == this.points ? 0 : ((Student) obj).getPoints() < this.points ? 1 : -1;
- }
- return -2;
- }
- }
- /*! ============================================================================= */
- package com.company;
- import java.io.File;
- import java.nio.file.Files;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.stream.Stream;
- public class StudentGroup {
- private Student students[];
- /*! Constructor */
- StudentGroup(String fileName) {
- try {
- var linesCount = 0;
- var file = new File(fileName);
- var scanner = new Scanner(file);
- try(Stream<String> lines = Files.lines(file.toPath())) {
- linesCount = (int)lines.count();
- }
- this.students = new Student[linesCount];
- while(scanner.hasNextLine())
- this.students[--linesCount] = new Student((scanner.next() + " " + scanner.next()), scanner.next(), scanner.nextInt());
- } catch(Exception err) {
- System.out.println(err);
- }
- }
- /*! Print function */
- public void print() {
- for(var i = 0; i < this.students.length; i++)
- System.out.println(this.students[i].toString());
- }
- /*! Sorting */
- public void sort() {
- Arrays.sort(this.students);
- }
- /*! Average */
- public int average() {
- var sum = 0;
- for(var i = 0; i < this.students.length; i++)
- sum += this.students[i].getPoints();
- return (sum / this.students.length);
- }
- }
- /*! ============================================================================== */
- Georgi Petrov 17621707 104
- Petur Ivanov 17621708 99
Add Comment
Please, Sign In to add comment