Monday, September 09, 2013

Pomodoro Timer in Java

I don’t know when I first read about it, but for some time now I’ve employed a version of the Pomodoro Technique when I’m working on an extended project. When I started learning to code, I thought it would be a neat project to code a Pomodoro timer. It’s been a project on my back burner for a while now.

Last week, I had the brainstorm that put my on the path of how to actually write it. I wasn’t looking for a timer as much as a countdown so that I could see how much time was left. What I really want is to update a display once a second for as long as the time is set to run.

My next goal is to rewrite this in Objective-C and create an iPhone app out of it, mostly because I’ve been listening to music a lot as I work, and I’d like the alert to be audible, so it makes sense to me to put the alert in the audio stream that I’m listening to.

            import java.util.Scanner;

/**
* A timer to implement the Pomodoro method for productivity.
* The timer prints a countdown to the console.
* Presets for 25 minutes and 5 minutes, hit 0 or anything else to exit.
*
* @author Peter Birk (kiyote23@gmail.com)
* @version 0.2 (08/30/13)
*/
class Pomodoro {
// int SECONDS = 60; constants that ended up not being used
// int MILLISECOND = 1000;

static void timer(int minute) throws InterruptedException {
if (minute < 10) {
System.out.print("0" + minute + " : 00");
}
else
System.out.print(minute + " : 00");
for (; minute > 0; )
{
int second = 60;
for (; second > 0; )
{
Thread.sleep(1000);
second--;
if (second >= 10) {
/*System.out.print("\b\b" + second); I sketched this in
Coderunner, where backspace worked
When I ported to Bluej, backspace didn't work, so I
used \f (flush?) Difference is it clears the whole
line instead of going back a couple of spaces. */
System.out.print("\f");
if (minute >= 10)
System.out.print((minute-1) + " : " + second);
//redundent code, revise
else
System.out.print("0" + (minute-1) + " : " + second);
}
else {
//System.out.print("\b\b" + "0" + second);
System.out.print("\f");
if (minute >= 10)
System.out.print((minute-1) + " : 0" + second);
else
System.out.print("0" + (minute-1) + " : 0" + second);
}
}
minute--;
if (minute >= 10)
//System.out.print("\b\b\b\b\b\b\b" + minute + " : 00");
System.out.print("\f" + minute + " : 00");
else
//System.out.print("\b\b\b\b\b\b\b" + "0" + minute + " : 00");
System.out.print("\f" + "0" + minute + " : 00");
}
System.out.print("\nDone!");
}

/* Eventually, I'd like to have the console display the current task
and the current
* round of pomodoro, so that breaks could be scaled after a number of
rounds, but the \f flushes
* the whole console, so I'll have to figure out a different solution,
or wire up a GUI window.
*
* This method isn't used right now.
*/

public static void pomodori(String task) throws InterruptedException {
System.out.println("Pomodoro: \"" + task + "\"");
int round = 1;
boolean completed = false;
while (!completed) {
System.out.println("Round: " + round);
timer(25);
System.out.println("Break.");
//int remainder = round % 4;
if ((round%4)==0)
timer(15);
else
timer(5);
round++;
}
}

public static void main(String[] args) throws InterruptedException
{
/*int time1 = 25; initial variables, ended up not being used
int time2 = 5;
int minute, second;
minute = 2;*/
//timer(2); testing code
//pomodori("Test timer.");
Scanner kb = new Scanner(System.in);
int answer = 1;
while(answer!=0) {
System.out.println("Enter 1 for 25, 2 for 5, 3 for 15, 0 for
quit:");
answer = kb.nextInt();
if (answer==1)
timer(25);
else if (answer==2)
timer(5);
else if (answer==3)
timer(15);
else
answer=0;
}

}
}