Advertisement
psi_mmobile

Untitled

Dec 3rd, 2022
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. import java.util.*;
  2. public class MyClass {
  3.     public static void main(String args[]) {
  4.         Scanner scanner = new Scanner(System.in);
  5.         LinkedHashMap<String, LinkedHashMap<String,Double>> animalAreaFoodMap = new LinkedHashMap<String,LinkedHashMap<String, Double>>();
  6.         String nextLine = scanner.nextLine();
  7.         while(true) {
  8.             if(nextLine.equals("EndDay")) {
  9.                 Iterator<Map.Entry<String,LinkedHashMap<String, Double>>> itr = animalAreaFoodMap.entrySet().iterator();
  10.                 while (itr.hasNext()) {
  11.                     Map.Entry pair = (Map.Entry)itr.next();
  12.                     LinkedHashMap<String, Double> areaAndFoodMap = (LinkedHashMap<String, Double>)pair.getValue();
  13.                     Iterator<Map.Entry<String, Double>> areaAndFoodIterator = areaAndFoodMap.entrySet().iterator();
  14.                     Map.Entry areaAndFood = (Map.Entry)areaAndFoodIterator.next();
  15.                     System.out.println(hungryAnimalsInArea(animalAreaFoodMap,areaAndFood.getKey().toString()));
  16.                     System.out.println("Animal:");
  17.                     System.out.printf("(area %s) %s -> %s\n",areaAndFood.getKey().toString(),pair.getKey(),areaAndFood.getValue());
  18.                 }
  19.                 break;
  20.             } else {
  21.                 String[] arrOfStr = nextLine.split("-");
  22.                 if (arrOfStr.length == 3) {
  23.                     if (animalAreaFoodMap.containsKey(arrOfStr[1])) {
  24.                         LinkedHashMap<String, Double> areaAndFoodEntry = animalAreaFoodMap.get(arrOfStr[1]);
  25.                         Iterator<Map.Entry<String, Double>> areaAndFoodOperator = areaAndFoodEntry.entrySet().iterator();
  26.                         Map.Entry areaAndFood = (Map.Entry)areaAndFoodOperator.next();
  27.                         double newFoodQuantity = (double)areaAndFood.getValue() - Double.parseDouble(arrOfStr[2]);
  28.                         areaAndFoodEntry.put(areaAndFood.getKey().toString(), newFoodQuantity);
  29.                         if (newFoodQuantity <= 0) {
  30.                             animalAreaFoodMap.remove(arrOfStr[1]);
  31.                             System.out.printf("%s was successfully fed\n",arrOfStr[1]);
  32.                         }
  33.                     }
  34.                 } else {
  35.                     if (animalAreaFoodMap.containsKey(arrOfStr[1])) {
  36.                         LinkedHashMap<String, Double> areaAndFoodEntry = animalAreaFoodMap.get(arrOfStr[1]);
  37.                         Iterator<Map.Entry<String, Double>> areaAndFoodOperator = areaAndFoodEntry.entrySet().iterator();
  38.                         Map.Entry areaAndFood = (Map.Entry)areaAndFoodOperator.next();
  39.                         double newFoodQuantity = (double)areaAndFood.getValue() + Double.parseDouble(arrOfStr[2]);
  40.                         areaAndFoodEntry.put(areaAndFood.getKey().toString(), newFoodQuantity);
  41.                     } else {
  42.                         LinkedHashMap<String, Double> areaAndFoodEntry = new LinkedHashMap<String, Double>();
  43.                         areaAndFoodEntry.put(arrOfStr[3],Double.parseDouble(arrOfStr[2]));
  44.                         animalAreaFoodMap.put(arrOfStr[1],areaAndFoodEntry);
  45.                     }
  46.                 }
  47.                
  48.             }
  49.             nextLine = scanner.nextLine();
  50.         }
  51.     }
  52.     public static int hungryAnimalsInArea(Map<String, LinkedHashMap<String,Double>> animalAreaFoodMap, String area) {
  53.         int areaAnimalCounter = 0;
  54.         Iterator<Map.Entry<String,LinkedHashMap<String,Double>>> itr = animalAreaFoodMap.entrySet().iterator();
  55.         while (itr.hasNext()) {
  56.             Map.Entry pair = (Map.Entry)itr.next();
  57.             LinkedHashMap<String, Double> areaAndFoodMap = (LinkedHashMap<String,Double>)pair.getValue();
  58.             Iterator<Map.Entry<String,Double>> areaAndFoodIterator = areaAndFoodMap.entrySet().iterator();
  59.             Map.Entry areaAndFood = (Map.Entry)areaAndFoodIterator.next();
  60.             if (areaAndFood.getKey().equals(area)) {
  61.                 areaAnimalCounter++;
  62.             }
  63.         }
  64.         return areaAnimalCounter;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement