Advertisement
deyanmalinov

03. Legendary Farming

Mar 18th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package DPM;
  2.  
  3. import java.util.Comparator;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. public class Main {
  9.     public static void main(String[] args) {
  10.         Scanner scan = new Scanner(System.in);
  11.         Map<String, Integer> keyMaterial = new LinkedHashMap<>();
  12.  
  13.         keyMaterial.put("shards", 0);
  14.         keyMaterial.put("fragments", 0);
  15.         keyMaterial.put("motes", 0);
  16.  
  17.         Map<String, Integer> junk = new LinkedHashMap<>();
  18.         String winer = "";
  19.  
  20.         boolean flag = false;
  21.         do {
  22.             String[] data = scan.nextLine().split(" ");
  23.             for (int i = 0; i < data.length; i += 2) {
  24.                 int quant = Integer.parseInt(data[i]);
  25.                 String material = data[i + 1].toLowerCase();
  26.                 if (!keyMaterial.containsKey(material)) {
  27.                     if (!junk.containsKey(material)) {
  28.                         junk.put(material, quant);
  29.                     } else {
  30.                         junk.put(material, junk.get(material) + quant);
  31.                     }
  32.                 } else {
  33.                     keyMaterial.put(material, keyMaterial.get(material) + quant);
  34.                     if (keyMaterial.get(material) >= 250) {
  35.                         keyMaterial.put(material, keyMaterial.get(material) - 250);
  36.                         winer = material;
  37.                         flag = true;
  38.                         break;
  39.                     }
  40.                 }
  41.             }
  42.         } while (!flag);
  43.  
  44.         if (winer.equals("shards")){
  45.             System.out.println("Shadowmourne obtained!");
  46.         }else if (winer.equals("motes")){
  47.             System.out.println("Dragonwrath obtained!");
  48.         }else if (winer.equals("fragments")){
  49.             System.out.println("Valanyr obtained!");
  50.         }
  51.         keyMaterial.entrySet().stream().sorted((e1,e2) ->{
  52.             int sort = Integer.compare(e2.getValue(), e1.getValue());
  53.             if (sort == 0){
  54.                 sort = e1.getKey().compareTo(e2.getKey());
  55.             }
  56.             return sort;
  57.         }).forEach(e -> {
  58.             System.out.println(String.format(
  59.                     "%s: %d", e.getKey(), e.getValue()
  60.             ));
  61.         });
  62.         junk.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
  63.                 .forEach(e -> {
  64.                     System.out.println(String.format("%s: %d", e.getKey(), e.getValue()));
  65.                 });
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement