-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaconEggsAndSpam.java
59 lines (45 loc) · 1.66 KB
/
BaconEggsAndSpam.java
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
48
49
50
51
52
53
54
55
56
57
58
59
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;
public class BaconEggsAndSpam {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int numCustomers = Integer.parseInt(bufferedReader.readLine());
while (numCustomers > 0) {
Menu menu = new Menu();
menu.createMap(bufferedReader, numCustomers);
menu.printMap();
System.out.println();
numCustomers = Integer.parseInt(bufferedReader.readLine());
}
bufferedReader.close();
}
}
class Menu {
private Map<String, TreeSet<String>> menuItems;
public Menu() {
}
public void createMap(BufferedReader bufferedReader, int numCustomers) throws IOException {
this.menuItems = new TreeMap<String, TreeSet<String>>();
for (int i = 0; i < numCustomers; i++) {
String[] order = bufferedReader.readLine().split(" ");
for (int j = 1; j < order.length; j++) {
TreeSet<String> set = menuItems.get(order[j]);
if(set == null) {
set = new TreeSet<String>();
menuItems.put(order[j], set);
}
set.add(order[0]);
}
}
}
public void printMap() {
this.menuItems.entrySet().forEach(entry -> {
System.out.println(entry.getKey() + " " + String.join(" ", new ArrayList<String>(entry.getValue())));
});
}
}