Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class MyClass {
- public static void main(String args[]) {
- Scanner scanner = new Scanner(System.in);
- LinkedHashMap<String, LinkedHashMap<String,Double>> animalAreaFoodMap = new LinkedHashMap<String,LinkedHashMap<String, Double>>();
- String nextLine = scanner.nextLine();
- while(true) {
- if(nextLine.equals("EndDay")) {
- Iterator<Map.Entry<String,LinkedHashMap<String, Double>>> itr = animalAreaFoodMap.entrySet().iterator();
- while (itr.hasNext()) {
- Map.Entry pair = (Map.Entry)itr.next();
- LinkedHashMap<String, Double> areaAndFoodMap = (LinkedHashMap<String, Double>)pair.getValue();
- Iterator<Map.Entry<String, Double>> areaAndFoodIterator = areaAndFoodMap.entrySet().iterator();
- Map.Entry areaAndFood = (Map.Entry)areaAndFoodIterator.next();
- System.out.println(hungryAnimalsInArea(animalAreaFoodMap,areaAndFood.getKey().toString()));
- System.out.println("Animal:");
- System.out.printf("(area %s) %s -> %s\n",areaAndFood.getKey().toString(),pair.getKey(),areaAndFood.getValue());
- }
- break;
- } else {
- String[] arrOfStr = nextLine.split("-");
- if (arrOfStr.length == 3) {
- if (animalAreaFoodMap.containsKey(arrOfStr[1])) {
- LinkedHashMap<String, Double> areaAndFoodEntry = animalAreaFoodMap.get(arrOfStr[1]);
- Iterator<Map.Entry<String, Double>> areaAndFoodOperator = areaAndFoodEntry.entrySet().iterator();
- Map.Entry areaAndFood = (Map.Entry)areaAndFoodOperator.next();
- double newFoodQuantity = (double)areaAndFood.getValue() - Double.parseDouble(arrOfStr[2]);
- areaAndFoodEntry.put(areaAndFood.getKey().toString(), newFoodQuantity);
- if (newFoodQuantity <= 0) {
- animalAreaFoodMap.remove(arrOfStr[1]);
- System.out.printf("%s was successfully fed\n",arrOfStr[1]);
- }
- }
- } else {
- if (animalAreaFoodMap.containsKey(arrOfStr[1])) {
- LinkedHashMap<String, Double> areaAndFoodEntry = animalAreaFoodMap.get(arrOfStr[1]);
- Iterator<Map.Entry<String, Double>> areaAndFoodOperator = areaAndFoodEntry.entrySet().iterator();
- Map.Entry areaAndFood = (Map.Entry)areaAndFoodOperator.next();
- double newFoodQuantity = (double)areaAndFood.getValue() + Double.parseDouble(arrOfStr[2]);
- areaAndFoodEntry.put(areaAndFood.getKey().toString(), newFoodQuantity);
- } else {
- LinkedHashMap<String, Double> areaAndFoodEntry = new LinkedHashMap<String, Double>();
- areaAndFoodEntry.put(arrOfStr[3],Double.parseDouble(arrOfStr[2]));
- animalAreaFoodMap.put(arrOfStr[1],areaAndFoodEntry);
- }
- }
- }
- nextLine = scanner.nextLine();
- }
- }
- public static int hungryAnimalsInArea(Map<String, LinkedHashMap<String,Double>> animalAreaFoodMap, String area) {
- int areaAnimalCounter = 0;
- Iterator<Map.Entry<String,LinkedHashMap<String,Double>>> itr = animalAreaFoodMap.entrySet().iterator();
- while (itr.hasNext()) {
- Map.Entry pair = (Map.Entry)itr.next();
- LinkedHashMap<String, Double> areaAndFoodMap = (LinkedHashMap<String,Double>)pair.getValue();
- Iterator<Map.Entry<String,Double>> areaAndFoodIterator = areaAndFoodMap.entrySet().iterator();
- Map.Entry areaAndFood = (Map.Entry)areaAndFoodIterator.next();
- if (areaAndFood.getKey().equals(area)) {
- areaAnimalCounter++;
- }
- }
- return areaAnimalCounter;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement