From 922809c595a48f52d56745923f54ff6dffea442d Mon Sep 17 00:00:00 2001 From: Robert Vokac Date: Sun, 21 Jan 2024 13:04:48 +0000 Subject: [PATCH] New improvements --- overtime.txt | 1 + src/main/java/rvc/timecalc/Main.java | 19 ++++++++-- src/main/java/rvc/timecalc/Utils.java | 54 +++++++++++++++++++++++++++ starttime.txt | 1 + 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 overtime.txt create mode 100644 src/main/java/rvc/timecalc/Utils.java create mode 100644 starttime.txt diff --git a/overtime.txt b/overtime.txt new file mode 100644 index 0000000..5b70e78 --- /dev/null +++ b/overtime.txt @@ -0,0 +1 @@ +0:09 \ No newline at end of file diff --git a/src/main/java/rvc/timecalc/Main.java b/src/main/java/rvc/timecalc/Main.java index 2280c98..0a90eba 100644 --- a/src/main/java/rvc/timecalc/Main.java +++ b/src/main/java/rvc/timecalc/Main.java @@ -1,6 +1,8 @@ package rvc.timecalc; import javax.swing.JOptionPane; +import java.io.File; +import java.io.IOException; /** * @author Robert @@ -8,9 +10,13 @@ import javax.swing.JOptionPane; */ public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws IOException { while (true) { boolean test = false; + File starttimeTxt = new File("starttime.txt"); + File overtimeTxt = new File("overtime.txt"); + String lastStartTime = Utils.readTextFromFile(starttimeTxt); + String lastOvertime = Utils.readTextFromFile(overtimeTxt); String startTime = test ? "7:00" : (String) JOptionPane.showInputDialog( null, @@ -19,7 +25,7 @@ public class Main { JOptionPane.PLAIN_MESSAGE, null, null, - "7:00" + lastStartTime == null ? "7:00" : lastStartTime ); String overTime = test ? "0:00" : (String) JOptionPane.showInputDialog( @@ -29,8 +35,15 @@ public class Main { JOptionPane.PLAIN_MESSAGE, null, null, - "0:00" + lastOvertime == null ? "0:00" : lastOvertime ); + + if(!starttimeTxt.exists()) { + Utils.writeTextToFile(starttimeTxt, startTime); + } + if(!overtimeTxt.exists()) { + Utils.writeTextToFile(overtimeTxt, overTime); + } try { TimeCalcWindow timeCalc = new TimeCalcWindow(startTime, overTime); diff --git a/src/main/java/rvc/timecalc/Utils.java b/src/main/java/rvc/timecalc/Utils.java new file mode 100644 index 0000000..6fe1da3 --- /dev/null +++ b/src/main/java/rvc/timecalc/Utils.java @@ -0,0 +1,54 @@ +package rvc.timecalc; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; + +/** + * @author Robert + * @since 15.02.2024 + */ +public class Utils { + private Utils() { + //Not meant to be instantiated. + } + + /** + * Count of bytes per one kilobyte. + */ + private static final int COUNT_OF_BYTES_PER_ONE_KILOBYTE = 1024; + + /** + * Writes text to a file. + * @param file file + * @param text text + */ + public static void writeTextToFile(final File file, final String text) { + FileOutputStream outputStream; + try { + outputStream = new FileOutputStream(file.getAbsolutePath()); + byte[] strToBytes = text.getBytes(); + outputStream.write(strToBytes); + + outputStream.close(); + } catch (IOException e) { + System.err.println(e); + throw new RuntimeException(e); + } + + } + + /** + * Reads text from file. + * @param file file + * @return String + * @throws IOException thrown, if an error during reading happens + */ + public static String readTextFromFile(final File file) + throws IOException { + return new String(Files.readAllBytes(file.toPath())); + } +} diff --git a/starttime.txt b/starttime.txt new file mode 100644 index 0000000..1d467db --- /dev/null +++ b/starttime.txt @@ -0,0 +1 @@ +6:39 \ No newline at end of file