Advertisement
emodev

Untitled

Apr 18th, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Comparator;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import java.util.stream.Collectors;
  8.  
  9. public class FeedTheAnimalsClass {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         String line = reader.readLine();
  14.  
  15.         Map<String, Animal> animals = new HashMap<>();
  16.  
  17.         while (!("Last Info").equals(line)) {
  18.             String[] cmdArgs = line.split(":");
  19.             String cmd = cmdArgs[0];
  20.             String name = cmdArgs[1];
  21.             String area = cmdArgs[3];
  22.  
  23.             if ("Add".equals(cmd)) {
  24.                 int dailyFoodLimit = Integer.parseInt(cmdArgs[2]);
  25.                 if (animals.containsKey(name)) {
  26.                     Animal animal = animals.get(name);
  27.                     animal.setDailyFood(animal.getDailyFood() + dailyFoodLimit);
  28.                     animals.put(name, animal);
  29.                 } else {
  30.                     animals.put(name, new Animal(name, area, dailyFoodLimit));
  31.                 }
  32.  
  33.             } else if ("Feed".equals(cmd)) {
  34.                 int food = Integer.parseInt(cmdArgs[2]);
  35.  
  36.                 if (animals.containsKey(name)) {
  37.                     int value = animals.get(name).getDailyFood();
  38.                     if (value - food <= 0) {
  39.                         animals.remove(name);
  40.                         System.out.printf("%s was successfully fed%n", name);
  41.                     } else {
  42.                         Animal animal = animals.get(name);
  43.                         animal.setDailyFood(animal.getDailyFood() - food);
  44.                         animals.put(name, animal);
  45.                     }
  46.                 }
  47.             }
  48.  
  49.  
  50.             line = reader.readLine();
  51.  
  52.         }
  53.  
  54.  
  55.         if (animals.size() > 0) {
  56.             System.out.printf("Animals:%n");
  57.  
  58.             animals.entrySet().stream().sorted((e1,e2) -> Integer.compare(e2.getValue().getDailyFood(),e1.getValue().getDailyFood())).forEach(e -> {
  59.                 System.out.printf("%s -> %dg%n", e.getValue().getName(), e.getValue().getDailyFood());
  60.             });
  61.  
  62.             System.out.printf("Areas with hungry animals:%n");
  63.  
  64.  
  65. //            To add: Make map automatically with stream with key area and value the count of animals with this area -> (count)
  66.  
  67.  
  68.             Map<String, Integer> areas = new HashMap<>();
  69.             for (Map.Entry<String, Animal> animalEntry : animals.entrySet()) {
  70.                 String area = animalEntry.getValue().getArea();
  71.                 areas.putIfAbsent(area, 0);
  72.                 areas.put(area, areas.get(area) + 1);
  73.             }
  74.  
  75.             areas.entrySet().stream().sorted((a,b) -> b.getValue().compareTo(a.getValue())).forEach(e -> {
  76.                 System.out.printf("%s : %d%n",e.getKey(),e.getValue());
  77.             });
  78.  
  79.         }
  80.     }
  81.  
  82.  
  83.     static class Animal {
  84.         private String name;
  85.         private String area;
  86.         private int dailyFood;
  87.  
  88.         public Animal(String name, String area, int dailyFood) {
  89.             this.name = name;
  90.             this.area = area;
  91.             this.dailyFood = dailyFood;
  92.         }
  93.  
  94.         public String getName() {
  95.             return this.name;
  96.         }
  97.  
  98.         public String getArea() {
  99.             return this.area;
  100.         }
  101.  
  102.         public int getDailyFood() {
  103.             return this.dailyFood;
  104.         }
  105.  
  106.         public void setDailyFood(int dailyFood) {
  107.             this.dailyFood = dailyFood;
  108.         }
  109.     }
  110. }
  111.  
  112.  
  113. class Animal {
  114.     private String name;
  115.     private String area;
  116.     private int dailyFood;
  117.  
  118.     public Animal(String name, String area, int dailyFood) {
  119.         this.name = name;
  120.         this.area = area;
  121.         this.dailyFood = dailyFood;
  122.     }
  123.  
  124.     public String getName() {
  125.         return this.name;
  126.     }
  127.  
  128.     public String getArea() {
  129.         return this.area;
  130.     }
  131.  
  132.     public int getDailyFood() {
  133.         return this.dailyFood;
  134.     }
  135.  
  136.     public void setDailyFood(int dailyFood) {
  137.         this.dailyFood = dailyFood;
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement