Advertisement
Guest User

Untitled

a guest
Jan 15th, 2024
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. package JavaFundamentals._21_FinalExamPreparation;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class _02_AdAstra {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         StringBuilder sb = new StringBuilder();
  11.         String input = scanner.nextLine();
  12.         //String regex = "([#|\\|])([A-za-z]+)(\\1)([0-9]{2,}/[0-9]{2,}/[0-9]{2,})(\\1)([0-9]+)(\\1)";
  13.         String regex = "([#|])([A-Za-z\\s]+)(\\1)([0-9]{2}\\/[0-9]{2}\\/[0-9]{2})(\\1)([0-9]{1,5})(\\1)";
  14.  
  15.         int totalCalories = 0;
  16.  
  17.         //Map<String, List<String>> foodMap = new LinkedHashMap<>();
  18.  
  19.         Pattern pattern = Pattern.compile(regex);
  20.         Matcher matcher = pattern.matcher(input);
  21.         while (matcher.find()) {
  22.             String foodName = matcher.group(2);
  23.             String date = matcher.group(4);
  24.             int calories = Integer.parseInt(matcher.group(6));
  25.  
  26.             //if (!foodMap.containsKey(foodName)) {
  27.             //    foodMap.put(foodName, new ArrayList<>());
  28.             //}
  29.             //foodMap.get(foodName).add(date);
  30.             //foodMap.get(foodName).add(String.valueOf(calories));
  31.             sb.append(System.getProperty("line.separator") + "Item: " + foodName + ", Best before: " + date + ", Nutrition: " + calories);
  32.             totalCalories += calories;
  33.         }
  34.  
  35.         int foodDays = totalCalories / 2000;
  36.         System.out.printf("You have food to last you for: %d days!%n", foodDays);
  37.        
  38.         System.out.println(sb.toString().trim());
  39.         //for (Map.Entry<String, List<String>> entry : foodMap.entrySet()) {
  40.         //    List<String> values = entry.getValue();
  41.         //    String date = values.get(0);
  42.         //    String calories = values.get(1);
  43.         //
  44.         //    System.out.printf("Item: %s, Best before: %s, Nutrition: %s%n", entry.getKey(), date, calories);
  45.         //}
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement