I am trying to find out how many minutes that are remaining until the next train will departure, based on the current system time in Java.
I am so far not familiar with the many aspects of Java, but I have managed to get the program to work in most use cases with a switch
statement and while
loop, but it feels like I am using too many workarounds to get the code to work as intended.
The next step is that I am trying to complete, the task in a less tedious and more elegant way. I was wondering if anyone could point me in the right direction to how the task could be completed in a similar way but with other features of Java, such as ArrayList
s, tables etc. that are suitable for what I am trying to accomplish.
public class TrainDepartures{
public static void main(String[] args) {
long time = System.currentTimeMillis();
int initialTime = (int) (time / (1000 * 60) % 60);
int findNextDeparture = (int) (time / (1000 * 60) % 60);
boolean foundDeparture = false;
loop: while (foundDeparture != true) {
findNextDeparture++;
switch (findNextDeparture) {
case 10:
System.out.print("Departure in " + (Math.abs(initialTime - 10)) + " min.");
break loop;
case 20:
System.out.print("Departure in " + (Math.abs(initialTime - 20)) + " min.");
break loop;
case 30:
System.out.print("Departure in " + (Math.abs(initialTime - 30)) + " min.");
break loop;
case 40:
System.out.print("Departure in " + (Math.abs(initialTime - 40)) + " min.");
break loop;
case 50:
System.out.print("Departure in " + (Math.abs(initialTime - 50)) + " min.");
break loop;
case 52:
System.out.print("Departure in " + (Math.abs(initialTime - 52)) + " min.");
break loop;
case 55:
System.out.print("Departure in " + (Math.abs(initialTime - 55)) + " min.");
break loop;
case 56:
System.out.print("Departure in " + (Math.abs(initialTime - 56)) + " min.");
break loop;
case 57:
System.out.print("Departure in " + (Math.abs(initialTime - 57)) + " min.");
break loop;
}
}
}
}