Advertisement
Lyubohd

Legendary Farming

Mar 5th, 2020
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.34 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.TreeMap;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         TreeMap<String, Integer> items = new TreeMap<>();
  8.         items.put("shards", 0);
  9.         items.put("motes", 0);
  10.         items.put("fragments", 0);
  11.         TreeMap<String, Integer> junk = new TreeMap<>();
  12.  
  13.         boolean isObtained = false;
  14.         while (!isObtained) { // 6 leathers 255 fragments 7 Shards
  15.             String[] tokens = scan.nextLine().split("\\s+");
  16.             for (int i = 0; i < tokens.length; i += 2) {
  17.                 int count = Integer.parseInt(tokens[i]);
  18.                 String type = tokens[i + 1].toLowerCase();
  19.                 if (items.containsKey(type)) {
  20.                     addItem(items, type, count);
  21.                     isObtained = hasLegendary(items, type);
  22.                     if (isObtained) {
  23.                         break;
  24.                     }
  25.                 } else {
  26.                     addItem(junk, type, count);
  27.                 }
  28.             }
  29.         }
  30.         items
  31.                 .entrySet()
  32.                 .stream()
  33.                 .sorted((i1, i2) -> Integer.compare(i2.getValue(), i1.getValue()))
  34.                 .forEach(i -> System.out.println(String.format("%s: %d", i.getKey(), i.getValue())));
  35.         junk
  36.                 .entrySet()
  37.                 .forEach(j -> System.out.println(String.format("%s: %d", j.getKey(), j.getValue())));
  38.     }
  39.  
  40.     public static boolean hasLegendary(TreeMap<String, Integer> items, String type) {
  41.         int count = items.get(type);
  42.         if (count >= 250) {
  43.             items.put(type, count - 250);
  44.             switch (type) {
  45.                 case "shards":
  46.                     System.out.println("Shadowmourne obtained!");
  47.                     return true;
  48.                 case "fragments":
  49.                     System.out.println("Valanyr obtained!");
  50.                     return true;
  51.                 case "motes":
  52.                     System.out.println("Dragonwrath obtained!");
  53.                     return true;
  54.             }
  55.         }
  56.         return false;
  57.     }
  58.  
  59.     public static void addItem(TreeMap<String, Integer> map, String key, int value) {
  60.         map.putIfAbsent(key, 0);
  61.         int count = map.get(key);
  62.         map.put(key, count + value);
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement