Posted By

bcmoney on 01/24/14


Tagged

java timer pomodoro reminder standup


Versions (?)

Pomodoro Timer


 / Published in: Java
 

This timer reminds a computer user to stand up every X seconds, Y times per day. Could be scheduled daily at start of regular work hours. Runs using the following command: -> PomodoroTimer

As in: -> PomodoroTimer 3600 8

Runs the reminder every hour for eight hours.

  1. package net.bcmoney;
  2.  
  3. import java.util.Timer;
  4. import java.util.TimerTask;
  5. import javax.swing.JOptionPane;
  6.  
  7.  
  8.  
  9. /**
  10.  *
  11.  * @author bcbcope
  12.  */
  13. public class PomodoroTimer {
  14.  
  15. public static final int TIME_IN_MILLISECONDS = 1000;
  16.  
  17. /**
  18.   * @param args the command line arguments
  19.   */
  20. public static void main(String[] args) {
  21. int interval = 0; //time interval in between calls (in seconds)
  22. int frequency = 0; //number of times to call per run before exiting
  23. if (args.length >= 2) {
  24. interval = Integer.parseInt(args[0]);
  25. frequency = Integer.parseInt(args[1]);
  26. } else if (args.length == 1) {
  27. interval = Integer.parseInt(args[1]);
  28. } else {
  29. interval = 1;
  30. frequency = 5;
  31. }
  32.  
  33. //1- Taking an instance of Timer class.
  34. Timer timer = new Timer("PomodoroReminderTimer");
  35.  
  36. //2- Taking an instance of class contains your repeated method.
  37. RemindToStandUp task = new RemindToStandUp(frequency);
  38.  
  39. //First Parameter is the task to run (our Pomodoro Timer "Stand Up!" reminder)
  40. //Second Parameter is the specified the Starting Time for your timer in MilliSeconds or Date
  41. //Third Parameter is the specified the Period between consecutive calling for the method.
  42. timer.schedule(task, 0, interval*TIME_IN_MILLISECONDS);
  43. }
  44. }
  45. /**
  46.  * RemindToStandUp
  47.  * A Pomodoro Timer is a single-run or interval-based reminder and notification
  48.  * system. In this case, the timer is used to remind a computer-user to stand
  49.  * up once every set amount of time in seconds (i.e. 30 minutes=1800,
  50.  * 1 hour = 3600, etc)
  51.  * NOTE:
  52.  * TimerTask implements Runnable interface so must override run method within
  53.  * our Pomodoro Timer code block.
  54.  *
  55.  */
  56. class RemindToStandUp extends TimerTask {
  57. private static int repetions = 0; //total number of times to call
  58. private int times = 0; //tracker for number of times called
  59.  
  60. public RemindToStandUp(int reps) {
  61. this.repetions = reps;
  62. }
  63.  
  64. @Override
  65. public void run() {
  66. times++;
  67. if (times <= repetions) {
  68. System.out.println("Pomodoro event sent by timer...");
  69. JOptionPane.showMessageDialog(null, "REMINDER: Stand up!" +
  70. System.getProperty("line.separator") +
  71. " Walk around a little, maybe get some fresh air.");
  72. } else {
  73. System.out.println("Pomodoro series over...");
  74. //Stop Timer.
  75. this.cancel();
  76. System.exit(0);
  77. System.out.println("Timer stopped!");
  78. }
  79. }
  80. }

Report this snippet  

You need to login to post a comment.