Guest User

Untitled

a guest
Apr 1st, 2022
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.27 KB | None | 0 0
  1. package Preparation;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class AdAstra {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         String input = scanner.nextLine();
  11.         Pattern pattern = Pattern.compile("(?<symbols>[#\\|])(?<name>[A-za-z\\s]+)(\\1)(?<date>[0-9]{2}[\\/][0-9]{2}[\\/][0-9]{2})(\\1)(?<calories>[\\d]{1,5})(\\1)");
  12.         Matcher matcher = pattern.matcher(input);
  13.         Map<String, List<String>> products = new LinkedHashMap<>();
  14.         int sum = 0;
  15.         while (matcher.find()){
  16.             String name = matcher.group("name");
  17.             String date = matcher.group("date");
  18.             String calories = matcher.group("calories");
  19.             sum += Integer.parseInt(calories);
  20.             products.put(name, new ArrayList<>());
  21.             products.get(name).add(date);
  22.             products.get(name).add(calories);
  23.         }
  24.         System.out.printf("You have food to last you for: %d days!%n",sum/2000);
  25.         for (Map.Entry<String, List<String>> entry : products.entrySet()) {
  26.             System.out.printf("Item: %s, Best before: %s, Nutrition: %s%n",entry.getKey(),entry.getValue().get(0),entry.getValue().get(1));
  27.         }
  28.  
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment