Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Preparation;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class AdAstra {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- Pattern pattern = Pattern.compile("(?<symbols>[#\\|])(?<name>[A-za-z\\s]+)(\\1)(?<date>[0-9]{2}[\\/][0-9]{2}[\\/][0-9]{2})(\\1)(?<calories>[\\d]{1,5})(\\1)");
- Matcher matcher = pattern.matcher(input);
- Map<String, List<String>> products = new LinkedHashMap<>();
- int sum = 0;
- while (matcher.find()){
- String name = matcher.group("name");
- String date = matcher.group("date");
- String calories = matcher.group("calories");
- sum += Integer.parseInt(calories);
- products.put(name, new ArrayList<>());
- products.get(name).add(date);
- products.get(name).add(calories);
- }
- System.out.printf("You have food to last you for: %d days!%n",sum/2000);
- for (Map.Entry<String, List<String>> entry : products.entrySet()) {
- System.out.printf("Item: %s, Best before: %s, Nutrition: %s%n",entry.getKey(),entry.getValue().get(0),entry.getValue().get(1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment