Advertisement
dezlatanov

Untitled

Feb 21st, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Demo {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. List<Integer> materials = Arrays
  9. .stream(scanner.nextLine().split("\\s+"))
  10. .map(Integer::parseInt)
  11. .collect(Collectors.toList());
  12. List<Integer> magic = Arrays
  13. .stream(scanner.nextLine().split("\\s+"))
  14. .map(Integer::parseInt)
  15. .collect(Collectors.toList());
  16.  
  17. Map<String, Integer> toys = new TreeMap<>();
  18.  
  19. while (!materials.isEmpty() && !magic.isEmpty()) {
  20. int materialsForCrafting = materials.get(materials.size() - 1);
  21. int magicForCrafting = magic.get(0);
  22. int craftedToysValue = materialsForCrafting * magicForCrafting;
  23.  
  24. if (craftedToysValue < 0) {
  25. int newMaterial = (magicForCrafting + materialsForCrafting);
  26. removeParts(materials,magic);
  27. materials.add(newMaterial);
  28. continue;
  29. }
  30. if (materialsForCrafting == 0) {
  31. materials.remove(materials.get(materials.size()-1));
  32. continue;
  33. }
  34. if (magicForCrafting == 0) {
  35. magic.remove(magic.remove(0));
  36. continue;
  37. }
  38. switch (craftedToysValue) {
  39. case 150:
  40. addToys(toys, "Doll");
  41. removeParts(materials, magic);
  42. break;
  43. case 250:
  44. addToys(toys, "Wooden Train");
  45. removeParts(materials, magic);
  46. break;
  47. case 300:
  48. addToys(toys, "Teddy bear");
  49. removeParts(materials, magic);
  50. break;
  51. case 400:
  52. addToys(toys, "Bicycle");
  53. removeParts(materials, magic);
  54. break;
  55. default:
  56. magic.remove(magic.get(0));
  57. int newMatForAdding = materialsForCrafting +15;
  58. materials.remove(materials.get(materials.size()-1));
  59. materials.add(newMatForAdding);
  60. //• If the product doesn’t equal one of the magic levels in the table and is a positive number,
  61. // remove only the magic value and increase the material value with 15.
  62. break;
  63. }
  64.  
  65.  
  66. }
  67. if (successValidation(toys)) {
  68. System.out.println("The presents are crafted! Merry Christmas!");
  69. } else {
  70. System.out.println("No presents this Christmas!");
  71. }
  72. if (!materials.isEmpty()) {
  73. System.out.print("Materials left: ");
  74. for (int i = materials.size()-1; i >= 0 ; i--) {
  75. if(i != 0){
  76. System.out.print(materials.get(i) + ", ");
  77. }else{
  78. System.out.print(materials.get(i) );
  79. }
  80.  
  81. }
  82. System.out.println();
  83. }
  84. if (!magic.isEmpty()) {
  85.  
  86.  
  87. }
  88. toys.entrySet()
  89. .stream()
  90. .forEach(e -> System.out.printf("%s: %d%n", e.getKey(), e.getValue()));
  91. }
  92. private static void removeParts(List<Integer> materials, List<Integer> magic) {
  93. magic.remove(0);
  94. materials.remove(materials.size() - 1);
  95. }
  96.  
  97. private static void addToys(Map<String, Integer> toys, String currentPresent) {
  98. if (toys.containsKey(currentPresent)) {
  99. int count = toys.get(currentPresent);
  100. toys.put(currentPresent, count + 1);
  101. } else {
  102. toys.put(currentPresent, 1);
  103. }
  104. }
  105. private static boolean successValidation(Map<String, Integer> toys) {
  106. if (toys.containsKey("Teddy bear") && toys.containsKey("Bicycle")) {
  107. return true;
  108. } else if (toys.containsKey("Doll") && toys.containsKey("Wooden train")) {
  109. return true;
  110. }
  111. return false;
  112. }
  113.  
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement