Advertisement
Guest User

SoftUni Java Final Exam - On the Way to Annapurna

a guest
Dec 8th, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4.  
  5. public class OnTheWayToAnnapurna {
  6.     public static void main(String[] args) {
  7.         Scanner input = new Scanner(System.in);
  8.  
  9.         TreeMap<String, ArrayList<String>> diary = new TreeMap<>();
  10.         String expression, command, shop, current;
  11.         List<String> items = new ArrayList<>();
  12.         String[] commands;
  13.  
  14.         while(!(expression = input.nextLine()).equals("END")){
  15.             commands = expression.split("\\-\\>");
  16.             command = commands[0];
  17.             shop = commands[1];
  18.  
  19.             if(command.equals("Add")){
  20.                 current = commands[2];
  21.                 items = Arrays.stream(current.split(",")).collect(Collectors.toList());
  22.             }
  23.  
  24.             switch (command){
  25.                 case "Add":{
  26.                     if(!diary.containsKey(shop)){
  27.                         diary.put(shop, new ArrayList<>());
  28.                     }
  29.  
  30.                     for (int a = 0; a < items.size(); a++) {
  31.                         diary.get(shop).add(items.get(a));
  32.                     }
  33.  
  34.                     break;
  35.                 }
  36.                 case "Remove":{
  37.                     if(diary.containsKey(shop)){
  38.                         diary.remove(shop);
  39.                     }
  40.                     break;
  41.                 }
  42.                 default: break;
  43.             }
  44.         }
  45.  
  46.         System.out.println("Stores list:");
  47.  
  48.         diary
  49.                 .entrySet()
  50.                 .stream()
  51.                 .sorted(Map.Entry.<String, ArrayList<String>>comparingByValue(Comparator.comparing(ArrayList::size)).thenComparing(Map.Entry.comparingByKey()).reversed())
  52.                 .forEach(element -> {
  53.                     System.out.println(element.getKey());
  54.                     element
  55.                             .getValue()
  56.                             .stream()
  57.                             .forEach(store -> System.out.printf("<<%s>>%n", store));
  58.                 });
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement