Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.TreeMap;
- public class Orders {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int n = input.nextInt();
- Map<String,Map<String,Integer>> orders = new LinkedHashMap<>();
- for (int i = 0; i < n; i++){
- String name = input.next();
- Integer count = input.nextInt();
- String product = input.next();
- if (!orders.containsKey(product)){
- Map<String,Integer> namesAndCount = new TreeMap<>();
- namesAndCount.put(name, count);
- orders.put(product, namesAndCount);
- }
- else {
- Map<String,Integer> oldNamesAndCount = orders.get(product);
- if (oldNamesAndCount.containsKey(name)){
- oldNamesAndCount.computeIfPresent(name, (k, v) -> v + count);
- }
- else{
- oldNamesAndCount.put(name, count);
- }
- orders.put(product, oldNamesAndCount);
- }
- }
- for(Map.Entry<String,Map<String,Integer>> entry : orders.entrySet()) {
- String product = entry.getKey();
- Map<String,Integer> namesAndCounts = entry.getValue();
- String result = namesAndCounts.toString().replace("{", "").replace("}", "").replaceAll("=", " ");
- System.out.println(product + ": " + result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment