Advertisement
meteor4o

JF-Exams-FeedTheAnimals

Aug 1st, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class FeedTheAnimals {
  6.     public static void main(String[] args) {
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         Map<String, Integer> animalFoodLimit = new HashMap<>();
  10.         Map<String, Integer> animalArea = new LinkedHashMap<>();
  11.  //       Map<String, HashMap<String, Integer>> animalsFoodArea = new HashMap<>();
  12.  
  13.         String name = "";
  14.         int food = 0;
  15.         String area = "";
  16.         int counter = 0;
  17.  
  18.         String input = sc.nextLine();
  19.  
  20.         while (!input.equals("Last Info")) {
  21.             String[] tokens = input.split(":");
  22.             name = tokens[1];
  23.             food = Integer.parseInt(tokens[2]);
  24.             area = tokens[3];
  25.  
  26.             if (tokens[0].equals("Add")) {
  27.                 if (!animalFoodLimit.containsKey(name)) {
  28.  
  29.                     animalFoodLimit.put(name, food);
  30.                     if (!animalArea.containsKey(area)) {
  31.                         animalArea.put(area, 1);
  32.                     } else {
  33.                         int areaCount = animalArea.get(area);
  34.                         animalArea.put(area, areaCount + 1);
  35.                     }
  36.                 } else {
  37.                     int currFood = animalFoodLimit.get(name)+ food;
  38.                     animalFoodLimit.put(name, currFood);
  39.  
  40.                 }
  41.             } else if (tokens[0].equals("Feed")) {
  42.                 if (animalFoodLimit.containsKey(name)) {
  43.                     int currFood = animalFoodLimit.get(name);
  44.                     animalFoodLimit.put(name, currFood - food);
  45.                         if (currFood - food <= 0) {
  46.                             animalFoodLimit.remove(name);
  47.                             System.out.printf("%s was successfully fed%n", name);
  48.  
  49.                                 if (animalArea.get(area) > 1) {
  50.                                     int areaValue = animalArea.get(area);
  51.                                     animalArea.put(area, areaValue - 1);
  52.                                 } else {
  53.                                     animalArea.remove(area);
  54.                                 }
  55.                         }
  56.                     }
  57. //                if (animalsFoodArea.containsKey(name)) {
  58. //                    int currFood = animalsFoodArea.get(name).get(area);
  59. //                    if (currFood - food <= 0) {
  60. //                        animalsFoodArea.remove(name);
  61. //                        hungryAnimals.remove(name);
  62. //                        System.out.printf("%s was successfully fed%n", name);
  63. //                    } else {
  64. //                        animalsFoodArea.get(name).put(area, currFood - food);
  65. //                    }
  66. //                }
  67.             }
  68.  
  69.             input = sc.nextLine();
  70.         }
  71.         System.out.println("Animals: ");
  72.         animalFoodLimit.entrySet().stream().sorted((f, s) -> {
  73.             int result = s.getValue() - f.getValue();
  74.             if (result == 0) {
  75.                 result = f.getKey().compareTo(s.getKey());
  76.             }
  77.             return result;
  78.         })
  79.         .forEach(e -> {
  80.             System.out.printf("%s -> %dg%n", e.getKey(), e.getValue());
  81.         });
  82.         System.out.println("Areas with hungry animals:");
  83.         animalArea.entrySet().stream().sorted((f,s) ->
  84.             s.getValue().compareTo(f.getValue()))
  85.                 .forEach(entry -> System.out.printf("%s : %d%n", entry.getKey(), entry.getValue()));
  86.  
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement