Compare commits
21 Commits
d87e503cfe
...
main
Author | SHA1 | Date | |
---|---|---|---|
854dc0697f | |||
426009bdfe | |||
c976a50051 | |||
a09bdcc7aa | |||
f17eb56506 | |||
2caf663ba7 | |||
4434129f1c | |||
f0bda09710 | |||
1f1e06a8e1 | |||
1e40a19b69 | |||
45bee2714b | |||
971bf22495 | |||
e6ef9ec87f | |||
7fc7efd166 | |||
23d73ee19d | |||
853beb07d2 | |||
e88c1a3d49 | |||
d0687c9568 | |||
d9553ace7a | |||
f8e1b13ae5 | |||
f5474a59fa |
@@ -19,7 +19,10 @@ public class DrinkingBar {
|
||||
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 int DEFAULT_TOTAL_TIME = 8 * MINS_PER_HOUR + MINS_PER_HALF_HOUR;
|
||||
private static final DecimalFormat LITER_FORMAT = new DecimalFormat("0.00");
|
||||
private static final BigDecimal DEFAULT_TOTAL_TIME_BD = BigDecimal.valueOf(DEFAULT_TOTAL_TIME);
|
||||
private static final BigDecimal DEFAULT_TOTAL_LITRES = BigDecimal.valueOf(2.0);
|
||||
private static final BigDecimal QUARTER_LITRE = BigDecimal.valueOf(0.25);
|
||||
private static final DecimalFormat LITRE_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);
|
||||
@@ -28,6 +31,7 @@ public class DrinkingBar {
|
||||
private LocalTime endTime;
|
||||
private long totalMinutes;
|
||||
private BigDecimal totalMinutesBD;
|
||||
private BigDecimal totalLitres;
|
||||
|
||||
|
||||
private DrinkingBar(LocalTime startTime) {
|
||||
@@ -35,6 +39,7 @@ public class DrinkingBar {
|
||||
this.totalMinutes = DEFAULT_TOTAL_TIME;
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
this.endTime = startTime.plusMinutes(totalMinutes);
|
||||
this.totalLitres = DEFAULT_TOTAL_LITRES;
|
||||
}
|
||||
|
||||
|
||||
@@ -57,15 +62,36 @@ public class DrinkingBar {
|
||||
}
|
||||
|
||||
|
||||
protected long getPassedMinutes() {
|
||||
return startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
}
|
||||
|
||||
|
||||
private void setEndTime(LocalTime endTime) {
|
||||
this.endTime = endTime;
|
||||
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
extraInitEndTimeTotalMinutes();
|
||||
}
|
||||
|
||||
|
||||
protected void extraInitEndTimeTotalMinutes() {
|
||||
// correct necessary litres to drink based on the end time.
|
||||
// lower the volume in quarter litre steps
|
||||
BigDecimal calcTotalLitres = DEFAULT_TOTAL_LITRES;
|
||||
BigDecimal totalLitresFromMinutes = DEFAULT_TOTAL_LITRES
|
||||
.multiply(totalMinutesBD) // reverse dreisatz
|
||||
.divide(DEFAULT_TOTAL_TIME_BD, MathContext.DECIMAL64);
|
||||
do {
|
||||
calcTotalLitres = calcTotalLitres.subtract(QUARTER_LITRE);
|
||||
} while (calcTotalLitres.compareTo(totalLitresFromMinutes) >= 0);
|
||||
// add quarter since we always did a step "too many", due to the do ... while loop
|
||||
this.totalLitres = calcTotalLitres.add(QUARTER_LITRE);
|
||||
}
|
||||
|
||||
|
||||
private void showLoadingBar() {
|
||||
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
long passedMinutes = getPassedMinutes();
|
||||
// long passedMinutes = 0; // DEBUG
|
||||
if (passedMinutes > totalMinutes) {
|
||||
passedMinutes = totalMinutes;
|
||||
@@ -84,28 +110,35 @@ public class DrinkingBar {
|
||||
|
||||
|
||||
private String fillLoadingBar(long passedMinutes, boolean progressive) {
|
||||
long effectivePassedMinutes = passedMinutes;
|
||||
if (passedMinutes > MINUTES_BEFORE_PAUSE && passedMinutes <= MINUTES_WITH_PAUSE) {
|
||||
long effectivePassedMinutes = passedMinutes < 0 ? 0 : passedMinutes;
|
||||
if (totalMinutes > MINUTES_WITH_PAUSE && passedMinutes > MINUTES_BEFORE_PAUSE && passedMinutes <= MINUTES_WITH_PAUSE) {
|
||||
effectivePassedMinutes = MINUTES_BEFORE_PAUSE;
|
||||
}
|
||||
double currentLitres = 2.0 / totalMinutes * effectivePassedMinutes + 0.25;
|
||||
double printedLitres = currentLitres - (currentLitres % 0.25);
|
||||
// double currentProgressToNextStep = 100 / 0.25 * (currentLitres - printedLitres);
|
||||
var effectivePassedMinutesBD = BigDecimal.valueOf(effectivePassedMinutes);
|
||||
BigDecimal currentLitres = totalLitres
|
||||
.multiply(effectivePassedMinutesBD) // reverse dreisatz
|
||||
.divide(totalMinutesBD, MathContext.DECIMAL64)
|
||||
.add(QUARTER_LITRE);
|
||||
BigDecimal printedLitres = currentLitres.subtract(currentLitres.remainder(QUARTER_LITRE, MathContext.DECIMAL64));
|
||||
/* BigDecimal currentProgressToNextStep = ONE_HUNDRED_PERCENT
|
||||
.multiply(currentLitres.subtract(printedLitres)) // reverse dreisatz
|
||||
.divide(QUARTER_LITRE, MathContext.DECIMAL64); */
|
||||
BigDecimal minutesToNextStep = getMinutesToNextStep(currentLitres);
|
||||
String progressivePart = progressive ? "\r" : "";
|
||||
return progressivePart + "Aktuelles Volumen: " + LITER_FORMAT.format(printedLitres) + "L - "
|
||||
return progressivePart + "Aktuelles Volumen: " + LITRE_FORMAT.format(printedLitres) + "L - "
|
||||
// + PERCENTAGE_FORMAT.format(currentProgressToNextStep) + "% - "
|
||||
+ minutesToTimeString(minutesToNextStep);
|
||||
}
|
||||
|
||||
|
||||
private BigDecimal getMinutesToNextStep(double currentLitres) {
|
||||
private BigDecimal getMinutesToNextStep(BigDecimal currentLitres) {
|
||||
// berechne Liter benötigt bis zum nächsten 0.25er Schritt
|
||||
double litresToNextStep = 0.25 - (currentLitres % 0.25);
|
||||
BigDecimal litresToNextStep = QUARTER_LITRE.subtract(currentLitres.remainder(QUARTER_LITRE));
|
||||
// berechne Minuten benötigt für 1 Liter
|
||||
double minutesPerLitre = totalMinutes / 2.0;
|
||||
BigDecimal minutesPerLitre = totalMinutesBD.divide(totalLitres, MathContext.DECIMAL64);
|
||||
// berechne Minuten benötigt bis zum nächsten 0.25er Schritt
|
||||
return BigDecimal.valueOf((minutesPerLitre * litresToNextStep) + 1);
|
||||
// runde auf ganze Zahl, da wir nur ganze Minuten anzeigen und damit 1.999 = 2 Minuten sind
|
||||
return minutesPerLitre.multiply(litresToNextStep).setScale(0, RoundingMode.HALF_EVEN);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -77,13 +77,6 @@ public class LoadingBar {
|
||||
}
|
||||
|
||||
|
||||
private void setEndTime(LocalTime endTime) {
|
||||
this.endTime = endTime;
|
||||
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (args.length == 0) {
|
||||
askParametersAndRun();
|
||||
@@ -330,6 +323,19 @@ public class LoadingBar {
|
||||
}
|
||||
|
||||
|
||||
protected long getPassedMinutes() {
|
||||
return startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
}
|
||||
|
||||
|
||||
private void setEndTime(LocalTime endTime) {
|
||||
this.endTime = endTime;
|
||||
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private boolean hasMittagspauseArrived() {
|
||||
return startTime.until(LocalTime.now(), ChronoUnit.MINUTES) < DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH;
|
||||
}
|
||||
@@ -393,8 +399,7 @@ public class LoadingBar {
|
||||
return MIN_LUNCH_DURATION;
|
||||
}
|
||||
long totalDuration = MAX_NUMBER_WORK_MINS + endTimeOffset;
|
||||
long effectiveLunchDuration = totalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
return getMinLunchDuration(effectiveLunchDuration);
|
||||
return getMinLunchDuration(totalDuration);
|
||||
}
|
||||
|
||||
|
||||
@@ -403,12 +408,12 @@ public class LoadingBar {
|
||||
return MIN_LUNCH_DURATION;
|
||||
}
|
||||
long totalDuration = startTime.until(manualEndTime, ChronoUnit.MINUTES);
|
||||
long effectiveLunchDuration = totalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
return getMinLunchDuration(effectiveLunchDuration);
|
||||
return getMinLunchDuration(totalDuration);
|
||||
}
|
||||
|
||||
|
||||
private long getMinLunchDuration(long effectiveLunchDuration) {
|
||||
private long getMinLunchDuration(long precalculatedTotalDuration) {
|
||||
long effectiveLunchDuration = precalculatedTotalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
if (effectiveLunchDuration < 0) {
|
||||
effectiveLunchDuration = 0;
|
||||
}
|
||||
@@ -431,7 +436,7 @@ public class LoadingBar {
|
||||
|
||||
|
||||
private void showLoadingBar() {
|
||||
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
long passedMinutes = getPassedMinutes();
|
||||
// long passedMinutes = 0; // DEBUG
|
||||
if (passedMinutes > totalMinutes) {
|
||||
passedMinutes = totalMinutes;
|
||||
|
@@ -112,6 +112,11 @@ public class SimpleLoadingBar {
|
||||
}
|
||||
|
||||
|
||||
protected long getPassedMinutes() {
|
||||
return startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
}
|
||||
|
||||
|
||||
private void setEndTime(LocalTime endTime) {
|
||||
this.endTime = endTime;
|
||||
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
@@ -128,7 +133,7 @@ public class SimpleLoadingBar {
|
||||
|
||||
|
||||
private void showLoadingBar() {
|
||||
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
long passedMinutes = getPassedMinutes();
|
||||
// long passedMinutes = 0; // DEBUG
|
||||
if (passedMinutes > totalMinutes) {
|
||||
passedMinutes = totalMinutes;
|
||||
|
@@ -1,3 +1,4 @@
|
||||
#/usr/bin/env bash
|
||||
set -uo pipefail
|
||||
|
||||
javac -p target/ src/**/*.java
|
||||
javac -d target/ src/**/*.java
|
||||
|
@@ -31,69 +31,91 @@ public abstract class AbstractLoadingBar {
|
||||
|
||||
protected AbstractLoadingBar(LocalTime startTime, long totalMinutes) {
|
||||
this.startTime = startTime;
|
||||
this.endTime = startTime.plusMinutes(totalMinutes);
|
||||
this.totalMinutes = totalMinutes;
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
setTotalMinutes(totalMinutes);
|
||||
}
|
||||
|
||||
|
||||
protected LocalTime getStartTime() {
|
||||
public LocalTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
|
||||
protected LocalTime getEndTime() {
|
||||
public LocalTime getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
|
||||
protected void setEndTime(LocalTime endTime) {
|
||||
protected final void setEndTime(LocalTime endTime) {
|
||||
this.endTime = endTime;
|
||||
this.totalMinutes = startTime.until(endTime, ChronoUnit.MINUTES);
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
extraInitEndTimeTotalMinutes();
|
||||
}
|
||||
|
||||
|
||||
protected void extraInitEndTimeTotalMinutes() {}
|
||||
|
||||
|
||||
protected long getTotalMinutes() {
|
||||
return totalMinutes;
|
||||
}
|
||||
|
||||
|
||||
protected final void setTotalMinutes(long totalMinutes) {
|
||||
this.totalMinutes = totalMinutes;
|
||||
this.totalMinutesBD = BigDecimal.valueOf(totalMinutes);
|
||||
this.endTime = startTime.plusMinutes(totalMinutes);
|
||||
extraInitEndTimeTotalMinutes();
|
||||
}
|
||||
|
||||
|
||||
protected BigDecimal getTotalMinutesBD() {
|
||||
return totalMinutesBD;
|
||||
}
|
||||
|
||||
|
||||
protected void showLoadingBar() {
|
||||
protected long getPassedMinutes() {
|
||||
return getPassedMinutes(false);
|
||||
}
|
||||
|
||||
|
||||
protected long getPassedMinutes(boolean passedMinutesZero) {
|
||||
return passedMinutesZero ? 0 : startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
}
|
||||
|
||||
|
||||
public void showLoadingBar() {
|
||||
showLoadingBar(false, false, 0);
|
||||
}
|
||||
|
||||
|
||||
protected void showLoadingBarDebug() {
|
||||
public void showLoadingBar(boolean debug, boolean passedMinutesZero) {
|
||||
showLoadingBar(debug, passedMinutesZero, 100L);
|
||||
}
|
||||
|
||||
|
||||
public void showLoadingBarDebug() {
|
||||
showLoadingBar(true, true, 100L);
|
||||
}
|
||||
|
||||
|
||||
protected void showLoadingBarDebug(long millisWaiting) {
|
||||
public void showLoadingBarDebug(long millisWaiting) {
|
||||
showLoadingBar(true, true, millisWaiting);
|
||||
}
|
||||
|
||||
|
||||
protected void showLoadingBarDebug(boolean passedMinutesZero) {
|
||||
public void showLoadingBarDebug(boolean passedMinutesZero) {
|
||||
showLoadingBar(true, passedMinutesZero, 100L);
|
||||
}
|
||||
|
||||
|
||||
protected void showLoadingBarDebug(boolean passedMinutesZero, long millisWaiting) {
|
||||
public void showLoadingBarDebug(boolean passedMinutesZero, long millisWaiting) {
|
||||
showLoadingBar(true, passedMinutesZero, millisWaiting);
|
||||
}
|
||||
|
||||
|
||||
private void showLoadingBar(boolean debug, boolean passedMinutesZero, long millisWaiting) {
|
||||
long passedMinutes = startTime.until(LocalTime.now().truncatedTo(ChronoUnit.MINUTES), ChronoUnit.MINUTES);
|
||||
if (debug && passedMinutesZero) {
|
||||
passedMinutes = 0;
|
||||
}
|
||||
long passedMinutes = getPassedMinutes(debug && passedMinutesZero);
|
||||
if (passedMinutes > totalMinutes) {
|
||||
passedMinutes = totalMinutes;
|
||||
} else if (passedMinutes < 0) {
|
||||
|
@@ -6,6 +6,8 @@ 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;
|
||||
@@ -20,12 +22,12 @@ public class DrinkingBar extends AbstractLoadingBar {
|
||||
private static final int MINUTES_BEFORE_PAUSE = 4 * CommonTools.MINS_PER_HOUR + MINS_PER_HALF_HOUR;
|
||||
private static final int MINUTES_WITH_PAUSE = 6 * CommonTools.MINS_PER_HOUR;
|
||||
private static final int DEFAULT_TOTAL_TIME = 8 * CommonTools.MINS_PER_HOUR + MINS_PER_HALF_HOUR;
|
||||
private static final DecimalFormat LITER_FORMAT = new DecimalFormat("0.00");
|
||||
private static final BigDecimal DEFAULT_TOTAL_TIME_BD = BigDecimal.valueOf(DEFAULT_TOTAL_TIME);
|
||||
private static final BigDecimal DEFAULT_TOTAL_LITRES = BigDecimal.valueOf(2.0);
|
||||
private static final BigDecimal QUARTER_LITRE = BigDecimal.valueOf(0.25);
|
||||
private static final DecimalFormat LITRE_FORMAT = new DecimalFormat("0.00");
|
||||
|
||||
|
||||
protected DrinkingBar(LocalTime startTime) {
|
||||
super(startTime, DEFAULT_TOTAL_TIME);
|
||||
}
|
||||
private BigDecimal totalLitres;
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
@@ -37,29 +39,58 @@ public class DrinkingBar extends AbstractLoadingBar {
|
||||
}
|
||||
|
||||
|
||||
protected DrinkingBar(LocalTime startTime) {
|
||||
super(startTime, DEFAULT_TOTAL_TIME);
|
||||
this.totalLitres = DEFAULT_TOTAL_LITRES;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void extraInitEndTimeTotalMinutes() {
|
||||
// correct necessary litres to drink based on the end time.
|
||||
// lower the volume in quarter litre steps
|
||||
BigDecimal calcTotalLitres = DEFAULT_TOTAL_LITRES;
|
||||
BigDecimal totalLitresFromMinutes = DEFAULT_TOTAL_LITRES
|
||||
.multiply(getTotalMinutesBD()) // reverse dreisatz
|
||||
.divide(DEFAULT_TOTAL_TIME_BD, MathContext.DECIMAL64);
|
||||
do {
|
||||
calcTotalLitres = calcTotalLitres.subtract(QUARTER_LITRE);
|
||||
} while (calcTotalLitres.compareTo(totalLitresFromMinutes) >= 0);
|
||||
// add quarter since we always did a step "too many", due to the do ... while loop
|
||||
this.totalLitres = calcTotalLitres.add(QUARTER_LITRE);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected String fillLoadingBar(long passedMinutes, boolean progressive) {
|
||||
long effectivePassedMinutes = passedMinutes;
|
||||
if (passedMinutes > MINUTES_BEFORE_PAUSE && passedMinutes <= MINUTES_WITH_PAUSE) {
|
||||
long effectivePassedMinutes = passedMinutes < 0 ? 0 : passedMinutes;
|
||||
if (getTotalMinutes() > MINUTES_WITH_PAUSE && passedMinutes > MINUTES_BEFORE_PAUSE && passedMinutes <= MINUTES_WITH_PAUSE) {
|
||||
effectivePassedMinutes = MINUTES_BEFORE_PAUSE;
|
||||
}
|
||||
double currentLitres = 2.0 / getTotalMinutes() * effectivePassedMinutes + 0.25;
|
||||
double printedLitres = currentLitres - (currentLitres % 0.25);
|
||||
// double currentProgressToNextStep = 100 / 0.25 * (currentLitres - printedLitres);
|
||||
var effectivePassedMinutesBD = BigDecimal.valueOf(effectivePassedMinutes);
|
||||
BigDecimal currentLitres = totalLitres
|
||||
.multiply(effectivePassedMinutesBD) // reverse dreisatz
|
||||
.divide(getTotalMinutesBD(), MathContext.DECIMAL64)
|
||||
.add(QUARTER_LITRE);
|
||||
BigDecimal printedLitres = currentLitres.subtract(currentLitres.remainder(QUARTER_LITRE, MathContext.DECIMAL64));
|
||||
/* BigDecimal currentProgressToNextStep = ONE_HUNDRED_PERCENT
|
||||
.multiply(currentLitres.subtract(printedLitres)) // reverse dreisatz
|
||||
.divide(QUARTER_LITRE, MathContext.DECIMAL64); */
|
||||
BigDecimal minutesToNextStep = getMinutesToNextStep(currentLitres);
|
||||
String progressivePart = progressive ? "\r" : "";
|
||||
return progressivePart + "Aktuelles Volumen: " + LITER_FORMAT.format(printedLitres) + "L - "
|
||||
return progressivePart + "Aktuelles Volumen: " + LITRE_FORMAT.format(printedLitres) + "L - "
|
||||
// + FormatTools.PERCENTAGE_FORMAT.format(currentProgressToNextStep) + "% - "
|
||||
+ FormatTools.minutesToTimeString(minutesToNextStep);
|
||||
}
|
||||
|
||||
|
||||
private BigDecimal getMinutesToNextStep(double currentLitres) {
|
||||
private BigDecimal getMinutesToNextStep(BigDecimal currentLitres) {
|
||||
// berechne Liter benötigt bis zum nächsten 0.25er Schritt
|
||||
double litresToNextStep = 0.25 - (currentLitres % 0.25);
|
||||
BigDecimal litresToNextStep = QUARTER_LITRE.subtract(currentLitres.remainder(QUARTER_LITRE));
|
||||
// berechne Minuten benötigt für 1 Liter
|
||||
double minutesPerLitre = getTotalMinutes() / 2.0;
|
||||
BigDecimal minutesPerLitre = getTotalMinutesBD().divide(totalLitres, MathContext.DECIMAL64);
|
||||
// berechne Minuten benötigt bis zum nächsten 0.25er Schritt
|
||||
return BigDecimal.valueOf((minutesPerLitre * litresToNextStep) + 1);
|
||||
// runde auf ganze Zahl, da wir nur ganze Minuten anzeigen und damit 1.999 = 2 Minuten sind
|
||||
return minutesPerLitre.multiply(litresToNextStep).setScale(0, RoundingMode.HALF_EVEN);
|
||||
}
|
||||
}
|
||||
|
@@ -2,59 +2,22 @@ package de.szimnau;
|
||||
|
||||
import de.szimnau.tools.CommonTools;
|
||||
import de.szimnau.tools.FormatTools;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import de.szimnau.tools.LoadingBarCliTools;
|
||||
|
||||
import static de.szimnau.tools.CommonTools.print;
|
||||
import static de.szimnau.tools.CommonTools.println;
|
||||
|
||||
|
||||
public class LoadingBar extends AbstractProgressBar {
|
||||
|
||||
private static final Pattern LUNCH_DURATION_PATTERN = Pattern.compile("\\d+");
|
||||
private static final Pattern OFFSET_PATTERN = Pattern.compile("[+-]\\d+");
|
||||
private static final int MIN_LUNCH_DURATION = 30;
|
||||
private static final LocalTime LATEST_LUNCH_TIME = LocalTime.of(13, 30);
|
||||
private static final long DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH = 5L * CommonTools.MINS_PER_HOUR;
|
||||
private static final int MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH = 6 * CommonTools.MINS_PER_HOUR;
|
||||
private static final long MAX_NUMBER_WORK_MINS = 8L * CommonTools.MINS_PER_HOUR;
|
||||
|
||||
|
||||
private enum DaySection {
|
||||
MITTAG("-m", "Mittag"),
|
||||
ZAPFENSTREICH("-z", "Zapfenstreich");
|
||||
|
||||
private final String param;
|
||||
private final String description;
|
||||
|
||||
|
||||
DaySection(String param, String description) {
|
||||
this.param = param;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
|
||||
public static DaySection byParam(String param) {
|
||||
return Arrays.stream(DaySection.values()).filter((DaySection section) -> section.getParam().equals(param)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
|
||||
public String getParam() {
|
||||
return param;
|
||||
}
|
||||
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
public static final long MAX_NUMBER_WORK_MINS = 8L * CommonTools.MINS_PER_HOUR;
|
||||
public static final int MIN_LUNCH_DURATION = 30;
|
||||
public static final LocalTime LATEST_LUNCH_TIME = LocalTime.of(13, 30);
|
||||
public static final int MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH = 6 * CommonTools.MINS_PER_HOUR;
|
||||
protected static final long DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH = 5L * CommonTools.MINS_PER_HOUR;
|
||||
|
||||
|
||||
protected LoadingBar(LocalTime startTime) {
|
||||
@@ -64,261 +27,36 @@ public class LoadingBar extends AbstractProgressBar {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (args.length == 0) {
|
||||
askParametersAndRun();
|
||||
LoadingBarCliTools.askParametersAndRun(LoadingBar::new);
|
||||
} else {
|
||||
parseParametersAndRun(args);
|
||||
LoadingBarCliTools.parseParametersAndRun(args, LoadingBar::new);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void askParametersAndRun() throws IOException {
|
||||
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
||||
print("Ankunftszeit: ");
|
||||
var startTime = LocalTime.parse(br.readLine(), FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
var lb = new LoadingBar(startTime);
|
||||
if (lb.hasMittagspauseArrived()) {
|
||||
handleMittagspause(br, lb);
|
||||
lb.showLoadingBar();
|
||||
// lb.showLoadingBarDebug(); // DEBUG
|
||||
}
|
||||
handleZapfenstreich(br, lb);
|
||||
lb.showLoadingBar();
|
||||
// lb.showLoadingBarDebug(); // DEBUG
|
||||
public boolean hasMittagspauseArrived() {
|
||||
return hasMittagspauseArrived(false);
|
||||
}
|
||||
|
||||
|
||||
protected static void handleMittagspause(BufferedReader br, LoadingBar lb) throws IOException {
|
||||
print("Mittagspause verschieben um (optional): ");
|
||||
String mittagspauseOffsetRaw = br.readLine();
|
||||
if (mittagspauseOffsetRaw != null && !mittagspauseOffsetRaw.isBlank()) {
|
||||
var mittagspauseOffset = Integer.parseInt(mittagspauseOffsetRaw);
|
||||
lb.initMittagspause(mittagspauseOffset);
|
||||
return;
|
||||
}
|
||||
print("Mittagspause um (optional): ");
|
||||
String manualMittagspauseRaw = br.readLine();
|
||||
if (manualMittagspauseRaw != null && !manualMittagspauseRaw.isBlank()) {
|
||||
var manualMittagspause = LocalTime.parse(manualMittagspauseRaw, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
lb.initMittagspause(manualMittagspause);
|
||||
} else {
|
||||
lb.initMittagspause();
|
||||
}
|
||||
public boolean hasMittagspauseArrived(boolean debugWithPassedMinutesZero) {
|
||||
return getPassedMinutes(debugWithPassedMinutesZero) < DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH;
|
||||
}
|
||||
|
||||
|
||||
protected static void handleZapfenstreich(BufferedReader br, LoadingBar lb) throws IOException {
|
||||
print("Mittagspause hat gedauert (optional): ");
|
||||
String mittagspauseDurationRaw = br.readLine();
|
||||
Integer mittagspauseDuration = null;
|
||||
if (mittagspauseDurationRaw != null && !mittagspauseDurationRaw.isBlank()) {
|
||||
mittagspauseDuration = Integer.valueOf(mittagspauseDurationRaw);
|
||||
}
|
||||
LocalTime vorlaeufigeEndzeit = lb.getStartTime().plusMinutes(MAX_NUMBER_WORK_MINS)
|
||||
.plusMinutes(mittagspauseDuration != null ? mittagspauseDuration : MIN_LUNCH_DURATION);
|
||||
println("Endzeit: " + FormatTools.TIME_FORMATTER.format(vorlaeufigeEndzeit));
|
||||
print("Feierabend verschieben um (optional): ");
|
||||
String zapfenstreichOffsetRaw = br.readLine();
|
||||
Integer zapfenstreichOffset = null;
|
||||
if (zapfenstreichOffsetRaw != null && !zapfenstreichOffsetRaw.isBlank()) {
|
||||
zapfenstreichOffset = Integer.valueOf(zapfenstreichOffsetRaw);
|
||||
lb.initZapfenstreich(mittagspauseDuration, zapfenstreichOffset);
|
||||
return;
|
||||
}
|
||||
print("Manuelle Uhrzeit Feierabend (optional): ");
|
||||
String manualZapfenstreichRaw = br.readLine();
|
||||
LocalTime manualZapfenstreich = null;
|
||||
if (manualZapfenstreichRaw != null && !manualZapfenstreichRaw.isBlank()) {
|
||||
manualZapfenstreich = LocalTime.parse(manualZapfenstreichRaw, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
lb.initZapfenstreich(mittagspauseDuration, manualZapfenstreich);
|
||||
} else {
|
||||
lb.initZapfenstreich(mittagspauseDuration);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void parseParametersAndRun(String[] args) {
|
||||
LoadingBar lb = getLoadingBarFromCLI(args);
|
||||
lb.showLoadingBar();
|
||||
// lb.showLoadingBarDebug(); // DEBUG
|
||||
}
|
||||
|
||||
|
||||
protected static LoadingBar getLoadingBarFromCLI(String[] args) {
|
||||
String nextArg = args[0];
|
||||
if ("--help".equals(nextArg)) {
|
||||
printHelp();
|
||||
System.exit(1);
|
||||
}
|
||||
verifyMinimumNumberOfArgs(args);
|
||||
verifyTimeFormat(nextArg, "Erstes Argument");
|
||||
var startTime = LocalTime.parse(nextArg, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
nextArg = args[1];
|
||||
var section = DaySection.byParam(nextArg);
|
||||
verifyDaySection(section, nextArg);
|
||||
return section == DaySection.MITTAG ? getLoadingBarMittagspause(args, startTime) : getLoadingBarZapfenstreich(args, startTime);
|
||||
}
|
||||
|
||||
|
||||
private static LoadingBar getLoadingBarMittagspause(String[] args, LocalTime startTime) {
|
||||
var lb = new LoadingBar(startTime);
|
||||
if (args.length == 2) {
|
||||
lb.initMittagspause();
|
||||
return lb;
|
||||
}
|
||||
String nextArg = args[2];
|
||||
if (OFFSET_PATTERN.matcher(nextArg).matches()) {
|
||||
lb.initMittagspause(Integer.parseInt(nextArg));
|
||||
return lb;
|
||||
}
|
||||
verifyTimeFormat(nextArg, "Argument nach " + DaySection.MITTAG.getParam());
|
||||
var manualMittagspause = LocalTime.parse(nextArg, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
lb.initMittagspause(manualMittagspause);
|
||||
return lb;
|
||||
}
|
||||
|
||||
|
||||
private static LoadingBar getLoadingBarZapfenstreich(String[] args, LocalTime startTime) {
|
||||
var lb = new LoadingBar(startTime);
|
||||
if (args.length == 2) {
|
||||
lb.initZapfenstreich();
|
||||
return lb;
|
||||
}
|
||||
String nextArg = args[2];
|
||||
LocalTime maxZapfenstreich = null;
|
||||
int endTimeOffset = 0;
|
||||
Integer lunchDuration = null;
|
||||
if (FormatTools.TIME_PATTERN.matcher(nextArg).matches()) {
|
||||
maxZapfenstreich = LocalTime.parse(nextArg, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
} else if (OFFSET_PATTERN.matcher(nextArg).matches()) {
|
||||
endTimeOffset = Integer.parseInt(nextArg);
|
||||
} else {
|
||||
verifyDurationFormat(nextArg, "Argument nach " + DaySection.ZAPFENSTREICH.getParam());
|
||||
lunchDuration = Integer.parseInt(nextArg);
|
||||
}
|
||||
if (args.length == 3) {
|
||||
if (maxZapfenstreich == null && endTimeOffset == 0) {
|
||||
lb.initZapfenstreich(lunchDuration);
|
||||
} else if (maxZapfenstreich == null) {
|
||||
lb.initZapfenstreich(lunchDuration, endTimeOffset);
|
||||
} else {
|
||||
lb.initZapfenstreich(lunchDuration, maxZapfenstreich);
|
||||
}
|
||||
return lb;
|
||||
}
|
||||
nextArg = args[3];
|
||||
if (lunchDuration == null) {
|
||||
println("Letztes Argument darf nur auf Mittagspausendauer folgen.");
|
||||
System.exit(1);
|
||||
}
|
||||
if (maxZapfenstreich == null && !OFFSET_PATTERN.matcher(nextArg).matches()) {
|
||||
verifyTimeFormat(nextArg, "Letztes Argument nach " + DaySection.ZAPFENSTREICH.getParam() + " und Mittagspausendauer");
|
||||
maxZapfenstreich = LocalTime.parse(nextArg, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
lb.initZapfenstreich(lunchDuration, maxZapfenstreich);
|
||||
return lb;
|
||||
}
|
||||
verifyOffsetFormat(nextArg, "Letztes Argument nach " + DaySection.ZAPFENSTREICH.getParam() + " und Enduhrzeit");
|
||||
endTimeOffset = Integer.parseInt(nextArg);
|
||||
lb.initZapfenstreich(lunchDuration, endTimeOffset);
|
||||
return lb;
|
||||
}
|
||||
|
||||
|
||||
private static void verifyMinimumNumberOfArgs(String[] args) {
|
||||
if (args.length >= 2) {
|
||||
return;
|
||||
}
|
||||
println("Mindestens 2 Argumente müssen gegeben sein.");
|
||||
printHelp();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyTimeFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(FormatTools.TIME_PATTERN, param, errMsgPrefix, "Uhrzeitformat (" + FormatTools.TIME_FORMAT + ")", false);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyDurationFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(LUNCH_DURATION_PATTERN, param, errMsgPrefix, "Minutenanzahl (ganze Zahl)", true);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyOffsetFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(OFFSET_PATTERN, param, errMsgPrefix, "Minutendifferenz (ganze Zahl mit Vorzeichen)", false);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyInputFormat(Pattern pattern, String param, String errMsgPrefix, String firstInputPart, boolean timeInputPossible) {
|
||||
if (pattern.matcher(param).matches()) {
|
||||
return;
|
||||
}
|
||||
var possibleTimeInputPart = timeInputPossible ? " oder Uhrzeitformat (" + FormatTools.TIME_FORMAT + ")" : "";
|
||||
println(errMsgPrefix + " \"" + param + "\" muss " + firstInputPart + possibleTimeInputPart + " entsprechen.");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyDaySection(DaySection section, String param) {
|
||||
if (section != null) {
|
||||
return;
|
||||
}
|
||||
List<String> sectionDescs = Arrays.stream(DaySection.values()).map((DaySection ds) -> ds.getDescription() + " (" + ds.getParam() + ")")
|
||||
.collect(Collectors.toList());
|
||||
String sectionDescsJoined = String.join(" oder ", sectionDescs);
|
||||
println("Argument nach Startzeit \"" + param + "\" muss Angabe für " + sectionDescsJoined + " sein.");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
|
||||
private static void printHelp() {
|
||||
println("Mögliche Argumente für LoadingBar:\n"
|
||||
+ "Normalfall Vormittag (Mittagspause <= " + LATEST_LUNCH_TIME + ")\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.MITTAG.getParam() + "\n"
|
||||
+ "Vormittag mit expliziter Mittagspause (<= " + LATEST_LUNCH_TIME + ")\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.MITTAG.getParam() + " " + FormatTools.TIME_FORMAT + "\n"
|
||||
+ "Vormittag mit abweichender Minutenanzahl Arbeitszeit\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.MITTAG.getParam() + " -+mm\n"
|
||||
+ "Normalfall Nachmittag (Mittagspause " + MIN_LUNCH_DURATION + " min)\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + "\n"
|
||||
+ "Nachmittag mit expliziter Länge Mittagspause (Mittagspause unter " + MIN_LUNCH_DURATION + " min wird auf " + MIN_LUNCH_DURATION + " min korrigiert)\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " mm\n"
|
||||
+ "Nachmittag mit explizitem Feierabend (Mittagspause je nach Minimum (s.u.))\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " " + FormatTools.TIME_FORMAT + "\n"
|
||||
+ "Nachmittag mit abweichender Minutenanzahl Arbeitszeit\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " -+mm\n"
|
||||
+ "Nachmittag mit explizitem Feierabend u. expliziter Länge Mittagspause (Mittagspause unter Minimum (s.u.) wird auf Minimum korrigiert)\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " mm " + FormatTools.TIME_FORMAT + "\n"
|
||||
+ "Nachmittag mit explizitem Feierabend u. abweichender Minutenanzahl Arbeitszeit\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " " + FormatTools.TIME_FORMAT + " -+mm\n\n"
|
||||
+ "Mittagspause minimum in Minuten:\n"
|
||||
+ " - bis " + MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min ("
|
||||
+ MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH / CommonTools.MINS_PER_HOUR + " std): 0\n"
|
||||
+ " - bis " + MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min + "
|
||||
+ MIN_LUNCH_DURATION + " min: Arbeitszeit - " + MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min\n"
|
||||
+ " - ab " + MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min + " + MIN_LUNCH_DURATION + " min: "
|
||||
+ MIN_LUNCH_DURATION + " min\n"
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
protected boolean hasMittagspauseArrived() {
|
||||
return getStartTime().until(LocalTime.now(), ChronoUnit.MINUTES) < DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH;
|
||||
}
|
||||
|
||||
|
||||
private void initMittagspause() {
|
||||
public void initMittagspause() {
|
||||
LocalTime defaultEndTime = getStartTime().plusMinutes(DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH);
|
||||
realInitMittagspause(defaultEndTime);
|
||||
}
|
||||
|
||||
|
||||
private void initMittagspause(int endTimeOffset) {
|
||||
public void initMittagspause(int endTimeOffset) {
|
||||
LocalTime offsetEndTime = getStartTime().plusMinutes(DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH + endTimeOffset);
|
||||
realInitMittagspause(offsetEndTime);
|
||||
}
|
||||
|
||||
|
||||
private void initMittagspause(LocalTime manualEndTime) {
|
||||
public void initMittagspause(LocalTime manualEndTime) {
|
||||
LocalTime effectiveEndTime = manualEndTime != null ? manualEndTime : getStartTime().plusMinutes(DEFAULT_NUMBER_WORK_MINS_BEFORE_LUNCH);
|
||||
realInitMittagspause(effectiveEndTime);
|
||||
}
|
||||
@@ -329,18 +67,18 @@ public class LoadingBar extends AbstractProgressBar {
|
||||
}
|
||||
|
||||
|
||||
private void initZapfenstreich() {
|
||||
public void initZapfenstreich() {
|
||||
LocalTime trueEndTime = getStartTime().plusMinutes(MAX_NUMBER_WORK_MINS + MIN_LUNCH_DURATION);
|
||||
realInitZapfenstreich(MIN_LUNCH_DURATION, trueEndTime);
|
||||
}
|
||||
|
||||
|
||||
private void initZapfenstreich(Integer manualLunchDuration) {
|
||||
public void initZapfenstreich(Integer manualLunchDuration) {
|
||||
initZapfenstreich(manualLunchDuration, 0);
|
||||
}
|
||||
|
||||
|
||||
private void initZapfenstreich(Integer manualLunchDuration, int endTimeOffset) {
|
||||
public void initZapfenstreich(Integer manualLunchDuration, int endTimeOffset) {
|
||||
long minLunchDuration = getMinLunchDuration(endTimeOffset);
|
||||
long realLunchDuration = getRealLunchDuration(manualLunchDuration, minLunchDuration);
|
||||
LocalTime trueEndTime = getStartTime().plusMinutes(MAX_NUMBER_WORK_MINS + realLunchDuration + endTimeOffset);
|
||||
@@ -348,7 +86,7 @@ public class LoadingBar extends AbstractProgressBar {
|
||||
}
|
||||
|
||||
|
||||
private void initZapfenstreich(Integer manualLunchDuration, LocalTime manualEndTime) {
|
||||
public void initZapfenstreich(Integer manualLunchDuration, LocalTime manualEndTime) {
|
||||
LocalTime trueEndTime = manualEndTime;
|
||||
long minLunchDuration = getMinLunchDuration(trueEndTime);
|
||||
long realLunchDuration = getRealLunchDuration(manualLunchDuration, minLunchDuration);
|
||||
@@ -360,12 +98,11 @@ public class LoadingBar extends AbstractProgressBar {
|
||||
|
||||
|
||||
private long getMinLunchDuration(int endTimeOffset) {
|
||||
if (endTimeOffset == 0) {
|
||||
if (endTimeOffset <= 0) {
|
||||
return MIN_LUNCH_DURATION;
|
||||
}
|
||||
long totalDuration = MAX_NUMBER_WORK_MINS + endTimeOffset;
|
||||
long effectiveLunchDuration = totalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
return getMinLunchDuration(effectiveLunchDuration);
|
||||
return getMinLunchDuration(totalDuration);
|
||||
}
|
||||
|
||||
|
||||
@@ -374,12 +111,12 @@ public class LoadingBar extends AbstractProgressBar {
|
||||
return MIN_LUNCH_DURATION;
|
||||
}
|
||||
long totalDuration = getStartTime().until(manualEndTime, ChronoUnit.MINUTES);
|
||||
long effectiveLunchDuration = totalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
return getMinLunchDuration(effectiveLunchDuration);
|
||||
return getMinLunchDuration(totalDuration);
|
||||
}
|
||||
|
||||
|
||||
private long getMinLunchDuration(long effectiveLunchDuration) {
|
||||
private long getMinLunchDuration(long precalculatedTotalDuration) {
|
||||
long effectiveLunchDuration = precalculatedTotalDuration - MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH;
|
||||
if (effectiveLunchDuration < 0) {
|
||||
effectiveLunchDuration = 0;
|
||||
}
|
||||
|
@@ -27,7 +27,7 @@ public class SimpleLoadingBar extends AbstractProgressBar {
|
||||
String nextArg = args[0];
|
||||
verifyTimeFormat(nextArg, "Erstes Argument");
|
||||
var firstTime = LocalTime.parse(nextArg, FormatTools.TIME_FORMATTER);
|
||||
LocalTime startTime = null;
|
||||
LocalTime startTime;
|
||||
String title = "";
|
||||
LocalTime endTime = null;
|
||||
if (args.length > 1) {
|
||||
@@ -94,8 +94,8 @@ public class SimpleLoadingBar extends AbstractProgressBar {
|
||||
|
||||
|
||||
@Override
|
||||
protected void showLoadingBar() {
|
||||
showLoadingBar();
|
||||
public void showLoadingBar() {
|
||||
super.showLoadingBar();
|
||||
// showLoadingBarDebug(); // DEBUG
|
||||
println(title);
|
||||
}
|
||||
|
@@ -1,84 +1,37 @@
|
||||
package de.szimnau;
|
||||
|
||||
import de.szimnau.tools.CommonTools;
|
||||
import de.szimnau.tools.FormatTools;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import de.szimnau.tools.LoadingBarCliTools;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static de.szimnau.tools.CommonTools.print;
|
||||
|
||||
|
||||
public class WorkLoadingBar extends AbstractProgressBar {
|
||||
public class WorkLoadingBar extends LoadingBar {
|
||||
|
||||
private final LoadingBar loadingBar;
|
||||
private final DrinkingBar drinkingBar;
|
||||
|
||||
|
||||
private WorkLoadingBar(LoadingBar loadingBar, DrinkingBar drinkingBar) {
|
||||
super(loadingBar.getStartTime());
|
||||
this.loadingBar = loadingBar;
|
||||
this.drinkingBar = drinkingBar;
|
||||
private WorkLoadingBar(LocalTime startTime) {
|
||||
super(startTime);
|
||||
this.loadingBar = new LoadingBar(startTime);
|
||||
this.drinkingBar = new DrinkingBar(startTime);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
if (args.length == 0) {
|
||||
askParametersAndRun();
|
||||
LoadingBarCliTools.askParametersAndRun(WorkLoadingBar::new);
|
||||
} else {
|
||||
parseParametersAndRun(args);
|
||||
LoadingBarCliTools.parseParametersAndRun(args, WorkLoadingBar::new);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void askParametersAndRun() throws IOException {
|
||||
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
||||
print("Ankunftszeit: ");
|
||||
var startTime = LocalTime.parse(br.readLine(), FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
var lb = new LoadingBar(startTime);
|
||||
var db = new DrinkingBar(startTime);
|
||||
var wlb = new WorkLoadingBar(lb, db);
|
||||
if (lb.hasMittagspauseArrived()) {
|
||||
handleMittagspause(br, wlb);
|
||||
wlb.showLoadingBar();
|
||||
// wlb.showLoadingBarDebug(); // DEBUG
|
||||
}
|
||||
handleZapfenstreich(br, wlb);
|
||||
wlb.showLoadingBar();
|
||||
// wlb.showLoadingBarDebug(); // DEBUG
|
||||
}
|
||||
|
||||
|
||||
private static void handleMittagspause(BufferedReader br, WorkLoadingBar wlb) throws IOException {
|
||||
LoadingBar.handleMittagspause(br, wlb.loadingBar);
|
||||
wlb.setEndTime(wlb.loadingBar.getEndTime(), false);
|
||||
}
|
||||
|
||||
|
||||
private static void handleZapfenstreich(BufferedReader br, WorkLoadingBar wlb) throws IOException {
|
||||
LoadingBar.handleZapfenstreich(br, wlb.loadingBar);
|
||||
wlb.setEndTime(wlb.loadingBar.getEndTime(), true);
|
||||
}
|
||||
|
||||
|
||||
private static void parseParametersAndRun(String[] args) {
|
||||
LoadingBar lb = LoadingBar.getLoadingBarFromCLI(args);
|
||||
var db = new DrinkingBar(lb.getStartTime());
|
||||
var wlb = new WorkLoadingBar(lb, db);
|
||||
wlb.setEndTime(wlb.loadingBar.getEndTime(), true);
|
||||
wlb.showLoadingBar();
|
||||
// wlb.showLoadingBarDebug(); // DEBUG
|
||||
}
|
||||
|
||||
|
||||
protected void setEndTime(LocalTime endTime, boolean finalEndTime) {
|
||||
setEndTime(endTime);
|
||||
if (finalEndTime) {
|
||||
drinkingBar.setEndTime(endTime);
|
||||
}
|
||||
@Override
|
||||
protected void extraInitEndTimeTotalMinutes() {
|
||||
LocalTime endTime = getEndTime();
|
||||
loadingBar.setEndTime(endTime);
|
||||
drinkingBar.setEndTime(endTime);
|
||||
}
|
||||
|
||||
|
||||
|
@@ -1,10 +1,8 @@
|
||||
package de.szimnau.tools;
|
||||
|
||||
import java.lang.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class CommonTools {
|
||||
|
@@ -1,6 +1,5 @@
|
||||
package de.szimnau.tools;
|
||||
|
||||
import java.lang.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DecimalFormat;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
@@ -0,0 +1,281 @@
|
||||
package de.szimnau.tools;
|
||||
|
||||
import de.szimnau.LoadingBar;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static de.szimnau.tools.CommonTools.print;
|
||||
import static de.szimnau.tools.CommonTools.println;
|
||||
|
||||
public class LoadingBarCliTools {
|
||||
|
||||
private static final Pattern OFFSET_PATTERN = Pattern.compile("[+-]\\d+");
|
||||
private static final Pattern LUNCH_DURATION_PATTERN = Pattern.compile("\\d+");
|
||||
|
||||
|
||||
private enum DaySection {
|
||||
MITTAG("-m", "Mittag"),
|
||||
ZAPFENSTREICH("-z", "Zapfenstreich");
|
||||
|
||||
private final String param;
|
||||
private final String description;
|
||||
|
||||
|
||||
DaySection(String param, String description) {
|
||||
this.param = param;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
|
||||
public static DaySection byParam(String param) {
|
||||
return Arrays.stream(DaySection.values()).filter((DaySection section) -> section.getParam().equals(param)).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
|
||||
public String getParam() {
|
||||
return param;
|
||||
}
|
||||
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private LoadingBarCliTools() {}
|
||||
|
||||
|
||||
public static void askParametersAndRun(Function<LocalTime, ? extends LoadingBar> constructor) throws IOException {
|
||||
var br = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
|
||||
print("Ankunftszeit: ");
|
||||
var startTime = LocalTime.parse(br.readLine(), FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
LoadingBar lb = constructor.apply(startTime);
|
||||
boolean debug = false;
|
||||
boolean passedMinutesZero = false;
|
||||
if (lb.hasMittagspauseArrived(debug && passedMinutesZero)) {
|
||||
handleMittagspause(br, lb);
|
||||
lb.showLoadingBar(debug, passedMinutesZero);
|
||||
}
|
||||
handleZapfenstreich(br, lb);
|
||||
lb.showLoadingBar(debug, passedMinutesZero);
|
||||
}
|
||||
|
||||
|
||||
private static void handleMittagspause(BufferedReader br, LoadingBar lb) throws IOException {
|
||||
print("Mittagspause verschieben um (optional): ");
|
||||
String mittagspauseOffsetRaw = br.readLine();
|
||||
if (mittagspauseOffsetRaw != null && !mittagspauseOffsetRaw.isBlank()) {
|
||||
var mittagspauseOffset = Integer.parseInt(mittagspauseOffsetRaw);
|
||||
lb.initMittagspause(mittagspauseOffset);
|
||||
return;
|
||||
}
|
||||
print("Mittagspause um (optional): ");
|
||||
String manualMittagspauseRaw = br.readLine();
|
||||
if (manualMittagspauseRaw != null && !manualMittagspauseRaw.isBlank()) {
|
||||
var manualMittagspause = LocalTime.parse(manualMittagspauseRaw, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
lb.initMittagspause(manualMittagspause);
|
||||
} else {
|
||||
lb.initMittagspause();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void handleZapfenstreich(BufferedReader br, LoadingBar lb) throws IOException {
|
||||
print("Mittagspause hat gedauert (optional): ");
|
||||
String mittagspauseDurationRaw = br.readLine();
|
||||
Integer mittagspauseDuration = null;
|
||||
if (mittagspauseDurationRaw != null && !mittagspauseDurationRaw.isBlank()) {
|
||||
mittagspauseDuration = Integer.valueOf(mittagspauseDurationRaw);
|
||||
}
|
||||
LocalTime vorlaeufigeEndzeit = lb.getStartTime().plusMinutes(LoadingBar.MAX_NUMBER_WORK_MINS)
|
||||
.plusMinutes(mittagspauseDuration != null ? mittagspauseDuration : LoadingBar.MIN_LUNCH_DURATION);
|
||||
println("Endzeit: " + FormatTools.TIME_FORMATTER.format(vorlaeufigeEndzeit));
|
||||
print("Feierabend verschieben um (optional): ");
|
||||
String zapfenstreichOffsetRaw = br.readLine();
|
||||
if (zapfenstreichOffsetRaw != null && !zapfenstreichOffsetRaw.isBlank()) {
|
||||
int zapfenstreichOffset = Integer.parseInt(zapfenstreichOffsetRaw);
|
||||
lb.initZapfenstreich(mittagspauseDuration, zapfenstreichOffset);
|
||||
return;
|
||||
}
|
||||
print("Manuelle Uhrzeit Feierabend (optional): ");
|
||||
String manualZapfenstreichRaw = br.readLine();
|
||||
if (manualZapfenstreichRaw != null && !manualZapfenstreichRaw.isBlank()) {
|
||||
LocalTime manualZapfenstreich = LocalTime.parse(manualZapfenstreichRaw, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
lb.initZapfenstreich(mittagspauseDuration, manualZapfenstreich);
|
||||
} else {
|
||||
lb.initZapfenstreich(mittagspauseDuration);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void parseParametersAndRun(String[] args, Function<LocalTime, ? extends LoadingBar> constructor) {
|
||||
String nextArg = args[0];
|
||||
if ("--help".equals(nextArg)) {
|
||||
printHelp();
|
||||
System.exit(1);
|
||||
}
|
||||
verifyMinimumNumberOfArgs(args);
|
||||
verifyTimeFormat(nextArg, "Erstes Argument");
|
||||
var startTime = LocalTime.parse(nextArg, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
nextArg = args[1];
|
||||
var section = DaySection.byParam(nextArg);
|
||||
verifyDaySection(section, nextArg);
|
||||
LoadingBar lb = constructor.apply(startTime);
|
||||
if (section == DaySection.MITTAG) {
|
||||
initLoadingBarMittagspause(args, lb);
|
||||
} else {
|
||||
initLoadingBarZapfenstreich(args, lb);
|
||||
}
|
||||
boolean debug = false;
|
||||
boolean passedMinutesZero = false;
|
||||
lb.showLoadingBar(debug, passedMinutesZero);
|
||||
}
|
||||
|
||||
|
||||
private static void initLoadingBarMittagspause(String[] args, LoadingBar lb) {
|
||||
if (args.length == 2) {
|
||||
lb.initMittagspause();
|
||||
return;
|
||||
}
|
||||
String nextArg = args[2];
|
||||
if (OFFSET_PATTERN.matcher(nextArg).matches()) {
|
||||
lb.initMittagspause(Integer.parseInt(nextArg));
|
||||
return;
|
||||
}
|
||||
verifyTimeFormat(nextArg, "Argument nach " + DaySection.MITTAG.getParam());
|
||||
var manualMittagspause = LocalTime.parse(nextArg, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
lb.initMittagspause(manualMittagspause);
|
||||
}
|
||||
|
||||
|
||||
private static void initLoadingBarZapfenstreich(String[] args, LoadingBar lb) {
|
||||
if (args.length == 2) {
|
||||
lb.initZapfenstreich();
|
||||
return;
|
||||
}
|
||||
String nextArg = args[2];
|
||||
LocalTime maxZapfenstreich = null;
|
||||
int endTimeOffset = 0;
|
||||
Integer lunchDuration = null;
|
||||
if (FormatTools.TIME_PATTERN.matcher(nextArg).matches()) {
|
||||
maxZapfenstreich = LocalTime.parse(nextArg, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
} else if (OFFSET_PATTERN.matcher(nextArg).matches()) {
|
||||
endTimeOffset = Integer.parseInt(nextArg);
|
||||
} else {
|
||||
verifyDurationFormat(nextArg, "Argument nach " + DaySection.ZAPFENSTREICH.getParam());
|
||||
lunchDuration = Integer.parseInt(nextArg);
|
||||
}
|
||||
if (args.length == 3) {
|
||||
if (maxZapfenstreich == null && endTimeOffset == 0) {
|
||||
lb.initZapfenstreich(lunchDuration);
|
||||
} else if (maxZapfenstreich == null) {
|
||||
lb.initZapfenstreich(lunchDuration, endTimeOffset);
|
||||
} else {
|
||||
lb.initZapfenstreich(lunchDuration, maxZapfenstreich);
|
||||
}
|
||||
return;
|
||||
}
|
||||
nextArg = args[3];
|
||||
if (lunchDuration == null) {
|
||||
println("Letztes Argument darf nur auf Mittagspausendauer folgen.");
|
||||
System.exit(1);
|
||||
}
|
||||
if (maxZapfenstreich == null && !OFFSET_PATTERN.matcher(nextArg).matches()) {
|
||||
verifyTimeFormat(nextArg, "Letztes Argument nach " + DaySection.ZAPFENSTREICH.getParam() + " und Mittagspausendauer");
|
||||
maxZapfenstreich = LocalTime.parse(nextArg, FormatTools.TIME_FORMATTER).truncatedTo(ChronoUnit.MINUTES);
|
||||
lb.initZapfenstreich(lunchDuration, maxZapfenstreich);
|
||||
return;
|
||||
}
|
||||
verifyOffsetFormat(nextArg, "Letztes Argument nach " + DaySection.ZAPFENSTREICH.getParam() + " und Enduhrzeit");
|
||||
endTimeOffset = Integer.parseInt(nextArg);
|
||||
lb.initZapfenstreich(lunchDuration, endTimeOffset);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyMinimumNumberOfArgs(String[] args) {
|
||||
if (args.length >= 2) {
|
||||
return;
|
||||
}
|
||||
println("Mindestens 2 Argumente müssen gegeben sein.");
|
||||
printHelp();
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyTimeFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(FormatTools.TIME_PATTERN, param, errMsgPrefix, "Uhrzeitformat (" + FormatTools.TIME_FORMAT + ")", false);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyDurationFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(LUNCH_DURATION_PATTERN, param, errMsgPrefix, "Minutenanzahl (ganze Zahl)", true);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyOffsetFormat(String param, String errMsgPrefix) {
|
||||
verifyInputFormat(OFFSET_PATTERN, param, errMsgPrefix, "Minutendifferenz (ganze Zahl mit Vorzeichen)", false);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyInputFormat(Pattern pattern, String param, String errMsgPrefix, String firstInputPart, boolean timeInputPossible) {
|
||||
if (pattern.matcher(param).matches()) {
|
||||
return;
|
||||
}
|
||||
var possibleTimeInputPart = timeInputPossible ? " oder Uhrzeitformat (" + FormatTools.TIME_FORMAT + ")" : "";
|
||||
println(errMsgPrefix + " \"" + param + "\" muss " + firstInputPart + possibleTimeInputPart + " entsprechen.");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
|
||||
private static void verifyDaySection(DaySection section, String param) {
|
||||
if (section != null) {
|
||||
return;
|
||||
}
|
||||
List<String> sectionDescs = Arrays.stream(DaySection.values()).map((DaySection ds) -> ds.getDescription() + " (" + ds.getParam() + ")")
|
||||
.collect(Collectors.toList());
|
||||
String sectionDescsJoined = String.join(" oder ", sectionDescs);
|
||||
println("Argument nach Startzeit \"" + param + "\" muss Angabe für " + sectionDescsJoined + " sein.");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
|
||||
private static void printHelp() {
|
||||
println("Mögliche Argumente für LoadingBar:\n"
|
||||
+ "Normalfall Vormittag (Mittagspause <= " + LoadingBar.LATEST_LUNCH_TIME + ")\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.MITTAG.getParam() + "\n"
|
||||
+ "Vormittag mit expliziter Mittagspause (<= " + LoadingBar.LATEST_LUNCH_TIME + ")\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.MITTAG.getParam() + " " + FormatTools.TIME_FORMAT + "\n"
|
||||
+ "Vormittag mit abweichender Minutenanzahl Arbeitszeit\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.MITTAG.getParam() + " -+mm\n"
|
||||
+ "Normalfall Nachmittag (Mittagspause " + LoadingBar.MIN_LUNCH_DURATION + " min)\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + "\n"
|
||||
+ "Nachmittag mit expliziter Länge Mittagspause (Mittagspause unter " + LoadingBar.MIN_LUNCH_DURATION + " min wird auf "
|
||||
+ LoadingBar.MIN_LUNCH_DURATION + " min korrigiert)\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " mm\n"
|
||||
+ "Nachmittag mit explizitem Feierabend (Mittagspause je nach Minimum (s.u.))\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " " + FormatTools.TIME_FORMAT + "\n"
|
||||
+ "Nachmittag mit abweichender Minutenanzahl Arbeitszeit\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " -+mm\n"
|
||||
+ "Nachmittag mit explizitem Feierabend u. expliziter Länge Mittagspause (Mittagspause unter Minimum (s.u.) wird auf Minimum korrigiert)\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " mm " + FormatTools.TIME_FORMAT + "\n"
|
||||
+ "Nachmittag mit explizitem Feierabend u. abweichender Minutenanzahl Arbeitszeit\n"
|
||||
+ FormatTools.TIME_FORMAT + " " + DaySection.ZAPFENSTREICH.getParam() + " " + FormatTools.TIME_FORMAT + " -+mm\n\n"
|
||||
+ "Mittagspause minimum in Minuten:\n"
|
||||
+ " - bis " + LoadingBar.MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min ("
|
||||
+ LoadingBar.MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH / CommonTools.MINS_PER_HOUR + " std): 0\n"
|
||||
+ " - bis " + LoadingBar.MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min + "
|
||||
+ LoadingBar.MIN_LUNCH_DURATION + " min: Arbeitszeit - " + LoadingBar.MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min\n"
|
||||
+ " - ab " + LoadingBar.MAX_NUMBER_WORK_MINS_WITHOUT_LUNCH + " min + " + LoadingBar.MIN_LUNCH_DURATION + " min: "
|
||||
+ LoadingBar.MIN_LUNCH_DURATION + " min\n"
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user