New tested Stopwatch :D

This commit is contained in:
LinlyBoi
2022-03-11 21:36:49 +02:00
parent 8ef95916e1
commit c22ad78dc8
6 changed files with 95 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
package Stopwatch;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
public class StopWatch
{
private LocalTime startTime,endTime;
StopWatch()
{
startTime=LocalTime.of(0,0,0);
endTime= LocalTime.of(0,0,0);
}
public LocalTime getStartTime()
{
return startTime;
}
public LocalTime getEndTime()
{
return endTime;
}
public LocalTime start()
{
return startTime=LocalTime.now();
}
public LocalTime stop()
{
return endTime=LocalTime.now();
}
public Long getElapsedTime()
{
Long milliesbetween= ChronoUnit.MILLIS.between(startTime,endTime);
return (milliesbetween);
}
}

View File

@@ -0,0 +1,53 @@
package Stopwatch;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.*;
public class StopWatchTest
{
@Test
public void getElapsedTime() throws InterruptedException
{
StopWatch Watch1 = new StopWatch();
StopWatch Watch2 = new StopWatch();
StopWatch Watch3 = new StopWatch();
StopWatch Watch4 = new StopWatch();
Watch1.start();
try
{
TimeUnit.MINUTES.sleep(1);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Watch1.stop();
Watch2.start();
TimeUnit.MILLISECONDS.sleep(69);
Watch2.stop();
Watch3.start();
TimeUnit.MILLISECONDS.sleep(10);
Watch3.stop();
Watch4.start();
TimeUnit.MILLISECONDS.sleep(12);
Watch4.start();
TimeUnit.MILLISECONDS.sleep(69);
Watch4.stop();
assertEquals(60000, Watch1.getElapsedTime(), 10);
assertEquals(69, Watch2.getElapsedTime(),10);
assertEquals(10, Watch3.getElapsedTime(),10);
assertEquals(69, Watch4.getElapsedTime(),10);
}
}