Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- public class FeedTheAnimals {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- Map<String, Integer> animalFoodLimit = new HashMap<>();
- Map<String, Integer> animalArea = new LinkedHashMap<>();
- // Map<String, HashMap<String, Integer>> animalsFoodArea = new HashMap<>();
- String name = "";
- int food = 0;
- String area = "";
- int counter = 0;
- String input = sc.nextLine();
- while (!input.equals("Last Info")) {
- String[] tokens = input.split(":");
- name = tokens[1];
- food = Integer.parseInt(tokens[2]);
- area = tokens[3];
- if (tokens[0].equals("Add")) {
- if (!animalFoodLimit.containsKey(name)) {
- animalFoodLimit.put(name, food);
- if (!animalArea.containsKey(area)) {
- animalArea.put(area, 1);
- } else {
- int areaCount = animalArea.get(area);
- animalArea.put(area, areaCount + 1);
- }
- } else {
- int currFood = animalFoodLimit.get(name)+ food;
- animalFoodLimit.put(name, currFood);
- }
- } else if (tokens[0].equals("Feed")) {
- if (animalFoodLimit.containsKey(name)) {
- int currFood = animalFoodLimit.get(name);
- animalFoodLimit.put(name, currFood - food);
- if (currFood - food <= 0) {
- animalFoodLimit.remove(name);
- System.out.printf("%s was successfully fed%n", name);
- if (animalArea.get(area) > 1) {
- int areaValue = animalArea.get(area);
- animalArea.put(area, areaValue - 1);
- } else {
- animalArea.remove(area);
- }
- }
- }
- // if (animalsFoodArea.containsKey(name)) {
- // int currFood = animalsFoodArea.get(name).get(area);
- // if (currFood - food <= 0) {
- // animalsFoodArea.remove(name);
- // hungryAnimals.remove(name);
- // System.out.printf("%s was successfully fed%n", name);
- // } else {
- // animalsFoodArea.get(name).put(area, currFood - food);
- // }
- // }
- }
- input = sc.nextLine();
- }
- System.out.println("Animals: ");
- animalFoodLimit.entrySet().stream().sorted((f, s) -> {
- int result = s.getValue() - f.getValue();
- if (result == 0) {
- result = f.getKey().compareTo(s.getKey());
- }
- return result;
- })
- .forEach(e -> {
- System.out.printf("%s -> %dg%n", e.getKey(), e.getValue());
- });
- System.out.println("Areas with hungry animals:");
- animalArea.entrySet().stream().sorted((f,s) ->
- s.getValue().compareTo(f.getValue()))
- .forEach(entry -> System.out.printf("%s : %d%n", entry.getKey(), entry.getValue()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement