The result is good but codegym tests do not pass..
package pl.codegym.task.task19.task1919;
/*
Obliczanie wynagrodzeń
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import static java.util.Map.Entry.comparingByValue;
import static java.util.stream.Collectors.toMap;
public class Solution {
public static void main(String[] args) throws IOException {
String file = args[0];
// "C:/Users/CEM/Desktop/input.txt"
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
List<String> strings = new ArrayList<>();
List<Double> doublesList = new ArrayList<>();
while ((line = bufferedReader.readLine()) != null) {
// line = line.replaceAll("[^a-zA-Z0-9]", "");
String doubles = line.replaceAll("[^\\d.]", "");
if(line.isEmpty()){
continue;
}
strings.add(line);
doublesList.add(Double.valueOf(doubles));
}
// doublesList.forEach(System.out::println);
List<String> stringList = new ArrayList<>();
for (String string : strings) {
string = string.replaceAll("[0-9]", "");
string = string.replaceAll("[^a-zA-Z0-9]", "");
stringList.add(string);
}
// Collections.sort(stringList);
// Collections.sort(doublesList);
Map<String, Double> listMap = new HashMap<>();
for (int i = 0; i < stringList.size(); i++) {
Double sum = listMap.get(stringList.get(i));
if (sum == null)
sum = doublesList.get(i);
else
sum += doublesList.get(i);
listMap.put(stringList.get(i), sum);
}
Map<String, Double> sorted = listMap
.entrySet()
.stream()
.sorted(comparingByValue())
.collect(
toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
LinkedHashMap::new));
// TreeMap<String, Double> sortedMap = new TreeMap<>(listMap);
// Set<Map.Entry<String, Double>> mappings = sortedMap.entrySet();
// mappings.stream().map(mapping -> mapping.getKey() + " " + mapping.getValue()).forEach(System.out::println);
sorted.forEach((key, value) ->
{
System.out.println( key + " " +value);
});
bufferedReader.close();
}
}