Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PogotovkaZaIzpit;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- 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);
- List<Item> items = new ArrayList<>();
- String pattern = "(\\||\\#)[A-Za-z0-9 ]{1,}\\1[0-9]{2}\\/[0-9]{2}\\/[0-9]{2}\\1([0-9]{1,4}|10000)\\1";
- String input = scanner.nextLine();
- char[] splitters = { '#', '|' };
- Pattern p = Pattern.compile(pattern);
- Matcher m = p.matcher(input);
- while (m.find()) {
- String[] data = m.group().split("[#|]");
- Item item = new Item(data[1], data[2], Integer.parseInt(data[3]));
- items.add(item);
- }
- int totalCalories = items.stream().mapToInt(Item::getCalories).sum();
- int days = totalCalories / 2000;
- System.out.println("You have food to last you for: " + days + " days!");
- for (Item item : items) {
- System.out.println("Item: " + item.getName() + ", Best before: " + item.getBestBefore() + ", Nutrition: " + item.getCalories());
- }
- }
- static class Item {
- private String name;
- private String bestBefore;
- private int calories;
- public Item(String name, String bestBefore, int calories) {
- this.name = name;
- this.bestBefore = bestBefore;
- this.calories = calories;
- }
- public String getName() {
- return name;
- }
- public String getBestBefore() {
- return bestBefore;
- }
- public int getCalories() {
- return calories;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement