Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bg.softuni.javafundamentals;
- import java.util.*;
- public class Fin03_13Dec_Meals2 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, List<String>> nameLike = new HashMap<>();
- int unlikedMeals = 0;
- String word;
- while (!(word = scanner.nextLine()).equals("Stop")) {
- String[] tokens = word.split("-");
- String name = tokens[1];
- String meal = tokens[2];
- switch (tokens[0]) {
- case "Like":
- if (!nameLike.containsKey(name)) {
- List<String> meals = new ArrayList<>();
- meals.add(meal);
- nameLike.put(name, meals);
- } else {
- nameLike.get(name).add(meal);
- }
- break;
- case "Unlike":
- if (!nameLike.containsKey(name)) {
- System.out.printf("%s is not at the party.%n", name);
- } else {
- if (nameLike.get(name).contains(meal)) {
- nameLike.get(name).remove(meal);
- System.out.printf("%s doesn't like the %s.%n", name, meal);
- unlikedMeals++;
- } else {
- System.out.printf("%s doesn't have the %s in his/her collection.%n", name, meal);
- }
- }
- break;
- }
- }
- nameLike.entrySet()
- .stream()
- .sorted((e1, e2) -> {
- int result = Integer.compare(e2.getValue().size(), e1.getValue().size());
- if (result == 0){
- result = e1.getKey().compareTo(e2.getKey());
- }
- return result;
- })
- .forEach(entry -> {
- System.out.printf("%s: %s%n", entry.getKey(), String.join(", ", entry.getValue()));
- });
- System.out.printf("Unliked meals: %d%n", unlikedMeals);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement