Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package JavaFundamentals._21_FinalExamPreparation;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class _02_AdAstra {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- StringBuilder sb = new StringBuilder();
- String input = scanner.nextLine();
- //String regex = "([#|\\|])([A-za-z]+)(\\1)([0-9]{2,}/[0-9]{2,}/[0-9]{2,})(\\1)([0-9]+)(\\1)";
- String regex = "([#|])([A-Za-z\\s]+)(\\1)([0-9]{2}\\/[0-9]{2}\\/[0-9]{2})(\\1)([0-9]{1,5})(\\1)";
- int totalCalories = 0;
- //Map<String, List<String>> foodMap = new LinkedHashMap<>();
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(input);
- while (matcher.find()) {
- String foodName = matcher.group(2);
- String date = matcher.group(4);
- int calories = Integer.parseInt(matcher.group(6));
- //if (!foodMap.containsKey(foodName)) {
- // foodMap.put(foodName, new ArrayList<>());
- //}
- //foodMap.get(foodName).add(date);
- //foodMap.get(foodName).add(String.valueOf(calories));
- sb.append(System.getProperty("line.separator") + "Item: " + foodName + ", Best before: " + date + ", Nutrition: " + calories);
- totalCalories += calories;
- }
- int foodDays = totalCalories / 2000;
- System.out.printf("You have food to last you for: %d days!%n", foodDays);
- System.out.println(sb.toString().trim());
- //for (Map.Entry<String, List<String>> entry : foodMap.entrySet()) {
- // List<String> values = entry.getValue();
- // String date = values.get(0);
- // String calories = values.get(1);
- //
- // System.out.printf("Item: %s, Best before: %s, Nutrition: %s%n", entry.getKey(), date, calories);
- //}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement