Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.stream.Collectors;
- public class FeedTheAnimalsClass {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String line = reader.readLine();
- Map<String, Animal> animals = new HashMap<>();
- while (!("Last Info").equals(line)) {
- String[] cmdArgs = line.split(":");
- String cmd = cmdArgs[0];
- String name = cmdArgs[1];
- String area = cmdArgs[3];
- if ("Add".equals(cmd)) {
- int dailyFoodLimit = Integer.parseInt(cmdArgs[2]);
- if (animals.containsKey(name)) {
- Animal animal = animals.get(name);
- animal.setDailyFood(animal.getDailyFood() + dailyFoodLimit);
- animals.put(name, animal);
- } else {
- animals.put(name, new Animal(name, area, dailyFoodLimit));
- }
- } else if ("Feed".equals(cmd)) {
- int food = Integer.parseInt(cmdArgs[2]);
- if (animals.containsKey(name)) {
- int value = animals.get(name).getDailyFood();
- if (value - food <= 0) {
- animals.remove(name);
- System.out.printf("%s was successfully fed%n", name);
- } else {
- Animal animal = animals.get(name);
- animal.setDailyFood(animal.getDailyFood() - food);
- animals.put(name, animal);
- }
- }
- }
- line = reader.readLine();
- }
- if (animals.size() > 0) {
- System.out.printf("Animals:%n");
- animals.entrySet().stream().sorted((e1,e2) -> Integer.compare(e2.getValue().getDailyFood(),e1.getValue().getDailyFood())).forEach(e -> {
- System.out.printf("%s -> %dg%n", e.getValue().getName(), e.getValue().getDailyFood());
- });
- System.out.printf("Areas with hungry animals:%n");
- // To add: Make map automatically with stream with key area and value the count of animals with this area -> (count)
- Map<String, Integer> areas = new HashMap<>();
- for (Map.Entry<String, Animal> animalEntry : animals.entrySet()) {
- String area = animalEntry.getValue().getArea();
- areas.putIfAbsent(area, 0);
- areas.put(area, areas.get(area) + 1);
- }
- areas.entrySet().stream().sorted((a,b) -> b.getValue().compareTo(a.getValue())).forEach(e -> {
- System.out.printf("%s : %d%n",e.getKey(),e.getValue());
- });
- }
- }
- static class Animal {
- private String name;
- private String area;
- private int dailyFood;
- public Animal(String name, String area, int dailyFood) {
- this.name = name;
- this.area = area;
- this.dailyFood = dailyFood;
- }
- public String getName() {
- return this.name;
- }
- public String getArea() {
- return this.area;
- }
- public int getDailyFood() {
- return this.dailyFood;
- }
- public void setDailyFood(int dailyFood) {
- this.dailyFood = dailyFood;
- }
- }
- }
- class Animal {
- private String name;
- private String area;
- private int dailyFood;
- public Animal(String name, String area, int dailyFood) {
- this.name = name;
- this.area = area;
- this.dailyFood = dailyFood;
- }
- public String getName() {
- return this.name;
- }
- public String getArea() {
- return this.area;
- }
- public int getDailyFood() {
- return this.dailyFood;
- }
- public void setDailyFood(int dailyFood) {
- this.dailyFood = dailyFood;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement