· 4 years ago · Mar 14, 2021, 11:50 PM
1Задача 03. Legendary Farming - Maps, Lambda and Stream API - Exercise
2
3You've beaten all the content and the last thing left to accomplish is own a legendary item. However, it's a tedious process and requires quite a bit of farming.
4Anyway, you are not too pretentious - any legendary will do. The possible items are:
5• Shadowmourne - requires 250 Shards
6• Valanyr - requires 250 Fragments
7• Dragonwrath - requires 250 Motes
8Shards, Fragments and Motes are the key materials, all else is junk. You will be given lines of input, such as
9"2 motes 3 ores 15 stones". Keep track of the key materials - the first that reaches the 250 mark wins the race. At that point, print the corresponding legendary obtained. Then, print the remaining shards, fragments, motes, ordered by quantity in descending order, then by name in ascending order, each on a new line. Finally, print the collected junk items, in alphabetical order.
10Input
11• Each line of input is in format {quantity} {material} {quantity} {material} … {quantity} {material}
12Output
13• On the first line, print the obtained item in format {Legendary item} obtained!
14• On the next three lines, print the remaining key materials in descending order by quantity
15o If two key materials have the same quantity, print them in alphabetical order
16• On the final several lines, print the junk items in alphabetical order
17o All materials are printed in format {material}: {quantity}
18o All output should be lowercase, except the first letter of the legendary
19
20package AssociativeArraysLambdaAndStreamAPI.Exercise;
21
22import java.util.*;
23
24public class LegendaryFarming_03 {
25 public static void main(String[] args) {
26 Scanner scanner = new Scanner(System.in);
27
28 String input = scanner.nextLine();
29
30 Map<String, Integer> materialsQuantity = new TreeMap<>();
31 materialsQuantity.put("shards", 0);
32 materialsQuantity.put("fragments", 0);
33 materialsQuantity.put("motes", 0);
34 Map<String, Integer> junkQuantity = new TreeMap<>();
35 boolean isFoundLegendaryItem = false;
36
37 while (!isFoundLegendaryItem){
38 String [] inputData = input.split("\\s+");
39
40 for (int i = 0; i < inputData.length; i = i + 2) {
41 int quantity = Integer.parseInt(inputData[i]);
42 String material = inputData[i + 1].toLowerCase();
43
44 switch (material){
45 case "shards":
46 case "fragments":
47 case "motes":
48 if (!materialsQuantity.containsKey(material)){
49 materialsQuantity.put(material, quantity);
50 } else {
51 materialsQuantity.put(material, materialsQuantity.get(material) + quantity);
52 }
53
54 if (materialsQuantity.get(material) >= 250){
55
56 if (materialsQuantity.get(material) > 250){
57 int difference = materialsQuantity.get(material) - 250;
58 materialsQuantity.put(material, difference);
59 }
60
61 if (material.equals("shards")){
62 System.out.printf("Shadowmourne obtained!%n");
63 isFoundLegendaryItem = true;
64 } else if (material.equals("fragments")){
65 System.out.printf("Valanyr obtained!%n");
66 isFoundLegendaryItem = true;
67 } else if (material.equals("motes")){
68 System.out.printf("Dragonwrath obtained!%n");
69 isFoundLegendaryItem = true;
70 }
71 }
72 break;
73 default:
74 if (!junkQuantity.containsKey(material)){
75 junkQuantity.put(material, quantity);
76 } else {
77 junkQuantity.put(material, junkQuantity.get(material) + quantity);
78 }
79 }
80 if (isFoundLegendaryItem){
81 materialsQuantity.entrySet().stream()
82 .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
83 .forEach(e -> System.out.printf("%s: %d%n", e.getKey(), e.getValue()));
84
85 junkQuantity.entrySet().stream()
86 .sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey()))
87 .forEach(e -> System.out.printf("%s: %d%n", e.getKey(), e.getValue()));
88 break;
89 }
90 }
91 if (isFoundLegendaryItem){
92 break;
93 }
94 input = scanner.nextLine();
95 }
96 }
97}
98