Advertisement
IrinaIgnatova

ExamPrep - FeedTheAnimals

Aug 1st, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.46 KB | None | 0 0
  1. package com.company;
  2.  
  3.  
  4. import java.util.*;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) {
  12.  
  13.         Scanner scanner = new Scanner(System.in);
  14.  
  15.         String input = scanner.nextLine();
  16.  
  17.         HashMap<String,Integer>nameAndFoodLimit=new LinkedHashMap<>();
  18.         HashMap<String,Integer>areaAndCountAnimals=new LinkedHashMap<>();
  19.  
  20.         while (!input.equals("Last Info")){
  21.             String [] tokens=input.split(":");
  22.             String command=tokens[0];
  23.             String name=tokens[1];
  24.             int dailyFoodLimit=Integer.parseInt(tokens[2]);
  25.             String area=tokens[3];
  26.  
  27.             if(command.equals("Add")){
  28.  
  29.                 if(!nameAndFoodLimit.containsKey(name)){
  30.                     nameAndFoodLimit.put(name,dailyFoodLimit);
  31.                 }else{
  32.                     Integer foodLimit = nameAndFoodLimit.get(name);
  33.                     int limitCurrent = foodLimit + dailyFoodLimit;
  34.                     nameAndFoodLimit.put(name,limitCurrent);
  35.                     if(!areaAndCountAnimals.containsKey(area)){
  36.                         areaAndCountAnimals.put(area,1);
  37.                     }else{
  38.                         Integer countAnimals = areaAndCountAnimals.get(area);
  39.                         areaAndCountAnimals.put(area,countAnimals+1);
  40.  
  41.                     }
  42.                 }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.             }else if(command.equals("Feed")){
  49.                 if(nameAndFoodLimit.containsKey(name)){
  50.                 Integer limit = nameAndFoodLimit.get(name);
  51.                 int leftFood = limit - dailyFoodLimit;
  52.  
  53.  
  54.  
  55.                     if(leftFood<=0){
  56.                         nameAndFoodLimit.remove(name);
  57.                         System.out.println(name+" was successfully fed");
  58.  
  59.  
  60.                             Integer count = areaAndCountAnimals.get(name);
  61.                             areaAndCountAnimals.put(name,count-1);
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                     }else{
  68.                         nameAndFoodLimit.put(name, leftFood);
  69.                     }
  70.  
  71.  
  72.                 }
  73.  
  74.             }
  75.  
  76.  
  77.  
  78.  
  79.             input=scanner.nextLine();
  80.         }
  81.         System.out.println("Animals:");
  82.         nameAndFoodLimit.entrySet().stream()
  83.                 .sorted((f,s)->{//тук в descending order искаме да сравним Value
  84.                     int result = s.getValue().compareTo(f.getValue());
  85.                     if(result==0){//ако по първи параметър (Value) са равни като сравним, тогава по (Key) ascending order
  86.                        result= f.getKey().compareTo(s.getKey());
  87.                     }
  88.                     return result;
  89.                 })
  90.                 .forEach(entry->
  91.                         System.out.printf("%s -> %dg%n",entry.getKey(),entry.getValue()));
  92.         System.out.println();
  93.  
  94.  
  95.         System.out.println("Areas with hungry animals:");
  96.  
  97.        areaAndCountAnimals.entrySet()
  98.                .stream()
  99.                .filter(entry->entry.getValue()>0)// entry отива в getValue и тук проверяваме дали е нахранено животното
  100.                .sorted((f,s)-> s.getValue().compareTo(f.getValue()))//подреждаме в descending order
  101.                .forEach(entry->{
  102.                    System.out.printf("%s : %d%n",entry.getKey(),entry.getValue());
  103.                });
  104.  
  105.  
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement