Viksy

Wild-Zoo

Apr 3rd, 2022 (edited)
1,162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class WildZoo {
  4.     public static void main(String[] args){
  5.         Scanner scanner = new Scanner(System.in);
  6.         HashMap<String, HashMap<Integer, String>> animalsMap = new HashMap<>();
  7.  
  8.         String input = "";
  9.         while(!input.equals("EndDay")){
  10.             input = scanner.nextLine();
  11.  
  12.             if(input.contains("Add:")){
  13.                 String newInput = input.replace("Add: ", "");
  14.                 String[] splitInput = newInput.split("-");
  15.  
  16.                 HashMap<Integer, String> tempMap = new HashMap<>();
  17.                 tempMap.put(Integer.parseInt(splitInput[1]), splitInput[2]);
  18.  
  19.                 if(animalsMap.containsKey(splitInput[0])){
  20.                     int oldAmount = 0;
  21.  
  22.                     for(Map.Entry<Integer, String> entry : animalsMap.get(splitInput[0]).entrySet()){
  23.                         oldAmount = entry.getKey();
  24.                     }
  25.  
  26.                     animalsMap.get(splitInput[0]).remove(oldAmount);
  27.                     animalsMap.get(splitInput[0]).put(Integer.parseInt(splitInput[1]) + oldAmount, splitInput[2]);
  28.                 } else{
  29.                     animalsMap.put(splitInput[0],tempMap);
  30.                 }
  31.             }
  32.  
  33.             if(input.contains("Feed:")){
  34.                 String newInput = input.replace("Feed: ", "");
  35.                 String[] splitInput = newInput.split("-");
  36.  
  37.                 if(animalsMap.containsKey(splitInput[0])){
  38.                     int oldAmount = 0;
  39.                     String oldArea = "";
  40.  
  41.                     for(Map.Entry<Integer, String> entry : animalsMap.get(splitInput[0]).entrySet()){
  42.                         oldAmount = entry.getKey();
  43.                         oldArea = entry.getValue();
  44.                     }
  45.  
  46.                     animalsMap.get(splitInput[0]).remove(oldAmount);
  47.                     animalsMap.get(splitInput[0]).put(oldAmount - Integer.parseInt(splitInput[1]), oldArea);
  48.  
  49.                     for(Map.Entry<Integer, String> entry : animalsMap.get(splitInput[0]).entrySet()){
  50.                         if(entry.getKey() <= 0){
  51.  
  52.                             for(String s : animalsMap.keySet()){
  53.                                 if(s.equals(splitInput[0])){
  54.                                     System.out.println(s + " was successfully fed");
  55.                                 }
  56.                             }
  57.                             animalsMap.remove(splitInput[0]);
  58.                         }
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.  
  64.         System.out.println("Animals:");
  65.         for(String s : animalsMap.keySet()){
  66.             for(Map.Entry<Integer, String> entry : animalsMap.get(s).entrySet()){
  67.                 System.out.println(" " + s + " -> " + entry.getKey() + "g");
  68.             }
  69.         }
  70.  
  71.         HashMap<String, Integer> areas = new HashMap<>();
  72.         for(String s : animalsMap.keySet()){
  73.             for(Map.Entry<Integer, String> entry : animalsMap.get(s).entrySet()){
  74.                 if(areas.containsKey(entry.getValue())){
  75.                     int curr = areas.get(entry.getValue());
  76.                     curr++;
  77.                     areas.replace(entry.getValue(), curr);
  78.                 } else{
  79.                     areas.put(entry.getValue(), 1);
  80.                 }
  81.             }
  82.         }
  83.  
  84.         System.out.println("Areas with hungry animals:");
  85.         for(String s : areas.keySet()){
  86.             System.out.println(" " + s + ": " + areas.get(s));
  87.         }
  88.     }
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment