[JAVA] Make your own pomodoro

Introduction

This article is a relay article of "2019 Tech Connect Summer" of Link Information Systems. .. Relayed by a group member of engineer.hanzomon. The link information system Facebook is here.

My name is Go (@Go_in_lis) and I am in charge of the 9th day of the bear team. I will introduce a work method called Pomodoro technique and a simple method (Java) to make a timer required for that work.

Pomodoro technique

The Pomodoro technique is an efficient work method proposed by Francesco Cirillo in the 1980s. The purpose of the Pomodoro technique is to improve the productivity of individuals and teams by following the steps below to increase the concentration and motivation of people.

Pomodoro technique procedure

  1. Set a task (goal).
  2. Perform Pomodoro (work). Usually, one Pomodoro takes 25 minutes.
  3. Take a short break. Usually, one short break is 3-5 minutes.
  4. Return to 2.
  5. When the four Pomodoros are completed, take a long break instead of a short break. Long breaks are usually 15-30 minutes.
  6. Return to 1.

However, concentration varies from person to person. I'm sure there are people who can finally concentrate in 25 minutes but are interrupted, or 5 minutes break is not enough. Therefore, in this article, I will introduce a method to create "your own Pomodoro timer" that can adjust the time of each item.

Implementation environment

Type name
development language Java
Development tools Eclipse Java 2019-06
Development os win10

Pomodoro Flowchart

The flow chart below shows the procedure of the program. The conditions posted in the loop are continuation conditions. pomodoro_flowchat.png

* Flowchart creation tool: https://www.draw.io/

Source code

Timer setting

You can set the time for each item by inputting from the console. And we also have a method that can read the value of each member of the field so that other methods can use the set time.

public class SetTimer
{
	private int pomodoroTime;
	private int shortBreakTime;
	private int pomoNum;
	private int longBreakTime;
	
	//Timer setting
	public SetTimer() throws NumberFormatException, IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.print("Pomodoro time(Minutes)Please enter:");
		pomodoroTime = Integer.parseInt(br.readLine());
		System.out.print("Short break time(Minutes)Please enter:");
		shortBreakTime = Integer.parseInt(br.readLine());
		System.out.print("Enter the number of Pomodoros up to the long break:");
		pomoNum = Integer.parseInt(br.readLine());
		System.out.print("Long break time(Minutes)Please enter:");
		longBreakTime = Integer.parseInt(br.readLine());
	}
	
	//Reading the data of each item
	public int getPomodoroTime()
	{
		return pomodoroTime;
	}
	public int getShortBreakTime()
	{
		return shortBreakTime;
	}
	public int getPomoNum()
	{
		return pomoNum;
	}
	public int getLongBreakTime()
	{
		return longBreakTime;
	}	
}

Countdown processing (using Thread.sleep)

As a countdown processing method used here, first convert the set time to seconds. Then, it loops the operation of waiting for 1 second and then reducing the time by 1 second. The loop ends when the time reaches zero. The code is below.

public class CountDown
{
	//fields
	private static long totalTimeBySec;
	private static long perSec = 1000;
	
	//method
	public static void bySleep(int totalTime) throws InterruptedException		//Countdown method using sleep method
	{
		totalTimeBySec =totalTime * 60;
		for(long i = totalTimeBySec; i >= 0; i--)
		{
			Thread.sleep(perSec);				//Wait a second
			ShowTime.remainTime(i);			//Show remaining time
		}
	}
}

Time display

Digital clocks used in everyday life are generally displayed in two digits regardless of minutes or seconds. Java has a method similar to "% 2d" in c language, but since it is an object-oriented programming language, I would like to use a class called DecimalFormat. The DecimalFormat class can use the constructor to set the decimal format. Then you can use the format () method to change the arguments according to the format. The code is below.

public static void remainTime(long timeBySec)
{
		long min = timeBySec/60;
		long sec = timeBySec%60;
		
		DecimalFormat df = new DecimalFormat("00");	//Output minutes and seconds in two digits
		
		System.out.println(df.format(min) + ":" + df.format(sec));
}

Other message display

Let the user decide whether or not to enter the next task. The io processing method at that time is written in the following class. And when starting or ending the Pomodoro break, I think it is necessary not only to set the display time to zero or the initial value, but also to present a corresponding message. The display methods for these messages are also summarized in the following classes.

public class Message
{
	public static boolean isContinue() throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		while(true)
		{
			System.out.println("Do you want to do the next task?(Y/N)");
			String reply = br.readLine();
			if(reply.equals("Y"))
			{
				System.out.println("Continue. let's do our best.");
				return true;
			}
			else if(reply.equals("N"))
			{
				System.out.println("It's done. thank you for your hard work.");
				return false;
			}
			else
			{
				continue;
			}
		}
	}
	
	public static void startPomo()
	{
		System.out.print("Pomodoro, let's get started.");
	}
	
	public static void startShortBreak()
	{
		System.out.print("Let's start a short break.");
	}
	
	public static void startLongBreak()
	{
		System.out.print("Let's start a long break.");
	}
}

Main processing

If you combine all the processes according to the flowchart, you will have your own Pomodoro timer.

public class Main
{
	public static void main(String[] args) throws InterruptedException, NumberFormatException, IOException
	{
		boolean isContinue = true;
		SetTimer timer = new SetTimer();			//Time setting for each item
		do {
			for(int i = 0; i < timer.getPomoNum(); i++)
			{
				Message.startPomo();
				CountDown.bySleep(timer.getPomodoroTime());			//Pomodoro
				if(i == timer.getPomoNum() - 1)
				{
					Message.startLongBreak();
					CountDown.bySleep(timer.getLongBreakTime());		//Long break
				}
				else
				{
					Message.startShortBreak();
					CountDown.bySleep(timer.getShortBreakTime());	//Short break
				}
			}
			isContinue = Message.isContinue();
		}while(isContinue);
	}
}

result

Let's see the execution result. After execution, you can first set the time of Pomodoro, short break, long break, and the number of Pomodoro until the long break by console input. When all the settings are done, the timer will start. This image has a short time set to show the effect. After one Pomodoro is finished, give a message that a short break will start and start a short break. After a short break, the next Pomodoro will begin. Even when entering a long break, it starts after presenting a message. After a long break, you can decide whether to move on to the next task. Type "Y" to enter the next task and start Pomodoro. Enter "N" to end the timer.

This console application outputs the remaining time line by line. The reason is to make sure that the time is properly counting down every second. To overwrite the previous line each time, it should be possible to use an escape sequence such as "\ b" or "\ r" for the double quotes of System.out.print ("") . , It may not be possible depending on the environment such as Eclipse version and system. I verified that it cannot be realized in this implementation environment.

References

[1] Pomodoro Technique - Wikipedia. https://en.wikipedia.org/wiki/Pomodoro_Technique [2] Cirillo, Francesco. "The pomodoro technique (the pomodoro)." Agile Processes in Software Engineering and 54.2 (2006).

Outlook

This time, I was wondering what the theme should be, and as a result of being slow to decide, I made just a console application. However, as a prospect of this system,

  1. Connect to the database and record the time and work contents
  2. Work efficiency analysis and advice
  3. It looks beautiful if it is an application with GUI or a Web application. Etc. can be realized.

in conclusion

It's boring, but thank you for watching until the end. If I have another chance, I would like to exhibit the upgraded Pomodoro. Next is the article about birds. Thank you, continue!


Link Information Systems Co., Ltd. is recruiting colleagues to work with as needed. We are also looking for work requests and business partners. Please feel free to contact us (https://www.lis.co.jp/inquiry).

Recommended Posts

Make your own pomodoro
Make your own Elasticsearch plugin
Create your own Java annotations
Make your own simple server in Java and understand HTTP
Make your own keyboard QMK in Docker. Volume unique to Windows
Create your own Solr Function Query
Use LocationAwareLogger for your own Logger
Handle your own annotations in Java
Create your own encode for String.getBytes ()
Articles are getting messy, so make a link collection of your own articles
Java: Try implementing your own comma-separated formatter
Try to organize your own object orientation
Create your own validator with Bean Validation
Utilization of Talend component (5) Create your own component