borovaneca

ApocalypsePreparation

Jun 12th, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.22 KB | None | 0 0
  1. package Advance.Exams.JavaAdvancedExam.February2023;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayDeque;
  7. import java.util.Arrays;
  8. import java.util.HashMap;
  9. import java.util.stream.Collectors;
  10.  
  11. public class ApocalypsePreparation {
  12.  
  13.     private static final int PATCH_REQUIREMENT = 30;
  14.     private static final int BANDAGE_REQUIREMENT = 40;
  15.     private static final int MEDKIT_REQUIREMENT = 100;
  16.     private static final String PATCH = "Patch";
  17.     private static final String BANDAGE = "Bandage";
  18.     private static final String MEDKIT = "MedKit";
  19.     public static void main(String[] args) throws IOException {
  20.         BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
  21.  
  22.         ArrayDeque<Integer> textiles = new ArrayDeque<>(Arrays.stream(scanner.readLine().split("\\s")).map(Integer::parseInt).collect(Collectors.toList()));
  23.         ArrayDeque<Integer> medicaments = new ArrayDeque<>();
  24.         int[] input = Arrays.stream(scanner.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  25.         Arrays.stream(input).forEach(medicaments::push);
  26.         HashMap<String, Integer> items = new HashMap<>();
  27.  
  28.  
  29.         while (!textiles.isEmpty() && !medicaments.isEmpty()) {
  30.             int textile = textiles.poll();
  31.             int medicament = medicaments.pop();
  32.             int sum = textile + medicament;
  33.             if (sum == PATCH_REQUIREMENT) {
  34.                 items.putIfAbsent(PATCH, 0);
  35.                 items.put(PATCH, items.get(PATCH) + 1);
  36.  
  37.             } else if (sum == BANDAGE_REQUIREMENT) {
  38.                 items.putIfAbsent(BANDAGE, 0);
  39.                 items.put(BANDAGE, items.get(BANDAGE) + 1);
  40.  
  41.             } else if (sum == MEDKIT_REQUIREMENT) {
  42.                 items.putIfAbsent(MEDKIT, 0);
  43.                 items.put(MEDKIT, items.get(MEDKIT) + 1);
  44.  
  45.             } else if (sum > MEDKIT_REQUIREMENT) {
  46.                 items.putIfAbsent(MEDKIT, 0);
  47.                 items.put(MEDKIT, items.get(MEDKIT) + 1);
  48.                 sum -= 100;
  49.                 int addReamaining = medicaments.pop() + sum;
  50.                 medicaments.push(addReamaining);
  51.  
  52.             } else {
  53.                 medicaments.push(medicament + 10);
  54.             }
  55.         }
  56.  
  57.         if (textiles.isEmpty() && medicaments.isEmpty()) {
  58.             System.out.println("Textiles and medicaments are both empty.");
  59.         } else if (textiles.isEmpty()){
  60.             System.out.println("Textiles are empty.");
  61.         } else {
  62.             System.out.println("Medicaments are empty.");
  63.         }
  64.  
  65.         items.entrySet().stream()
  66.                 .sorted((name1, name2) -> name1.getKey().compareTo(name2.getKey()))
  67.                 .sorted((n1, n2) -> Integer.compare(n2.getValue(), n1.getValue()))
  68.                 .forEach(item -> System.out.printf("%s - %d%n", item.getKey(), item.getValue()));
  69.  
  70.         if (!medicaments.isEmpty()) {
  71.             System.out.println("Medicaments left: " + String.join(", ", medicaments.toString().replaceAll("[\\[\\]]", "")));
  72.         } else if (!textiles.isEmpty()) {
  73.             System.out.println("Textiles left: " + String.join(", ", textiles.toString().replaceAll("[\\[\\]]", "")));
  74.         }
  75.     }
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment