Advertisement
Ligh7_of_H3av3n

02. Ad Astra

Mar 23rd, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package PogotovkaZaIzpit;
  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 AdAstra {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.  
  14.         List<Item> items = new ArrayList<>();
  15.  
  16.         String pattern = "(\\||\\#)[A-Za-z0-9 ]{1,}\\1[0-9]{2}\\/[0-9]{2}\\/[0-9]{2}\\1([0-9]{1,4}|10000)\\1";
  17.         String input = scanner.nextLine();
  18.         char[] splitters = { '#', '|' };
  19.         Pattern p = Pattern.compile(pattern);
  20.         Matcher m = p.matcher(input);
  21.  
  22.         while (m.find()) {
  23.             String[] data = m.group().split("[#|]");
  24.             Item item = new Item(data[1], data[2], Integer.parseInt(data[3]));
  25.             items.add(item);
  26.         }
  27.  
  28.         int totalCalories = items.stream().mapToInt(Item::getCalories).sum();
  29.         int days = totalCalories / 2000;
  30.  
  31.         System.out.println("You have food to last you for: " + days + " days!");
  32.  
  33.         for (Item item : items) {
  34.             System.out.println("Item: " + item.getName() + ", Best before: " + item.getBestBefore() + ", Nutrition: " + item.getCalories());
  35.         }
  36.     }
  37.  
  38.     static class Item {
  39.         private String name;
  40.         private String bestBefore;
  41.         private int calories;
  42.  
  43.         public Item(String name, String bestBefore, int calories) {
  44.             this.name = name;
  45.             this.bestBefore = bestBefore;
  46.             this.calories = calories;
  47.         }
  48.  
  49.         public String getName() {
  50.             return name;
  51.         }
  52.  
  53.         public String getBestBefore() {
  54.             return bestBefore;
  55.         }
  56.  
  57.         public int getCalories() {
  58.             return calories;
  59.         }
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement