Jak dodać obiekty z pliku tekstowego do ArrayList i wyświetlić największą wartość za pomocą Collections.max

0

Witam serdecznie. Zmagam się z pewnym zadaniem i w trakcie pogubiłem się i nie wiem jak je dokończyć.Treść zadania:
Plik tekstowy ma postać:
Jan, Kowalski, 5000
Zygmunt, Radziwil, 9800
Eustachy, Kozak, 3100
Zbigniew, Brodaty, 2500
Oblicz średnią arytmetyczną pensji wszystkich pracowników. Wypisz na ekranie imię i nazwisko pracownika o największej pensji.

public class Zad6Plik {
    public class Employee {
        private String firstName;
        private String lastName;
        private double salary;

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public double getSalary() {
            return salary;
        }

        public void setSalary(double salary) {
            this.salary = salary;
        }
    }

    public static void readFile(String osoby) {
        try {
            FileReader fr = new FileReader(osoby);
            Scanner sc = new Scanner(fr);
            String firstName;
            String lastName;
            double salary;
            int total = 0;
            int count = 0;
            List<Employee> employees = new ArrayList<Employee>();
            while (sc.hasNextLine()) {
                firstName = sc.next();
                lastName = sc.next();
                salary = sc.nextDouble();

                total += salary;
                count++;
                // Add to List
                employees.add()
            }
            double average = total / count;
            System.out.println("The average salary of all employees are: " + average);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        readFile("osoby.txt");
    }
}

Poradziłem sobie z odczytaniem danych i obliczeniem średniego wynagrodzenia. Natomiast nie wiem jak dodać obiekty do ArrayList stworzyłem do tego celu klasę Employee opartą na JavaBeans. Pracownika z największą metodą chciałem wyświetlić za pomocą Collections.max . Proszę o pomoc bo zamotałem się i nie wiem jak to dalej zrobić. Pozdrawiam

0

Stwórz konstruktor klasy Employee później dodawaj do listy obiekty tej klasy. Czyli:

Konstruktor:

    public Employee(String firstName, String lastName, double salary) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
    }

Później dodajesz do tej Twojej listy tak:

employees.add(new Employee(firstName, lastName, salary));

Jakbyś miał pytania to pisz :)

1

@devKoksu:
Klasa Zad6Plik:

public class Zad6Plik {

    public static void readFile(String osoby) throws IOException {
        FileReader fr = null;
        try {
            fr = new FileReader(osoby);
            Scanner sc = new Scanner(fr);
            String firstName;
            String lastName;
            double salary;
            int total = 0;
            int count = 0;
            List<Employee> employees = new ArrayList<Employee>();
            while (sc.hasNextLine()) {
                firstName = sc.next();
                lastName = sc.next();
                salary = sc.nextDouble();

                total += salary;
                count++;
                // Add to List
                employees.add(new Employee(firstName, lastName, salary));
            }
            double average = total / count;
            System.out.println("The average salary of all employees are: " + average);

            System.out.println("Wyświetlamy wszystkie obiekty: ");
            for(Employee e : employees) {
                System.out.println(e);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            fr.close();
        }
    }

    public static void main(String[] args) throws IOException {
        readFile("osoby.txt");
    }
}

Oraz klasa Employee:

public class Employee {

    private String firstName;
    private String lastName;
    private double salary;

    public Employee(String firstName, String lastName, double salary) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.salary = salary;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", salary=" + salary +
                '}';
    }
}

Zwróć uwagę na finally w try catch :) Dodałem również toStringa aby sprawdzić czy obiekty się wyświetlają.

1 użytkowników online, w tym zalogowanych: 0, gości: 1