Advertisement
CR7CR7

astra-list

Aug 18th, 2023
1,396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.03 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.*;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String input = scanner.nextLine();
  8.         Pattern pattern = Pattern.compile("([|#])([A-Za-z\\s]+)\\1(\\d{2}/\\d{2}/\\d{2})\\1(\\d+)\\1");
  9.         Matcher matcher = pattern.matcher(input);
  10.         int totalCalories = 0;
  11.         List<String> foodItems = new ArrayList<>();
  12.         while (matcher.find()) {
  13.             String itemName = matcher.group(2);
  14.             String expirationDate = matcher.group(3);
  15.             int calories = Integer.parseInt(matcher.group(4));
  16.             totalCalories += calories;
  17.             foodItems.add(String.format("Item: %s, Best before: %s, Nutrition: %d", itemName, expirationDate, calories));
  18.         }
  19.         int days = totalCalories / 2000;
  20.         System.out.println("You have food to last you for: " + days + " days!");
  21.         for (String item : foodItems) {
  22.             System.out.println(item);
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement