Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MapsLambdaAndStreamAPI_Exercise;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class LegendaryFarming {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Integer> elements = new HashMap<>();
- elements.put("shards", 0);
- elements.put("fragments", 0);
- elements.put("motes", 0);
- Map<String, Integer> rubbish = new HashMap<>();
- boolean end = false;
- while (!end) {
- String[] input = scanner.nextLine().split("\\s+");
- for (int i = 0; i < input.length; i += 2) {
- int quantity = Integer.parseInt(input[i]);
- String item = input[i + 1].toLowerCase();
- if (item.equalsIgnoreCase("shards")) {
- elements.put(item, elements.get(item) + quantity);
- if (elements.get(item) >= 250) {
- System.out.println("Shadowmourne obtained!");
- elements.put(item, elements.get(item) - 250);
- end = true;
- break;
- }
- }
- if (item.equalsIgnoreCase("fragments")) {
- elements.put(item, elements.get(item) + quantity);
- if (elements.get(item) >= 250) {
- System.out.println("Valanyr obtained!");
- elements.put(item, elements.get(item) - 250);
- end = true;
- break;
- }
- }
- if (item.equalsIgnoreCase("motes")) {
- elements.put(item, elements.get(item) + quantity);
- if (elements.get(item) >= 250) {
- System.out.println("Dragonwrath obtained!");
- elements.put(item, elements.get(item) - 250);
- end = true;
- break;
- }
- }
- if (!rubbish.containsKey(item)) {
- rubbish.put(item, quantity);
- } else {
- rubbish.put(item, rubbish.get(item) + quantity);
- }
- }
- }
- elements.entrySet().stream().sorted(
- LegendaryFarming::howToCompare
- /* (left ,right) -> {
- int leftCount = left.getValue();
- int righCount = right.getValue();
- if (leftCount != righCount) {
- if (righCount > leftCount) {
- return 1;
- } else {
- return -1;
- }
- } else {
- String leftElement = left.getKey();
- String rightElement = right.getKey();
- int weight = leftElement.compareTo(rightElement);
- return weight;
- }
- }*/
- ).forEach(keyValuePair -> {
- System.out.println(keyValuePair.getKey() + ": " + keyValuePair.getValue());
- });
- rubbish.entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey))
- .forEach(keyValuePair -> System.out.println(keyValuePair.getKey() + ": " + keyValuePair.getValue()));
- /* int a = 1;
- int b = 6;
- int c;
- if (a > b) {
- c = 100;
- } else {
- c = 800;
- }
- c = a > b ? 100 : 800;
- */
- /* elements.entrySet().stream().sorted((left, right)-> {
- int countWieght = Integer.compare(right.getValue(), left.getValue());
- return countWieght == 0 ? left.getKey().compareTo(right.getKey()) : countWieght;
- });
- */
- }
- static int howToCompare(Map.Entry<String, Integer> left, Map.Entry<String, Integer> right) {
- int leftCount = left.getValue();
- int righCount = right.getValue();
- if (leftCount != righCount) {
- if (righCount > leftCount) {
- return 1;
- } else {
- return -1;
- }
- } else {
- String leftElement = left.getKey();
- String rightElement = right.getKey();
- int weight = leftElement.compareTo(rightElement);
- return weight;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement