-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChildrenCount.java
More file actions
28 lines (22 loc) · 928 Bytes
/
Copy pathChildrenCount.java
File metadata and controls
28 lines (22 loc) · 928 Bytes
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
import java.util.*;
public class ChildrenCount{
private ArrayList<InsuranceRecord> records;
public ChildrenCount(ArrayList<InsuranceRecord> records) {
this.records = records;
}
public void print_children_count(){
HashMap<Integer, Integer> children_counts = new HashMap<>();
for (InsuranceRecord record : records) {
children_counts.put(record.children, children_counts.getOrDefault(record.children, 0) + 1);
}
ArrayList<Integer> children_numbers = new ArrayList<>(children_counts.keySet());
Collections.sort(children_numbers);
for(int children : children_numbers) {
System.out.printf("%d children: ", children);
for(int i = 0; i < children_counts.get(children); i++) {
System.out.print("*");
}
System.out.println(" (" + children_counts.get(children) + ")");
}
}
}