Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- public class Parser {
- public static void main(String[] args) throws Exception {
- File file = new File("history.txt");
- BufferedReader reader = new BufferedReader(new FileReader(file));
- SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy hh:mm:ss");
- final Map<String, Integer> dayHourMap = new HashMap<>();
- String line;
- while ((line = reader.readLine()) != null) {
- if (line.matches("[0-9][0-9]\\.[0-9][0-9]\\.[0-9][0-9][0-9]. [0-9][0-9]:[0-9][0-9]:[0-9][0-9].*$")) {
- if (!line.contains("Cupcake")) {
- continue;
- }
- String[] split = line.split(",");
- Date date = dateFormat.parse(split[0]);
- Calendar cal = Calendar.getInstance();
- cal.setTime(date);
- String day = "" + cal.get(Calendar.DAY_OF_MONTH);
- String hour = "" + cal.get(Calendar.HOUR_OF_DAY);
- String dayHour = "DAY: " + day + " Hour: " + hour;
- if (!dayHourMap.containsKey(dayHour)) {
- dayHourMap.put(dayHour, 0);
- }
- dayHourMap.put(dayHour, dayHourMap.get(dayHour) + 1);
- }
- }
- histogram(dayHourMap);
- reader.close();
- }
- private static void histogram(Map<String, Integer> map) {
- map.keySet().stream().sorted().forEach(key -> {
- int amount = map.get(key);
- StringBuilder lines = new StringBuilder();
- for (int i = 0; i < amount; i++) {
- lines.append("|");
- }
- lines.append(" ").append(amount);
- if (key.length() <= 14) {
- System.out.printf("%s => %s\n", key, lines.toString());
- } else if (key.length() <= 15) {
- System.out.printf("%s => %s\n", key, lines.toString());
- } else {
- System.out.printf("%s => %s\n", key, lines.toString());
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement