Advertisement
CR7CR7

AdAstra

Jul 17th, 2023
1,336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class AdAstra {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String regexFoodCalories = "(?<Separator>[\\|#]+)(?<ItemName>[A-Za-z\\s]+)(\\1)(?<ExpirationDate>\\d{2}/\\d{2}/\\d{2})(\\1)(?<Calories>\\d+)(\\1)";
  11.  
  12.         String itemName = "ItemName";
  13.         String expirationDate = "ExpirationDate";
  14.         String calories = "Calories";
  15.  
  16.         String input = scanner.nextLine();
  17.  
  18.         int dailyCaloriesNeeded = 2000;
  19.         int totalCaloriesPacked = 0;
  20.  
  21.         Pattern patternCalories = Pattern.compile(regexFoodCalories);
  22.         Matcher matcherCalories = patternCalories.matcher(input);
  23.  
  24.         while (matcherCalories.find()) {
  25.             String currentItemName =  matcherCalories.group(itemName);
  26.             String currentExpirationDate =  matcherCalories.group(expirationDate);
  27.             String currentCalories =  matcherCalories.group(calories);
  28.             totalCaloriesPacked += Integer.parseInt(currentCalories);
  29.         }
  30.  
  31.         System.out.printf("You have food to last you for: %d days!\n", totalCaloriesPacked / dailyCaloriesNeeded);
  32.  
  33.         Pattern patternFood = Pattern.compile(regexFoodCalories);
  34.         Matcher matcherFood = patternFood.matcher(input);
  35.  
  36.         while (matcherFood.find()) {
  37.             String currentItemName = matcherFood.group(itemName);
  38.             String currentExpirationDate = matcherFood.group(expirationDate);
  39.             String currentCalories =  matcherFood.group(calories);
  40.             System.out.printf("Item: %s, Best before: %s, Nutrition: %s\n", currentItemName, currentExpirationDate, currentCalories);
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement