From b586dd4c4eab35253f574ce4bf56a4ef1b67c437 Mon Sep 17 00:00:00 2001 From: Vinh_Lam_the Date: Tue, 5 Nov 2024 23:05:56 +0700 Subject: [PATCH] Add Daily tasks function --- TimeTrackerApp.java | 71 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/TimeTrackerApp.java b/TimeTrackerApp.java index 54237e1..9489ac0 100644 --- a/TimeTrackerApp.java +++ b/TimeTrackerApp.java @@ -8,6 +8,8 @@ import java.io.IOException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; import javax.sound.sampled.*; public class TimeTrackerApp { @@ -101,6 +103,10 @@ public static void main(String[] args) { JButton viewButton = new JButton("View Saved Data"); buttonPanel.add(viewButton); + // Add button View Daily time spent + JButton dailyViewButton = new JButton("View Daily Time Spent"); + buttonPanel.add(dailyViewButton); + // Add button panel to main panel mainPanel.add(buttonPanel, BorderLayout.CENTER); @@ -205,6 +211,14 @@ public void actionPerformed(ActionEvent e) { } }); + // View Daily action + dailyViewButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + showDailyTimeTasks(); + } + }); + frame.setVisible(true); } @@ -300,4 +314,61 @@ private static void showSavedData() { JOptionPane.showMessageDialog(null, "No data found or unable to read the file."); } } + + private static void showDailyTimeTasks() { + StringBuilder content = new StringBuilder(); + Map dailyTasks = new HashMap<>(); + LocalDateTime now = LocalDateTime.now(); + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd"); + + try (BufferedReader reader = new BufferedReader(new FileReader("time_tracking_log.txt"))) { + String line; + while ((line = reader.readLine()) != null) { + String[] parts = line.split(" - "); + if (parts.length >= 3) { + String datePart = parts[0]; + String taskPart = parts[1].replace("Task: ", ""); + String durationPart = parts[2].replace("Duration: ", ""); + + String taskDate = datePart.split(" ")[0]; + if (taskDate.equals(dtf.format(now))) { + int durationInSeconds = convertDurationToSeconds(durationPart); + dailyTasks.put(taskPart, dailyTasks.getOrDefault(taskPart, 0) + durationInSeconds); + } + } + } + + if (dailyTasks.isEmpty()) { + content.append("No tasks recorded for today."); + } else { + for (Map.Entry entry : dailyTasks.entrySet()) { + content.append(entry.getKey()).append(": ").append(formatTime(entry.getValue())).append("\n"); + } + } + + JTextArea textArea = new JTextArea(content.toString()); + textArea.setEditable(false); + JScrollPane scrollPane = new JScrollPane(textArea); + scrollPane.setPreferredSize(new Dimension(600, 200)); + JOptionPane.showMessageDialog(null, scrollPane, "Daily Time Spent", JOptionPane.INFORMATION_MESSAGE); + } catch (IOException ex) { + JOptionPane.showMessageDialog(null, "No data found or unable to read the file."); + } + } + + private static int convertDurationToSeconds(String duration) { + String[] durationParts = duration.split(", "); + int totalSeconds = 0; + for (String part : durationParts) { + part = part.trim(); + if (part.endsWith("hours")) { + totalSeconds += Integer.parseInt(part.split(" ")[0]) * 3600; + } else if (part.endsWith("minutes")) { + totalSeconds += Integer.parseInt(part.split(" ")[0]) * 60; + } else if (part.endsWith("seconds")) { + totalSeconds += Integer.parseInt(part.split(" ")[0]); + } + } + return totalSeconds; + } }