Advertisement
meteor4o

JF-Maps-Exercise-03.LegendaryFarming

Jul 14th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6. import java.util.TreeMap;
  7.  
  8. public class LegendaryFarming {
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.  
  12.         Map<String, Integer> junk = new HashMap<>();
  13.         Map<String, Integer> keyMaterials = new TreeMap<>();
  14.         keyMaterials.put("motes", 0);
  15.         keyMaterials.put("fragments", 0);
  16.         keyMaterials.put("shards", 0);
  17.  
  18.         int[] collectedKeyValues = new int[3];
  19.         boolean itemObtained = false;
  20.  
  21.         while (!itemObtained) {
  22.             String[] tokens = sc.nextLine().split("\\s+");
  23.  
  24.             for (int i = 0; i < tokens.length; i+=2) {
  25.                 int quantity = Integer.parseInt(tokens[i]);
  26.                 String material = tokens[i+1].toLowerCase();
  27.  
  28.                 if (keyMaterials.containsKey(material)) {
  29.                     int value = keyMaterials.get(material) + quantity;
  30.                     keyMaterials.put(material, value);
  31.  
  32.                     if ("motes".equals(material)) {
  33.                         collectedKeyValues[0] += quantity;
  34.                         itemObtained = collectedKeyValues[0] >= 250;
  35.                     } else if ("fragments".equals(material)) {
  36.                         collectedKeyValues[1] += quantity;
  37.                         itemObtained = collectedKeyValues[1] >= 250;
  38.                     } else if ("shards".equals(material)) {
  39.                         collectedKeyValues[2] += quantity;
  40.                         itemObtained = collectedKeyValues[2] >= 250;
  41.                     }
  42.  
  43.                 } else {
  44.                     if (!junk.containsKey(material)) {
  45.                         junk.put(material, quantity);
  46.                     } else {
  47.                         junk.put(material, junk.get(material + quantity));
  48.                     }
  49.                 }
  50.             }
  51.  
  52.         }
  53.  
  54.         String itemName = "";
  55.         if (collectedKeyValues[0] >= 250) {
  56.             int value = keyMaterials.get("motes") - 250;
  57.             keyMaterials.put("motes", 250);
  58.             itemName = "Dragonwrath";
  59.         } else if (collectedKeyValues[1] >= 250) {
  60.             int value = keyMaterials.get("fragments") - 250;
  61.             keyMaterials.put("fragments", 250);
  62.             itemName = "Valanyr";
  63.         } else if (collectedKeyValues[2] >= 250) {
  64.             int value = keyMaterials.get("shard") - 250;
  65.             keyMaterials.put("shards", 250);
  66.             itemName = "Shadowmourne";
  67.         }
  68.  
  69.         System.out.println(itemName + " obtained!");
  70.         keyMaterials.entrySet().stream().sorted((a,b) -> {
  71.  
  72.              int result = Integer.compare(a.getValue(), b.getValue());
  73.                 if (result == 0) {
  74.                 result = a.getKey().compareTo(b.getKey());
  75.             }
  76.                 return result;
  77.             }).forEach(entry ->
  78.                 System.out.println(entry.getKey() + ": " + entry.getValue()));
  79.  
  80.  
  81.         for (Map.Entry<String, Integer> entry : junk.entrySet()) {
  82.             System.out.println(entry.getKey() + ": " + entry.getValue());
  83.         }
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement