Skip to content

Commit

Permalink
Formatting timer to mm:ss
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Hermida committed Apr 21, 2011
1 parent 3fec5be commit 3915ae6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/com/happyprog/pairhero/views/MainView.java
Expand Up @@ -64,8 +64,18 @@ public void gameFinished() {
dialog.open();
}

public void onTimeChange(int seconds) {
updateInfo(timerLabel, String.format("%d", seconds));
public void onTimeChange(int timeInSeconds) {
int minutes = timeInSeconds / 60;
int seconds = timeInSeconds % 60;

updateInfo(timerLabel, String.format("%s:%s", withZeroes(minutes), withZeroes(seconds)));
}

private String withZeroes(int time) {
if (time < 10) {
return String.format("0%d", time);
}
return String.format("%d", time);
}

private void updateInfo(final Label label, final String text) {
Expand Down
51 changes: 51 additions & 0 deletions test/com/happyprog/pairhero/views/TimeFormatLearningTest.java
@@ -0,0 +1,51 @@
package com.happyprog.pairhero.views;

import static org.junit.Assert.*;

import org.junit.Test;

public class TimeFormatLearningTest {
@Test
public void oneSecond() throws Exception {
assertEquals("00:01", format(1));
}

@Test
public void twentySeconds() throws Exception {
assertEquals("00:20", format(20));
}

@Test
public void oneMinute() throws Exception {
assertEquals("01:00", format(60));
}

@Test
public void oneMinuteAndThirtySeconds() throws Exception {
assertEquals("01:30", format(90));
}

@Test
public void twentyFiveMinutes() throws Exception {
assertEquals("25:00", format(1500));
}

@Test
public void twentyFourMinutesAndFiftyNineSeconds() throws Exception {
assertEquals("24:59", format(1499));
}

private String format(int time) {
int minutes = time / 60;
int seconds = time % 60;

return String.format("%s:%s", fill(minutes), fill(seconds));
}

private Object fill(int seconds) {
if (seconds < 10) {
return String.format("0%d", seconds);
}
return String.format("%d", seconds);
}
}

0 comments on commit 3915ae6

Please sign in to comment.