Advertisement
desislava_topuzakova

01. Santa's Present Factory

Jun 10th, 2023
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. package examPreparation;
  2.  
  3. import org.w3c.dom.ls.LSOutput;
  4.  
  5. import java.util.*;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Task1 {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. String materialsInput = scanner.nextLine(); //"10 -5 20 15 -30 10".split(" ") -> ["10", "-5", "20"...]
  13. String magicsInput = scanner.nextLine(); //"40 60 10 4 10 0"
  14.  
  15. Stack<Integer> materials = new Stack<>();
  16. Arrays.stream(materialsInput.split("\\s+"))
  17. .map(Integer::parseInt).forEach(materials::push);
  18.  
  19. Queue<Integer> magics = Arrays.stream(magicsInput.split("\\s+"))
  20. .map(Integer::parseInt)
  21. .collect(Collectors.toCollection(ArrayDeque::new));
  22.  
  23. //играчка -> бр. произведени
  24. Map<String, Integer> presentsMap = new TreeMap<>();
  25. presentsMap.put("Doll", 0);
  26. presentsMap.put("Wooden train", 0);
  27. presentsMap.put("Teddy bear", 0);
  28. presentsMap.put("Bicycle", 0);
  29.  
  30. while (!materials.isEmpty() && !magics.isEmpty()) {
  31. //първия материал
  32. int material = materials.peek();
  33. int magic = magics.peek();
  34.  
  35. int totalMagic = magic * material; //получената магия
  36.  
  37. //проверка за получената магия
  38. if (totalMagic == 150) {
  39. //кукла
  40. materials.pop();
  41. magics.poll();
  42. presentsMap.put("Doll", presentsMap.get("Doll") + 1);
  43. } else if (totalMagic == 250) {
  44. //влакче
  45. materials.pop();
  46. magics.poll();
  47. presentsMap.put("Wooden train", presentsMap.get("Wooden train") + 1);
  48. } else if (totalMagic == 300) {
  49. //мече
  50. materials.pop();
  51. magics.poll();
  52. presentsMap.put("Teddy bear", presentsMap.get("Teddy bear") + 1);
  53. } else if (totalMagic == 400) {
  54. //колело
  55. materials.pop();
  56. magics.poll();
  57. presentsMap.put("Bicycle", presentsMap.get("Bicycle") + 1);
  58. } else if (totalMagic < 0) {
  59. int sum = material + magic;
  60. materials.pop();
  61. magics.poll();
  62. materials.push(sum);
  63. } else if (totalMagic > 0) {
  64. magics.poll();
  65. materials.push(materials.pop() + 15);
  66. } else {
  67. if (magic == 0) {
  68. magics.poll();
  69. }
  70. if(material == 0) {
  71. materials.pop();
  72. }
  73. }
  74. }
  75.  
  76. //проверка дали сме произвели нужните подаръци
  77. //1. кукла и влакче
  78. //2. мече и колело
  79. boolean isDollAndTrain = presentsMap.get("Doll") > 0 && presentsMap.get("Wooden train") > 0;
  80. boolean isBearAndBicycle = presentsMap.get("Teddy bear") > 0 && presentsMap.get("Bicycle") > 0;
  81. if (isDollAndTrain || isBearAndBicycle) {
  82. //справили сме се с изработката
  83. System.out.println("The presents are crafted! Merry Christmas!");
  84. } else {
  85. //не сме се справили с изработката
  86. System.out.println("No presents this Christmas!");
  87. }
  88.  
  89. //останалите материали
  90. if (!materials.isEmpty()) {
  91. System.out.print("Materials left: ");
  92. //[3, 4, 5]
  93. Collections.reverse(materials);
  94. System.out.println(materials.toString().replace("[", "").replace("]", ""));
  95. }
  96.  
  97. //останалите магии
  98. if (!magics.isEmpty()) {
  99. System.out.print("Magic left: ");
  100. System.out.println(magics.toString().replace("[", "").replace("]", ""));
  101. }
  102.  
  103. //отпечатваме: играчка: брой > 0
  104. for (Map.Entry<String, Integer> entry : presentsMap.entrySet()) {
  105. //entry
  106. //key: играчка
  107. //value: бройка
  108. if (entry.getValue() > 0) {
  109. System.out.println(entry.getKey() + ": " + entry.getValue());
  110. }
  111. }
  112.  
  113. }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement