77 lines
3.7 KiB
Java
77 lines
3.7 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.io.IOException;
|
|
import java.math.BigDecimal;
|
|
import java.math.MathContext;
|
|
import java.math.RoundingMode;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.text.DecimalFormat;
|
|
import java.time.LocalTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
public class DrinkingBar {
|
|
|
|
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");
|
|
private static final int MINS_PER_HOUR = 60;
|
|
private static final int MINS_PER_HALF_HOUR = MINS_PER_HOUR / 2;
|
|
private static final int MINUTES_BEFORE_PAUSE = 4 * MINS_PER_HOUR + MINS_PER_HALF_HOUR;
|
|
private static final int MINUTES_WITH_PAUSE = 6 * MINS_PER_HOUR;
|
|
private static final DecimalFormat LITER_FORMAT = new DecimalFormat("0.00");
|
|
private static final DecimalFormat PERCENTAGE_FORMAT = new DecimalFormat("00.00");
|
|
private static final BigDecimal MINS_PER_HOUR_BD = BigDecimal.valueOf(MINS_PER_HOUR);
|
|
private static final MathContext MC_INTEGER = new MathContext(1, RoundingMode.HALF_EVEN);
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
|
System.out.print("Ankunftszeit: ");
|
|
var startTime = LocalTime.parse(br.readLine(), TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
|
|
|
long totalMinutes = 8 * MINS_PER_HOUR + MINS_PER_HALF_HOUR;
|
|
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
|
// long passedMinutes = 0; // DEBUG
|
|
double prevPrintedLitres = 0.0;
|
|
while (passedMinutes < totalMinutes) {
|
|
if (passedMinutes <= MINUTES_BEFORE_PAUSE || passedMinutes > MINUTES_WITH_PAUSE) {
|
|
double currentLitres = 2.0 / totalMinutes * passedMinutes + 0.25;
|
|
double printedLitres = currentLitres - (currentLitres % 0.25);
|
|
double currentProgressToNextStep = 100 / 0.25 * (currentLitres - printedLitres);
|
|
long minutesToNextStep = getMinutesToNextStep(currentLitres, totalMinutes);
|
|
System.out.print("\rAktuelles Volumen: " + LITER_FORMAT.format(printedLitres) + "L - "
|
|
+ PERCENTAGE_FORMAT.format(currentProgressToNextStep) + "% - " + minutesToTimeString(minutesToNextStep));
|
|
prevPrintedLitres = printedLitres;
|
|
}
|
|
try {
|
|
var now = LocalTime.now();
|
|
var oneMinuteLater = now.plusMinutes(1).truncatedTo(ChronoUnit.MINUTES);
|
|
// +1 second to adjust for ignored milliseconds as it is better to switch between 00 and 01 as between 59 and 00
|
|
TimeUnit.SECONDS.sleep(now.until(oneMinuteLater, ChronoUnit.SECONDS) + 1);
|
|
// TimeUnit.MILLISECONDS.sleep(100L); // DEBUG
|
|
} catch (InterruptedException ie) {
|
|
throw new RuntimeException(ie);
|
|
}
|
|
passedMinutes++;
|
|
}
|
|
System.out.println("");
|
|
}
|
|
|
|
|
|
private static long getMinutesToNextStep(double currentLitres, long totalMinutes) {
|
|
// berechne Liter benötigt bis zum nächsten 0.25er Schritt
|
|
double litresToNextStep = 0.25 - (currentLitres % 0.25);
|
|
// berechne Minuten benötigt für 1 Liter
|
|
double minutesPerLitre = totalMinutes / 2.0;
|
|
// berechne Minuten benötigt bis zum nächsten 0.25er Schritt
|
|
return (long) (minutesPerLitre * litresToNextStep) + 1;
|
|
}
|
|
|
|
|
|
private static String minutesToTimeString(long minutes) { // DEBUG
|
|
var minutesBD = BigDecimal.valueOf(minutes);
|
|
BigDecimal[] hoursAndMinutes = minutesBD.divideAndRemainder(MINS_PER_HOUR_BD, MC_INTEGER);
|
|
return LocalTime.of(hoursAndMinutes[0].intValue(), hoursAndMinutes[1].intValue()).format(TIME_FORMATTER);
|
|
}
|
|
}
|