RGAlex

O2. Ad Astra / FinalExam - O1

Mar 25th, 2021
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. package FinalExam.O1;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class O2AdAstra {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         String input = scanner.nextLine();
  13.         Matcher matcher = Pattern.compile("([#|])(?<product>[A-Z][A-Z a-z]+)\\1(?<date>\\d{2}/\\d{2}/\\d{2})\\1(?<nutrition>\\d+)\\1")
  14.                 .matcher(input);
  15.         List<String> allGoods = new ArrayList<>();
  16.  
  17.         int total = 0;
  18.         while (matcher.find()) {
  19.             allGoods.add(String.format("Item: %s, Best before: %s, Nutrition: %s",
  20.                     matcher.group("product"), matcher.group("date"), matcher.group("nutrition")));
  21.             total += Integer.parseInt(matcher.group("nutrition"));
  22.         }
  23.         int day = total / 2000;
  24.         System.out.println("You have food to last you for: " + day + " days!");
  25.         for (String s : allGoods) {
  26.             System.out.println(s);
  27.         }
  28.     }
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment