Advertisement
Marchoy1991

Ad

Aug 15th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class Ex2 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         String input = scanner.nextLine();
  11.  
  12.         String regex = "([#]|[|])(?<item>[A-Za-z\\s]+)\\1(?<date>\\d{2}\\/\\d{2}\\/\\d{2})\\1(?<calories>\\d{1,5})\\1";
  13.         Pattern pattern = Pattern.compile(regex);
  14.         Matcher matcher = pattern.matcher(input);
  15.  
  16.         Map<String, String> hoho = new LinkedHashMap<>();
  17.         Map<String, Integer> ho = new LinkedHashMap<>();
  18.         int calCount = 0;
  19.         while (matcher.find()) {
  20.             String food = matcher.group("item");
  21.             String expire = matcher.group("date");
  22.             int calories = Integer.parseInt(matcher.group("calories"));
  23.             calCount = calCount + calories;
  24.             hoho.put(food, expire);
  25.             ho.put(food, calories);
  26.         }
  27.         int calPerDay = calCount / 2000;
  28.         System.out.println(String.format("You have food to last you for: %d days!", calPerDay));
  29.         hoho
  30.                 .forEach((key, value) -> System.out.printf("Item: %s, Best before %s, Nutrition: %d%n",
  31.                         key, value, ho.get()));
  32.     }
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement