Advertisement
Guest User

Untitled

a guest
Aug 9th, 2021
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ProgrammingFundamentalsFinalExamRetake01;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class AdAstra {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         String stringLine = scanner.nextLine();
  11.         String regex = "(?<symbols>[#\\|])(?<name>[A-za-z\\s]+)(\\1)(?<date>[0-9]{2}[\\/][0-9]{2}[\\/][0-9]{2})(\\1)(?<calories>[\\d]{1,5})(\\1)";
  12.         Pattern pattern = Pattern.compile(regex);
  13.         Matcher matcher = pattern.matcher(stringLine);
  14.         Map<String,String> foodDate = new LinkedHashMap<>();
  15.         Map<String,Integer> foodCalories = new LinkedHashMap<>();
  16.         List<String> items = new LinkedList<>();
  17.         int totalCalories = 0;    //change
  18.         while (matcher.find()){
  19.             String nameofItem = matcher.group("name");
  20.             String expirationDate = matcher.group("date");
  21.             int calories = Integer.parseInt(matcher.group("calories"));
  22.            
  23.             foodDate.putIfAbsent(nameofItem,expirationDate);
  24.             foodCalories.putIfAbsent(nameofItem,calories);
  25.             totalCalories += calories;  //change
  26.             items.add(String.format("Item: %s, Best before: %s, Nutrition: %d%n", nameofItem,expirationDate,calories));
  27.         }
  28.  
  29.         int sum = 0;
  30.         for (Map.Entry<String, Integer> entry:foodCalories.entrySet()) {
  31.  
  32.            sum += entry.getValue();
  33.         }
  34.  
  35.         System.out.printf("You have food to last you for: %d days!%n", totalCalories/2000); //change
  36.  
  37.  
  38.             items.forEach(System.out::print);
  39.  
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement