I am trying to create a program for my college marathon . I want to be able start a stopwatch (timer) for every one running the marathon and as each person finishes the marathon i want to type in there id number and stop there Stopwatch (timer) and print out a statement saying ("you finished the marathon in " + (Time).
Just wondering is this possible to create and what way would i go about it . any help would be greatly appreciated
Regards
Niall
See this answer for creating a digital clock in Swing.
Though this answer is only a clock, it could easily be made into a stop watch. Make use of the System.currentTimeMillis()
. Have a static
start time, that is populated to a data structure of a Runner
object that will hold name
startTime
and endTime
. For each runner when they're finished, they get their own endTime
.
See this question for elapsed time formatting.
UPDATE
There are number of ways you can handle this. One solution is you could store the Runners
in a HashMap
public class Runner {
long endTime;
Integer id;
public void setEndTime(long endTime) {
this.endTime = endTime;
}
}
public class GUI {
Map<Integer, Runner> runners = new HashMap<>();
public GUI {
Runner runner = new Runner(12334....) // id
map.put(runner.getId(), runner);
}
}
Like I said there are a number of ways to set the end time. One way is to have a endTime
variable also in your GUI class. When you click a button, the variable will be assigned. Then in a text field you can type in the runner id, and assign the endtime to the runner in the map that matches the id. So everytime the button is pressed, it will be a new end time set the endTime
variable, so each runner will get their own endTime