desislava_topuzakova

Untitled

Nov 5th, 2021
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. package Maps;
  2.  
  3. import java.util.*;
  4.  
  5. public class LegendaryFarming_03 {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. //materials -> shards, fragments, motes
  9. //материал -> количество
  10. Map<String, Integer> materials = new TreeMap<>(); //1. fragments 2. motes 3. shards`
  11. materials.put("shards", 0);
  12. materials.put("fragments", 0);
  13. materials.put("motes", 0);
  14. //junk -> others
  15. Map<String, Integer> junks = new TreeMap<>();
  16. boolean isOver = false;
  17. while (!isOver) { //прекъсваме, когато купим item (isOver == true)
  18. String input = scanner.nextLine();
  19. String[] inputData = input.split(" ");
  20. //["3", "Motes", "5", "stones", "5", "Shards"]
  21. for (int index = 0; index < inputData.length; index += 2) { //Обходим всеки ред
  22. int quantity = Integer.parseInt(inputData[index]);
  23. String material = inputData[index + 1].toLowerCase();
  24. //проверка дали материалът е валиден -> shards, fragments, motes
  25. if (material.equals("shards") || material.equals("fragments") || material.equals("motes")) {
  26. int currentQuantity = materials.get(material);
  27. materials.put(material, currentQuantity + quantity);
  28. //проверак дали >= 250
  29. if (materials.get(material) >= 250) {
  30. //печелим item
  31. if (material.equals("shards")) {
  32. System.out.println("Shadowmourne obtained!");
  33. } else if (material.equals("fragments")) {
  34. System.out.println("Valanyr obtained!");
  35. } else { //motes
  36. System.out.println("Dragonwrath obtained!");
  37. }
  38. //намалям количеството на съответния материал с 250
  39. materials.put(material, materials.get(material) - 250);
  40. isOver = true;
  41. break;
  42. }
  43. } else {
  44. //junk
  45. //putIfAbsent -> добавя само ако ключа го няма
  46. if (!junks.containsKey(material)) {
  47. junks.put(material, quantity);
  48. } else {
  49. int currentQuantity = junks.get(material);
  50. junks.put(material, currentQuantity + quantity);
  51. }
  52. }
  53. }
  54. }
  55.  
  56. //сортираме материалите по намаляващ ред (descending order) на количестовото
  57. //entry: material -> quantity
  58. materials.entrySet().stream().sorted((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()))
  59. .forEach(entry -> System.out.printf("%s: %d%n", entry.getKey(), entry.getValue()));
  60.  
  61. junks.entrySet().forEach(entry -> System.out.println(entry.getKey() + ": " + entry.getValue()));
  62.  
  63. }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment