Advertisement
Guest User

gadnataZadacha

a guest
Mar 23rd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. package MapsLambdaAndStreamAPI_Exercise;
  2.  
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. public class LegendaryFarming {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         Map<String, Integer> elements = new HashMap<>();
  13.         elements.put("shards", 0);
  14.         elements.put("fragments", 0);
  15.         elements.put("motes", 0);
  16.         Map<String, Integer> rubbish = new HashMap<>();
  17.  
  18.         boolean end = false;
  19.  
  20.         while (!end) {
  21.             String[] input = scanner.nextLine().split("\\s+");
  22.             for (int i = 0; i < input.length; i += 2) {
  23.                 int quantity = Integer.parseInt(input[i]);
  24.                 String item = input[i + 1].toLowerCase();
  25.  
  26.                 if (item.equalsIgnoreCase("shards")) {
  27.                     elements.put(item, elements.get(item) + quantity);
  28.                     if (elements.get(item) >= 250) {
  29.                         System.out.println("Shadowmourne obtained!");
  30.                         elements.put(item, elements.get(item) - 250);
  31.                         end = true;
  32.                         break;
  33.                     }
  34.                 }
  35.                 if (item.equalsIgnoreCase("fragments")) {
  36.                     elements.put(item, elements.get(item) + quantity);
  37.                     if (elements.get(item) >= 250) {
  38.                         System.out.println("Valanyr obtained!");
  39.                         elements.put(item, elements.get(item) - 250);
  40.                         end = true;
  41.                         break;
  42.                     }
  43.                 }
  44.  
  45.                 if (item.equalsIgnoreCase("motes")) {
  46.                     elements.put(item, elements.get(item) + quantity);
  47.                     if (elements.get(item) >= 250) {
  48.                         System.out.println("Dragonwrath obtained!");
  49.                         elements.put(item, elements.get(item) - 250);
  50.                         end = true;
  51.                         break;
  52.                     }
  53.                 }
  54.  
  55.                 if (!rubbish.containsKey(item)) {
  56.                     rubbish.put(item, quantity);
  57.                 } else {
  58.                     rubbish.put(item, rubbish.get(item) + quantity);
  59.                 }
  60.  
  61.             }
  62.         }
  63.  
  64.         elements.entrySet().stream().sorted(
  65.                 LegendaryFarming::howToCompare
  66.          /*       (left ,right) -> {
  67.                     int leftCount = left.getValue();
  68.                     int righCount = right.getValue();
  69.  
  70.                     if (leftCount != righCount) {
  71.                         if (righCount > leftCount) {
  72.                             return 1;
  73.                         } else {
  74.                             return -1;
  75.                         }
  76.                     } else {
  77.                         String leftElement = left.getKey();
  78.                         String rightElement = right.getKey();
  79.  
  80.                         int weight = leftElement.compareTo(rightElement);
  81.                         return weight;
  82.                     }
  83.                 }*/
  84.         ).forEach(keyValuePair -> {
  85.             System.out.println(keyValuePair.getKey() + ": " + keyValuePair.getValue());
  86.         });
  87.  
  88.         rubbish.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
  89.                 .forEach(keyValuePair -> System.out.println(keyValuePair.getKey() + ": " + keyValuePair.getValue()));
  90.  
  91.     /*    int a = 1;
  92.         int b = 6;
  93.         int c;
  94.         if (a > b) {
  95.             c = 100;
  96.         } else {
  97.             c = 800;
  98.         }
  99.  
  100.         c = a > b ? 100 : 800;
  101.         */
  102.  
  103.       /*  elements.entrySet().stream().sorted((left, right)-> {
  104.             int countWieght = Integer.compare(right.getValue(), left.getValue());
  105.             return countWieght == 0 ? left.getKey().compareTo(right.getKey()) : countWieght;
  106.         });
  107.         */
  108.  
  109.     }
  110.  
  111.     static int howToCompare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) {
  112.  
  113.  
  114.         int leftCount = left.getValue();
  115.         int righCount = right.getValue();
  116.  
  117.         if (leftCount != righCount) {
  118.             if (righCount > leftCount) {
  119.                 return 1;
  120.             } else {
  121.                 return -1;
  122.             }
  123.         } else {
  124.             String leftElement = left.getKey();
  125.             String rightElement = right.getKey();
  126.  
  127.             int weight = leftElement.compareTo(rightElement);
  128.             return weight;
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement