Advertisement
marior333

Wild Zoo

Apr 5th, 2022
1,151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. package FundamentalsFinalExam;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class Zoo {
  8.  
  9.     static class Animal {
  10.         String name;
  11.         int food;
  12.         String area;
  13.  
  14.         public Animal(String name, int food, String area) {
  15.             this.name = name;
  16.             this.food = food;
  17.             this.area = area;
  18.         }
  19.  
  20.         public String getName() {
  21.             return name;
  22.         }
  23.  
  24.         public void setName(String name) {
  25.             this.name = name;
  26.         }
  27.  
  28.         public int getFood() {
  29.             return food;
  30.         }
  31.  
  32.         public void setFood(int food) {
  33.             this.food = food;
  34.         }
  35.  
  36.         public String getArea() {
  37.             return area;
  38.         }
  39.  
  40.         public void setArea(String area) {
  41.             this.area = area;
  42.         }
  43.     }
  44.  
  45.     public static void main(String[] args) {
  46.         Scanner scanner = new Scanner(System.in);
  47.  
  48.         String input = scanner.nextLine();
  49.  
  50.         Map<String, Animal> mapAnimals = new LinkedHashMap<>();
  51.         Map<String, Integer> hungryAnimals = new LinkedHashMap<>();
  52.  
  53.         while (!input.equals("EndDay")) {
  54.             String[] arr = input.split("[: -]+");
  55.             switch (arr[0]) {
  56.                 case "Add":
  57.                     handleAdd(mapAnimals, arr[1], Integer.parseInt(arr[2]), arr[3], hungryAnimals);
  58.                     break;
  59.                 case "Feed":
  60.                     handleFeed(mapAnimals, arr[1], Integer.parseInt(arr[2]), hungryAnimals);
  61.                     break;
  62.                 default:
  63.                     break;
  64.  
  65.             }
  66.  
  67.  
  68.             input = scanner.nextLine();
  69.         }
  70.  
  71.         System.out.println("Animals:");
  72.         mapAnimals.entrySet().forEach(e -> System.out.println(e.getKey() + " -> " + e.getValue().getFood() + "g"));
  73.         System.out.println("Areas with hungry animals:");
  74.         hungryAnimals.entrySet().forEach(e -> System.out.println(e.getKey() + ": " + e.getValue()));
  75.     }
  76.  
  77.  
  78.     private static void handleAdd(Map<String, Animal> mapAnimals, String name, int foodQuantity, String area, Map<String, Integer> hungryAnimals) {
  79.         if (!mapAnimals.containsKey(name)) {
  80.             Animal animal = new Animal(name, foodQuantity, area);
  81.             mapAnimals.put(name, animal);
  82.         } else {
  83.             int oldFoodQuantity = mapAnimals.get(name).getFood();
  84.             mapAnimals.get(name).setFood(foodQuantity + oldFoodQuantity);
  85.         }
  86.         if (!hungryAnimals.containsKey(area)) {
  87.             hungryAnimals.put(area, 1);
  88.         } else {
  89.             int countValue = hungryAnimals.get(area);
  90.             hungryAnimals.put(area, countValue + 1);
  91.         }
  92.     }
  93.  
  94.     private static void handleFeed(Map<String, Animal> mapAnimals, String name, int foodQuantity, Map<String, Integer> hungryAnimals) {
  95.         Animal animal = mapAnimals.get(name);
  96.         if (mapAnimals.containsKey(name)) {
  97.             int foodNeeded = animal.getFood();
  98.             int feedResult = foodNeeded - foodQuantity;
  99.             if (feedResult > 0) {
  100.                 animal.setFood(feedResult);
  101.             } else {
  102.                 System.out.println(name + " was successfully fed");
  103.                 String livingArea = animal.getArea();
  104.                 int value = hungryAnimals.get(livingArea);
  105.                 hungryAnimals.put(livingArea, value - 1);
  106.                 if (hungryAnimals.get(livingArea) == 0){
  107.                     hungryAnimals.remove(livingArea);
  108.                 }
  109.                 mapAnimals.remove(name);
  110.             }
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement