Java sum of salaries from .csv file

Viewed 244

I am trying to sum up salaries for each type of job from .csv file and right now my output only shows how much each person earns. I need to sum this earing and I don't have any idea how to write method which will sum up salaries for each job. Do you have any idea how could I change my code to get sum of earings?

package src;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
public class Main {
   public static void main(String[] args) {
       Path path = Path.of("sample.csv");
       try (Stream<String> lines = Files.lines(path);) {
           lines.filter(line -> line.startsWith(" "))
                .map(line -> LineToPerson(line))
                .forEach(System.out::println);
       }
       catch (IOException e) {
           e.printStackTrace();
       }
   }
   private static Person LineToPerson(String line) {
       String[] elements = line.split(";");
       String id = elements[0];
       String name = elements[1];
       String surname = elements[2];
       String job = elements[3];
       Double salary = Double.valueOf(elements[4].replaceAll("\"", "").replaceAll(" ", "").replaceAll(",", "."));
       Person p = new Person(id, name, surname, job, salary);
       return p;
   }
}

--------------------class Person----------------------------------

package src;

public class Person {

   private String id;
   private String name;
   private String surname;
   private String job;
   private double salary;

   public Person() {

   }
   public Person(String id, String name, String surname, String job, double salary) {
       this.id = id.replaceAll("\"", "").replaceAll(" ", "");
       this.name = name.replaceAll("\"", "").replaceAll(" ", "");
       this.surname = surname.replaceAll("\"", "").replaceAll(" ", "");
       this.job = job.replaceAll("\"", "").replaceAll(" ", "");
       this.salary = salary;
   }
   public String getId() {
       return id;
   }
   public String getName() {
       return name;
   }
   public String getSurname() {
       return surname;
   }
   public String getJob() {
       return job;
   }
   public double getSalary() {
       return salary;
   }
   public String toString() {
       if (job.equals("Teacher")) {
           return "Teacher salary = " + salary;
       } else if (job.equals("Janitor")) {
           return "Janitor salary = " + salary;
       } else return "";
   }
}
1 Answers

You need to group by job as follows:

lines.filter(line -> line.startsWith(" "))
        .map(line -> LineToPerson(line))
        .collect(Collectors.groupingBy(person -> person.getJob(),
                                Collectors.summingDouble(person -> person.getSalary())))
        .forEach((job,sumSalary) -> System.out.println(job + "\t" + sumSalary));

Replace

try (Stream<String> lines = Files.lines(path);) {
   lines.filter(line -> line.startsWith(" "))
        .map(line -> LineToPerson(line))
        .forEach(System.out::println);
}

with

try (Stream<String> lines = Files.lines(path);) {
    lines.filter(line -> line.startsWith(" "))
        .map(line -> LineToPerson(line))
        .collect(Collectors.groupingBy(person -> person.getJob(),
                                Collectors.summingDouble(person -> person.getSalary())))
        .forEach((job,sumSalary) -> System.out.println(job + "\t" + sumSalary));
}
Related