Advertisement
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 _4_Orders {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- int count = scan.nextInt();
- scan.nextLine();
- Map<String, TreeMap<String, Integer>> orders = new LinkedHashMap<>();
- for (int i = 0; i < count; i++) {
- String[] inputs = scan.nextLine().split("[^A-Za-z0-9]+");
- String product = inputs[2];
- String customer = inputs[0];
- Integer amount = Integer.parseInt(inputs[1]);
- if (!orders.containsKey(product)) {
- orders.put(product, new TreeMap<String, Integer>());
- }
- TreeMap<String, Integer> amounts = orders.get(product);
- int previousAmount = 0;
- if (amounts.containsKey(customer)) {
- previousAmount = amounts.get(customer);
- }
- amounts.put(customer, previousAmount + amount);
- }
- for (String product : orders.keySet()) {
- System.out.print(product + ": ");
- TreeMap<String, Integer> amounts = orders.get(product);
- boolean first = true;
- for (Map.Entry<String, Integer> pair : amounts.entrySet()) {
- if (!first) {
- System.out.print(", ");
- }
- first = false;
- String customer = pair.getKey();
- int amount = pair.getValue();
- System.out.print(customer + " " + amount);
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement