-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdAstra.java
More file actions
47 lines (33 loc) · 1.44 KB
/
Copy pathAdAstra.java
File metadata and controls
47 lines (33 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package finalExam;
import java.util.LinkedHashMap;
import java.util.Map;
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);
String text = scanner.nextLine();
int totalFood = 0;
Pattern pattern = Pattern.compile("(?<symbols>[\\|\\#])(?<name>[A-Za-z\\s]+)(\\1)(?<date>\\d{2}\\/\\d{2}\\/\\d{2})(\\1)(?<calories>\\d{0,5})(\\1)");
Matcher matcher = pattern.matcher(text);
Map<String,Map<String,Integer>> productMap = new LinkedHashMap<>();
while(matcher.find()){
String foodName = matcher.group("name");
String date = matcher.group("date");
int calories = Integer.parseInt(matcher.group("calories"));
Map<String,Integer> dataMap = new LinkedHashMap<>();
dataMap.putIfAbsent(date,calories);
productMap.putIfAbsent(foodName,dataMap);
totalFood += calories;
}
int days = totalFood / 2000;
System.out.printf("You have food to last you for: %d days!%n",days);
productMap.entrySet().forEach(e->{
System.out.printf("Item: %s, ",e.getKey());
e.getValue().entrySet().forEach(element ->{
System.out.printf("Best before: %s, Nutrition: %d%n",element.getKey(),element.getValue());
});
});
}
}