Advertisement
Guest User

Untitled

a guest
Nov 10th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class Problem4OfficeStuff {
  9. public static void main(String[] args) {
  10. Scanner sc = new Scanner(System.in);
  11. Pattern pattern = Pattern.compile("([a-zA-Z]+) - ([0-9]+) - ([a-zA-Z]+)");
  12. TreeMap<String,LinkedHashMap<String,Integer>> hold = new TreeMap<String,LinkedHashMap<String,Integer>>();
  13. int count = Integer.parseInt(sc.nextLine());
  14. for (int i = 0; i < count; i++) {
  15. String input = sc.nextLine();
  16. Matcher match = pattern.matcher(input);
  17. while (match.find()){
  18. String company = match.group(1);
  19. int amount = Integer.parseInt(match.group(2));
  20. String product = match.group(3);
  21.  
  22. if (!hold.containsKey(company)){
  23. hold.put(company,new LinkedHashMap<>());
  24. }
  25. if (!hold.get(company).containsKey(product)){
  26. hold.get(company).put(product, amount);
  27. }
  28. else {
  29. int oldValue = hold.get(company).get(product);
  30. hold.get(company).put(product,(amount+oldValue));
  31. }
  32.  
  33. }
  34. }
  35. for (Map.Entry<String, LinkedHashMap<String, Integer>> entry : hold.entrySet()) {
  36. System.out.print(entry.getKey()+": ");
  37. StringBuffer sb = new StringBuffer();
  38. for (Map.Entry<String, Integer> in : entry.getValue().entrySet()) {
  39. sb.append(in.getKey());
  40. sb.append("-");
  41. sb.append(in.getValue());
  42. sb.append(", ");
  43.  
  44. }
  45. String output = sb.toString();
  46. output = output.substring(0,output.length()-2);
  47. System.out.println(output);
  48. }
  49.  
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement